first commit of bot config
This commit is contained in:
47
web/backupbot.cgi
Normal file
47
web/backupbot.cgi
Normal file
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env python3
|
||||
import cgi
|
||||
import cgitb
|
||||
import os
|
||||
import json
|
||||
|
||||
cgitb.enable()
|
||||
print("Content-Type: application/json\n")
|
||||
|
||||
ENV_FILE = "/config/web/backupbot.env"
|
||||
|
||||
|
||||
def read_env():
|
||||
env = {}
|
||||
if os.path.exists(ENV_FILE):
|
||||
with open(ENV_FILE) as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#") and "=" in line:
|
||||
key, val = line.split("=", 1)
|
||||
env[key.strip()] = val.strip()
|
||||
return env
|
||||
|
||||
|
||||
def write_env(env):
|
||||
with open(ENV_FILE, "w") as f:
|
||||
for key, val in env.items():
|
||||
f.write(f"{key}={val}\n")
|
||||
|
||||
|
||||
form = cgi.FieldStorage()
|
||||
action = form.getvalue("action")
|
||||
|
||||
if action == "get":
|
||||
env = read_env()
|
||||
print(json.dumps(env))
|
||||
elif action == "set":
|
||||
try:
|
||||
raw = os.environ.get("CONTENT_LENGTH")
|
||||
length = int(raw) if raw else 0
|
||||
data = json.loads(os.read(0, length))
|
||||
write_env(data)
|
||||
print(json.dumps({"status": "ok", "message": "Configuration saved."}))
|
||||
except Exception as e:
|
||||
print(json.dumps({"status": "error", "message": str(e)}))
|
||||
else:
|
||||
print(json.dumps({"status": "error", "message": "Invalid action"}))
|
||||
91
web/index.html
Normal file
91
web/index.html
Normal file
@@ -0,0 +1,91 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>BackupBot Configuration</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: sans-serif;
|
||||
margin: 2rem;
|
||||
background: #f4f4f4;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
input {
|
||||
width: 200px;
|
||||
}
|
||||
|
||||
button {
|
||||
margin-top: 1rem;
|
||||
padding: 0.5rem 1rem;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>BackupBot Configuration</h1>
|
||||
|
||||
<form id="configForm">
|
||||
<label>Timezone:
|
||||
<input type="text" name="TZ">
|
||||
</label>
|
||||
<label>Backup Directory:
|
||||
<input type="text" name="BACKUP_DIR">
|
||||
</label>
|
||||
<label>Log File:
|
||||
<input type="text" name="LOG_FILE">
|
||||
</label>
|
||||
<label>Backup Hour:
|
||||
<input type="number" name="BACKUP_HOUR" min="0" max="23">
|
||||
</label>
|
||||
<label>Backup Minute:
|
||||
<input type="number" name="BACKUP_MINUTE" min="0" max="59">
|
||||
</label>
|
||||
<label>Max Retries:
|
||||
<input type="number" name="MAX_RETRIES" min="1" max="10">
|
||||
</label>
|
||||
<label>Gotify URL:
|
||||
<input type="text" name="GOTIFY_URL">
|
||||
</label>
|
||||
<label>Gotify Token:
|
||||
<input type="text" name="GOTIFY_TOKEN">
|
||||
</label>
|
||||
<button type="submit">Save Configuration</button>
|
||||
</form>
|
||||
|
||||
<p id="status"></p>
|
||||
|
||||
<script>
|
||||
async function loadConfig() {
|
||||
const res = await fetch('/config/web/backupbot.cgi?action=get');
|
||||
const data = await res.json();
|
||||
const form = document.getElementById('configForm');
|
||||
Object.keys(data).forEach(key => {
|
||||
if (form.elements[key]) form.elements[key].value = data[key];
|
||||
});
|
||||
}
|
||||
|
||||
async function saveConfig(e) {
|
||||
e.preventDefault();
|
||||
const formData = new FormData(document.getElementById('configForm'));
|
||||
const obj = Object.fromEntries(formData.entries());
|
||||
const res = await fetch('/config/web/backupbot.cgi?action=set', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify(obj)
|
||||
});
|
||||
const result = await res.json();
|
||||
document.getElementById('status').innerText = result.message;
|
||||
}
|
||||
|
||||
document.getElementById('configForm').addEventListener('submit', saveConfig);
|
||||
loadConfig();
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user