diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..e619f0a --- /dev/null +++ b/.drone.yml @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5d381cc..f80bc9d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +*.json +!caddy.json +caddy/files/ + # ---> Python # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/CaddyAPI.py b/CaddyAPI.py index 8b61ad2..978e68a 100644 --- a/CaddyAPI.py +++ b/CaddyAPI.py @@ -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=}') \ No newline at end of file diff --git a/api-conf.json.sample b/api-conf.json.sample new file mode 100644 index 0000000..83292a0 --- /dev/null +++ b/api-conf.json.sample @@ -0,0 +1,5 @@ +{ + "host": "localhost", + "port": 2019, + "configAdapter": "caddyfile" +} \ No newline at end of file