add basic use

This commit is contained in:
2024-10-27 13:22:04 +01:00
parent 54f1dbbf3c
commit 1a62638d34
4 changed files with 57 additions and 9 deletions

23
.drone.yml Normal file
View 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
View File

@@ -1,3 +1,7 @@
*.json
!caddy.json
caddy/files/
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/

View File

@@ -28,7 +28,7 @@ class CaddyAPI:
headers = {"Accept": "application/json"}
match contentType:
case 'json':
headers['Content-Type'] = 'text/caddyfile'
headers['Content-Type'] = 'application/json'
case 'caddyfile':
headers['Content-Type'] = 'text/caddyfile'
case _:
@@ -63,9 +63,9 @@ class CaddyAPI:
f.write(json.dumps(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:
match configAdaapter:
match configAdapter:
case 'json':
with open(configFile, 'r') as f:
config = json.load(f)
@@ -73,9 +73,9 @@ class CaddyAPI:
case 'caddyfile':
with open(configFile) as f:
config = f.read()
return self.__query('load', config, configAdaapter)
return self.__query('load', config, configAdapter)
case _:
self._log.error(f'not supported {configAdaapter=}')
self._log.error(f'not supported {configAdapter=}')
return False
else:
@@ -84,8 +84,24 @@ class CaddyAPI:
if __name__ == '__main__':
caddy = CaddyAPI('localhost')
data = caddy.get_config('caddy.bak.json')
print(f'{data=}')
load = caddy.load_config('Caddyfile', 'caddyfile')
from datetime import datetime
from os import mkdir
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')
print(f'{load=}')

5
api-conf.json.sample Normal file
View File

@@ -0,0 +1,5 @@
{
"host": "localhost",
"port": 2019,
"configAdapter": "caddyfile"
}