add production flag to config

This commit is contained in:
2026-03-18 03:27:34 +08:00
parent 5fa5d49b82
commit 7d0afa9284
2 changed files with 9 additions and 3 deletions
+2 -1
View File
@@ -10,6 +10,7 @@
"max_url_length": 131072,
"max_retention_days": 3650,
"rate_limit_requests": 10,
"rate_limit_window": 60
"rate_limit_window": 60,
"production": true
}
+7 -2
View File
@@ -68,6 +68,7 @@ def load_config(path: str) -> dict:
cfg.setdefault("max_retention_days", 3650)
cfg.setdefault("rate_limit_requests", 60)
cfg.setdefault("rate_limit_window", 60)
cfg.setdefault("production", False)
# Derive base_path from the path component of base_url.
raw = urlparse(cfg["base_url"]).path.strip("/")
@@ -346,9 +347,9 @@ class Handler(BaseHTTPRequestHandler):
return
if path == "/":
# Serve frontend if static/index.html exists, else health check
# In production, nginx serves static files; backend only serves health check.
index_path = os.path.join(STATIC_DIR, "index.html")
if os.path.isfile(index_path):
if not self.cfg.get("production") and os.path.isfile(index_path):
self._serve_static("index.html")
else:
self._send_json(200, {"status": "ok", "service": "url-shortener"})
@@ -367,6 +368,9 @@ class Handler(BaseHTTPRequestHandler):
self._handle_lookup()
elif path == "/static" or path.startswith("/static/"):
if self.cfg.get("production"):
self._send_empty(404)
return
rel = path[len("/static"):].lstrip("/") or "index.html"
self._serve_static(rel)
@@ -689,6 +693,7 @@ def main() -> None:
log.info("Short codes : %s%s chars", cfg["min_short_length"], cfg["max_short_length"])
log.info("Retention days : %s", cfg["retention_days"])
log.info("Rate limit : %s req/%ss per IP", cfg["rate_limit_requests"], cfg["rate_limit_window"])
log.info("Production : %s", cfg["production"])
try:
server.serve_forever()