From 53004a7ecefdf9d17f421eac734f710dbce7f14c Mon Sep 17 00:00:00 2001
From: anima
Date: Sun, 27 Oct 2024 11:23:31 +0100
Subject: [PATCH] add initial version
---
CaddyAPI.py | 69 ++++++++++++++++++++++++++++++++++++++
caddy/compose.yml | 14 ++++++++
caddy/files/conf/Caddyfile | 12 +++++++
requirements.txt | 1 +
4 files changed, 96 insertions(+)
create mode 100644 CaddyAPI.py
create mode 100755 caddy/compose.yml
create mode 100755 caddy/files/conf/Caddyfile
create mode 100644 requirements.txt
diff --git a/CaddyAPI.py b/CaddyAPI.py
new file mode 100644
index 0000000..9e6d1cf
--- /dev/null
+++ b/CaddyAPI.py
@@ -0,0 +1,69 @@
+import requests
+import logging
+import json
+
+class CaddyAPI:
+ """API wrapper for Caddy"""
+ __AUTHOR__ = 'anima'
+ __VERSION__ = '0.1.0'
+
+ _log = logging.getLogger(__name__)
+ __logFile = logging.FileHandler('CaddyAPI.log')
+ __logPrinter = logging.StreamHandler()
+ __logFormat = logging.Formatter('%(asctime)s %(levelname)s \t %(name)s : %(message)s')
+ __logFile.setFormatter(__logFormat)
+ __logPrinter.setFormatter(__logFormat)
+ _log.addHandler(__logFile)
+ _log.addHandler(__logPrinter)
+
+ def __init__(self, host: str, port: int = 2019) -> None:
+ self.__baseurl = host
+ self.__port = port
+
+ def __query(self, path: str, data: str = None) -> dict | bool:
+ """base query method
+
+ """
+ url = f'http://{self.__baseurl}:{self.__port}/{path}'
+ headers = {
+ "Accept": "application/json"
+ }
+ if data is None:
+ response = requests.get(url, headers = headers)
+ else:
+ response = requests.post(url, headers = headers, json = data)
+
+ if response.status_code == 200:
+ content = response.content.decode()
+ if content.startswith('{'):
+ return response.json()
+ else: return True
+ else:
+ self._log.error(f'error on api call {response.status_code} {response.url=} {data=}')
+ return False
+
+
+ def get_config(self, saveTo: str = None) -> dict:
+ config = self.__query('config')
+ if config and config is not None:
+ if saveTo is not None:
+ with open(saveTo, 'w') as f:
+ f.write(json.dumps(config))
+ return config
+
+ def load_config(self, configFile: str):
+ if configFile is not None:
+ with open(configFile, 'r') as f:
+ config = json.load(f)
+ return self.__query('load', config)
+ return config
+ else:
+ return False
+
+
+if __name__ == '__main__':
+ caddy = CaddyAPI('localhost')
+ data = caddy.get_config('caddy.bak.json')
+ print(f'{data=}')
+ load = caddy.load_config('caddy.json')
+ print(f'{load=}')
\ No newline at end of file
diff --git a/caddy/compose.yml b/caddy/compose.yml
new file mode 100755
index 0000000..461a197
--- /dev/null
+++ b/caddy/compose.yml
@@ -0,0 +1,14 @@
+services:
+ caddy:
+ image: caddy:${TAG:-alpine}
+ container_name: caddy
+ restart: unless-stopped
+ ports:
+ - 80:80
+ - 443:443
+ - ${APIPORT:-2019}:2019
+ volumes:
+ - ${BASEDIR:-./files}/conf/:/etc/caddy/
+ - ${BASEDIR:-./files}/data/:/data/
+ #environment:
+ # - CADDY_ADMIN='0.0.0.0'
\ No newline at end of file
diff --git a/caddy/files/conf/Caddyfile b/caddy/files/conf/Caddyfile
new file mode 100755
index 0000000..6bc638a
--- /dev/null
+++ b/caddy/files/conf/Caddyfile
@@ -0,0 +1,12 @@
+# https://caddyserver.com/docs/caddyfile/options#admin
+# https://caddyserver.com/docs/json/admin/
+# https://caddyserver.com/docs/caddyfile
+
+{
+ admin 0.0.0.0:2019
+ auto_https off
+}
+
+:80 {
+ respond "Hello, World"
+}
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..663bd1f
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1 @@
+requests
\ No newline at end of file