added timezone dropdown selector
This commit is contained in:
@@ -3,48 +3,64 @@ import cgi
|
|||||||
import cgitb
|
import cgitb
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
import glob
|
||||||
|
|
||||||
cgitb.enable()
|
cgitb.enable()
|
||||||
|
|
||||||
ENV_FILE = Path("/config/backupbot.env")
|
|
||||||
print("Content-Type: application/json\n")
|
print("Content-Type: application/json\n")
|
||||||
|
|
||||||
|
ENV_FILE = "/config/backupbot.env"
|
||||||
|
ZONEINFO_DIR = "/usr/share/zoneinfo"
|
||||||
|
|
||||||
|
|
||||||
def read_env():
|
def read_env():
|
||||||
env = {}
|
env = {}
|
||||||
if ENV_FILE.exists():
|
if os.path.exists(ENV_FILE):
|
||||||
with ENV_FILE.open() as f:
|
with open(ENV_FILE) 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)
|
||||||
env[key.strip()] = val.strip().split("#")[0].strip()
|
key = key.strip()
|
||||||
|
val = val.strip().split("#")[0].strip()
|
||||||
|
env[key] = val
|
||||||
return env
|
return env
|
||||||
|
|
||||||
|
|
||||||
def write_env(env):
|
def write_env(env):
|
||||||
# Rotate the old env file just in case
|
with open(ENV_FILE, "w") as f:
|
||||||
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")
|
||||||
|
|
||||||
|
|
||||||
|
def list_timezones():
|
||||||
|
zones = []
|
||||||
|
for root, _, files in os.walk(ZONEINFO_DIR):
|
||||||
|
rel_root = os.path.relpath(root, ZONEINFO_DIR)
|
||||||
|
if rel_root.startswith("posix") or rel_root.startswith("right"):
|
||||||
|
continue
|
||||||
|
for file in files:
|
||||||
|
if file.startswith(".") or file.endswith((".tab", ".zi")):
|
||||||
|
continue
|
||||||
|
zones.append(os.path.join(rel_root, file) if rel_root != "." else file)
|
||||||
|
return sorted(zones)
|
||||||
|
|
||||||
|
|
||||||
form = cgi.FieldStorage()
|
form = cgi.FieldStorage()
|
||||||
action = form.getvalue("action")
|
action = form.getvalue("action")
|
||||||
|
|
||||||
try:
|
if action == "get":
|
||||||
if action == "get":
|
print(json.dumps(read_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."}))
|
||||||
else:
|
except Exception as e:
|
||||||
print(json.dumps({"status": "error", "message": "Invalid action"}))
|
print(json.dumps({"status": "error", "message": str(e)}))
|
||||||
except Exception as e:
|
elif action == "get_timezones":
|
||||||
print(json.dumps({"status": "error", "message": str(e)}))
|
print(json.dumps({"timezones": list_timezones()}))
|
||||||
|
else:
|
||||||
|
print(json.dumps({"status": "error", "message": "Invalid action"}))
|
||||||
|
|||||||
@@ -32,7 +32,9 @@
|
|||||||
|
|
||||||
<form id="configForm">
|
<form id="configForm">
|
||||||
<label>Timezone:
|
<label>Timezone:
|
||||||
<input type="text" name="TZ">
|
<select id="tzSelect" name="TZ">
|
||||||
|
<option value="">Loading...</option>
|
||||||
|
</select>
|
||||||
</label>
|
</label>
|
||||||
<label>Backup Directory:
|
<label>Backup Directory:
|
||||||
<input type="text" name="BACKUP_DIR">
|
<input type="text" name="BACKUP_DIR">
|
||||||
@@ -61,6 +63,19 @@
|
|||||||
<p id="status"></p>
|
<p id="status"></p>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
async function loadTimezones() {
|
||||||
|
const res = await fetch('/cgi-bin/backupbot.cgi?action=get_timezones');
|
||||||
|
const data = await res.json();
|
||||||
|
const select = document.getElementById('tzSelect');
|
||||||
|
select.innerHTML = '';
|
||||||
|
data.timezones.forEach(tz => {
|
||||||
|
const opt = document.createElement('option');
|
||||||
|
opt.value = tz;
|
||||||
|
opt.textContent = tz;
|
||||||
|
select.appendChild(opt);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function loadConfig() {
|
async function loadConfig() {
|
||||||
const res = await fetch('/cgi-bin/backupbot.cgi?action=get');
|
const res = await fetch('/cgi-bin/backupbot.cgi?action=get');
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
|||||||
Reference in New Issue
Block a user