add production flag to config
This commit is contained in:
+2
-1
@@ -10,6 +10,7 @@
|
|||||||
"max_url_length": 131072,
|
"max_url_length": 131072,
|
||||||
"max_retention_days": 3650,
|
"max_retention_days": 3650,
|
||||||
"rate_limit_requests": 10,
|
"rate_limit_requests": 10,
|
||||||
"rate_limit_window": 60
|
"rate_limit_window": 60,
|
||||||
|
"production": true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+7
-2
@@ -68,6 +68,7 @@ def load_config(path: str) -> dict:
|
|||||||
cfg.setdefault("max_retention_days", 3650)
|
cfg.setdefault("max_retention_days", 3650)
|
||||||
cfg.setdefault("rate_limit_requests", 60)
|
cfg.setdefault("rate_limit_requests", 60)
|
||||||
cfg.setdefault("rate_limit_window", 60)
|
cfg.setdefault("rate_limit_window", 60)
|
||||||
|
cfg.setdefault("production", False)
|
||||||
|
|
||||||
# Derive base_path from the path component of base_url.
|
# Derive base_path from the path component of base_url.
|
||||||
raw = urlparse(cfg["base_url"]).path.strip("/")
|
raw = urlparse(cfg["base_url"]).path.strip("/")
|
||||||
@@ -346,9 +347,9 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if path == "/":
|
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")
|
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")
|
self._serve_static("index.html")
|
||||||
else:
|
else:
|
||||||
self._send_json(200, {"status": "ok", "service": "url-shortener"})
|
self._send_json(200, {"status": "ok", "service": "url-shortener"})
|
||||||
@@ -367,6 +368,9 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
self._handle_lookup()
|
self._handle_lookup()
|
||||||
|
|
||||||
elif path == "/static" or path.startswith("/static/"):
|
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"
|
rel = path[len("/static"):].lstrip("/") or "index.html"
|
||||||
self._serve_static(rel)
|
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("Short codes : %s–%s chars", cfg["min_short_length"], cfg["max_short_length"])
|
||||||
log.info("Retention days : %s", cfg["retention_days"])
|
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("Rate limit : %s req/%ss per IP", cfg["rate_limit_requests"], cfg["rate_limit_window"])
|
||||||
|
log.info("Production : %s", cfg["production"])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
|
|||||||
Reference in New Issue
Block a user