feat: server settings and snapshot upload in UI
CI / test (push) Successful in 27s

This commit is contained in:
strsv committed 2026-11-09 04:00:30 +03:00
1 parent ffe9e76a5b
commit e9d6fb8207
9 files changed
+472 -16

No files matched your search

+110
View File
@@ -697,6 +697,37 @@ def game_filters(qs):
MOOD_NAME_RE = re.compile(r"^[a-z0-9][a-z0-9-]{1,39}$")
SERVER_HANDLE_RE = re.compile(r"^[a-z0-9][a-z0-9-]{1,39}$")
SERVER_CONFIG = os.path.join(BASE, "data", "server.json")
def read_server_config():
try:
with open(SERVER_CONFIG, encoding="utf-8") as handle:
data = json.load(handle)
except (OSError, ValueError):
return {}
return data if isinstance(data, dict) else {}
def write_server_config(config):
os.makedirs(os.path.dirname(SERVER_CONFIG), exist_ok=True)
tmp = SERVER_CONFIG + ".tmp"
with open(tmp, "w", encoding="utf-8") as handle:
json.dump(config, handle, ensure_ascii=False, indent=2)
os.replace(tmp, SERVER_CONFIG)
try:
os.chmod(SERVER_CONFIG, 0o600)
except OSError:
pass
def server_public(config):
return {
"url": config.get("url") or "",
"handle": config.get("handle") or "",
"token_set": bool(config.get("token")),
}
def normalize_mood_payload(body):
@@ -824,6 +855,12 @@ class Handler(BaseHTTPRequestHandler):
self._start_hltb(self._body())
elif path == "/api/hltb/match":
self._hltb_match(self._body())
elif path == "/api/server":
self._save_server_config(self._body())
elif path == "/api/server/test":
self._test_server()
elif path == "/api/server/upload":
self._upload_snapshot(self._body())
elif path == "/api/refresh":
body = self._body()
mode = body.get("mode", "incremental")
@@ -1020,6 +1057,10 @@ class Handler(BaseHTTPRequestHandler):
self._activity(qs)
elif path == "/api/update":
self._update(qs)
elif path == "/api/server":
config = read_server_config()
self._json({**server_public(config),
"configured": bool(config.get("url") and config.get("token"))})
elif path == "/api/steam/friends":
self._steam_friends()
elif path.startswith("/api/steam/friends/"):
@@ -2016,6 +2057,75 @@ class Handler(BaseHTTPRequestHandler):
finally:
con.close()
def _save_server_config(self, body):
config = read_server_config()
url = str(body.get("url") or "").strip().rstrip("/")
handle = str(body.get("handle") or "").strip().lower()
if url and not re.match(r"^https?://", url):
self._error(400, "invalid URL")
return
if handle and not SERVER_HANDLE_RE.match(handle):
self._error(400, "invalid handle")
return
if body.get("clear_token"):
config.pop("token", None)
token = body.get("token")
if token is not None:
token = str(token).strip()
if token:
config["token"] = token
config["url"] = url
config["handle"] = handle
write_server_config(config)
self._json({**server_public(config),
"configured": bool(config.get("url") and config.get("token"))})
def _test_server(self):
config = read_server_config()
url = (config.get("url") or "").rstrip("/")
if not url:
self._error(400, "server is not configured")
return
results, errors = {}, {}
for name, path in (("health", "/healthz"), ("ready", "/readyz")):
try:
request = urllib.request.Request(
url + path, headers={"User-Agent": "SteamBase/1.0"})
with urllib.request.urlopen(request, timeout=10) as response:
results[name] = response.status
except urllib.error.HTTPError as exc:
results[name] = exc.code
except Exception as exc:
results[name] = None
errors[name] = type(exc).__name__
self._json({
"url": url,
"health": results.get("health"),
"ready": results.get("ready"),
"ok": results.get("health") == 200 and results.get("ready") == 200,
"error": errors or None,
})
def _upload_snapshot(self, body):
config = read_server_config()
if not config.get("url") or not config.get("token"):
self._error(400, "server is not configured")
return
script = os.path.join(BASE, "tools", "upload_snapshot.py")
args = [script]
if config.get("handle"):
args.extend(["--handle", config["handle"]])
if body.get("no_notes"):
args.append("--no-notes")
if body.get("include_dlc"):
args.append("--include-dlc")
if body.get("mode") in ("replace", "merge"):
args.extend(["--mode", body["mode"]])
if not JOBS.start("snapshot", args):
self._error(409, "another job is running")
return
self._json({"started": True})
def _start_hltb(self, body):
if importlib.util.find_spec("howlongtobeatpy") is None:
self._error(400, "howlongtobeatpy не установлен: pip install -r requirements-hltb.txt")