redid some code
All checks were successful
Build and Push Docker Image / build (push) Successful in 49s
All checks were successful
Build and Push Docker Image / build (push) Successful in 49s
This commit is contained in:
@@ -3,30 +3,32 @@ import cgi
|
|||||||
import cgitb
|
import cgitb
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
cgitb.enable()
|
cgitb.enable()
|
||||||
print("Content-Type: application/json\n")
|
|
||||||
|
|
||||||
ENV_FILE = "/config/backupbot.env"
|
ENV_FILE = Path("/config/backupbot.env")
|
||||||
|
print("Content-Type: application/json\n")
|
||||||
|
|
||||||
|
|
||||||
def read_env():
|
def read_env():
|
||||||
env = {}
|
env = {}
|
||||||
if os.path.exists(ENV_FILE):
|
if ENV_FILE.exists():
|
||||||
with open(ENV_FILE) as f:
|
with ENV_FILE.open() as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line or line.startswith("#") or "=" not in line:
|
if not line or line.startswith("#") or "=" not in line:
|
||||||
continue
|
continue
|
||||||
key, val = line.split("=", 1)
|
key, val = line.split("=", 1)
|
||||||
key = key.strip()
|
env[key.strip()] = val.strip().split("#")[0].strip()
|
||||||
val = val.strip().split("#")[0].strip() # strip inline comments
|
|
||||||
env[key] = val
|
|
||||||
return env
|
return env
|
||||||
|
|
||||||
|
|
||||||
def write_env(env):
|
def write_env(env):
|
||||||
with open(ENV_FILE, "w") as f:
|
# Rotate the old env file just in case
|
||||||
|
if ENV_FILE.exists():
|
||||||
|
ENV_FILE.replace(ENV_FILE.with_suffix(".env.bak"))
|
||||||
|
with ENV_FILE.open("w") as f:
|
||||||
for key, val in env.items():
|
for key, val in env.items():
|
||||||
f.write(f"{key}={val}\n")
|
f.write(f"{key}={val}\n")
|
||||||
|
|
||||||
@@ -34,17 +36,15 @@ def write_env(env):
|
|||||||
form = cgi.FieldStorage()
|
form = cgi.FieldStorage()
|
||||||
action = form.getvalue("action")
|
action = form.getvalue("action")
|
||||||
|
|
||||||
if action == "get":
|
try:
|
||||||
env = read_env()
|
if action == "get":
|
||||||
print(json.dumps(env))
|
print(json.dumps(read_env()))
|
||||||
elif action == "set":
|
elif action == "set":
|
||||||
try:
|
length = int(os.environ.get("CONTENT_LENGTH", "0"))
|
||||||
raw = os.environ.get("CONTENT_LENGTH")
|
|
||||||
length = int(raw) if raw else 0
|
|
||||||
data = json.loads(os.read(0, length))
|
data = json.loads(os.read(0, length))
|
||||||
write_env(data)
|
write_env(data)
|
||||||
print(json.dumps({"status": "ok", "message": "Configuration saved."}))
|
print(json.dumps({"status": "ok", "message": "Configuration saved."}))
|
||||||
except Exception as e:
|
else:
|
||||||
print(json.dumps({"status": "error", "message": str(e)}))
|
print(json.dumps({"status": "error", "message": "Invalid action"}))
|
||||||
else:
|
except Exception as e:
|
||||||
print(json.dumps({"status": "error", "message": "Invalid action"}))
|
print(json.dumps({"status": "error", "message": str(e)}))
|
||||||
|
|||||||
Reference in New Issue
Block a user