add basic use
This commit is contained in:
34
CaddyAPI.py
34
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=}')
|
||||
Reference in New Issue
Block a user