add basic use
This commit is contained in:
23
.drone.yml
Normal file
23
.drone.yml
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: exec
|
||||||
|
name: unittests
|
||||||
|
|
||||||
|
platform:
|
||||||
|
os: linux
|
||||||
|
arch: amd64
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: init venv
|
||||||
|
commands:
|
||||||
|
- /usr/bin/python3 -m venv .venv
|
||||||
|
- . .venv/bin/activate
|
||||||
|
- name: install requirements
|
||||||
|
commands:
|
||||||
|
- .venv/bin/pip3 install -r requirements.txt
|
||||||
|
- name: run unittests
|
||||||
|
commands:
|
||||||
|
- .venv/bin/python3 -m CaddyAPI
|
||||||
|
|
||||||
|
node:
|
||||||
|
server: testing
|
||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,3 +1,7 @@
|
|||||||
|
*.json
|
||||||
|
!caddy.json
|
||||||
|
caddy/files/
|
||||||
|
|
||||||
# ---> Python
|
# ---> Python
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|||||||
32
CaddyAPI.py
32
CaddyAPI.py
@@ -28,7 +28,7 @@ class CaddyAPI:
|
|||||||
headers = {"Accept": "application/json"}
|
headers = {"Accept": "application/json"}
|
||||||
match contentType:
|
match contentType:
|
||||||
case 'json':
|
case 'json':
|
||||||
headers['Content-Type'] = 'text/caddyfile'
|
headers['Content-Type'] = 'application/json'
|
||||||
case 'caddyfile':
|
case 'caddyfile':
|
||||||
headers['Content-Type'] = 'text/caddyfile'
|
headers['Content-Type'] = 'text/caddyfile'
|
||||||
case _:
|
case _:
|
||||||
@@ -63,9 +63,9 @@ class CaddyAPI:
|
|||||||
f.write(json.dumps(config))
|
f.write(json.dumps(config))
|
||||||
return config
|
return config
|
||||||
|
|
||||||
def load_config(self, configFile: str, configAdaapter: str = 'json'):
|
def load_config(self, configFile: str, configAdapter: str = 'json'):
|
||||||
if configFile is not None:
|
if configFile is not None:
|
||||||
match configAdaapter:
|
match configAdapter:
|
||||||
case 'json':
|
case 'json':
|
||||||
with open(configFile, 'r') as f:
|
with open(configFile, 'r') as f:
|
||||||
config = json.load(f)
|
config = json.load(f)
|
||||||
@@ -73,9 +73,9 @@ class CaddyAPI:
|
|||||||
case 'caddyfile':
|
case 'caddyfile':
|
||||||
with open(configFile) as f:
|
with open(configFile) as f:
|
||||||
config = f.read()
|
config = f.read()
|
||||||
return self.__query('load', config, configAdaapter)
|
return self.__query('load', config, configAdapter)
|
||||||
case _:
|
case _:
|
||||||
self._log.error(f'not supported {configAdaapter=}')
|
self._log.error(f'not supported {configAdapter=}')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@@ -84,8 +84,24 @@ class CaddyAPI:
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
caddy = CaddyAPI('localhost')
|
from datetime import datetime
|
||||||
data = caddy.get_config('caddy.bak.json')
|
from os import mkdir
|
||||||
print(f'{data=}')
|
from os.path import exists
|
||||||
|
|
||||||
|
now = datetime.now()
|
||||||
|
|
||||||
|
with open('api-conf.json', 'r') as f:
|
||||||
|
conf = json.load(f)
|
||||||
|
|
||||||
|
caddy = CaddyAPI(conf['host'], conf['port'])
|
||||||
|
backdir = 'backup'
|
||||||
|
if not exists(backdir):
|
||||||
|
mkdir(backdir)
|
||||||
|
data = caddy.get_config(f'{backdir}/caddy_{now.strftime("%Y-%m-%d_%H-%M-%s")}_.json')
|
||||||
|
|
||||||
|
if conf['configAdapter'] == 'json':
|
||||||
|
load = caddy.load_config('caddy.json')
|
||||||
|
else:
|
||||||
load = caddy.load_config('Caddyfile', 'caddyfile')
|
load = caddy.load_config('Caddyfile', 'caddyfile')
|
||||||
|
|
||||||
print(f'{load=}')
|
print(f'{load=}')
|
||||||
5
api-conf.json.sample
Normal file
5
api-conf.json.sample
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 2019,
|
||||||
|
"configAdapter": "caddyfile"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user