Files
weval-l99/fix_api_error.py
2026-04-20 14:29:18 +02:00

61 lines
1.8 KiB
Python

#!/usr/bin/env python3
path = "/etc/nginx/sites-enabled/weval-consulting"
with open(path, "rb") as f:
raw = f.read()
if b"location @api_error" in raw:
print("ALREADY")
exit(0)
# Find the FIRST "server {" and its closing "}"
# Server 1 goes from line 7 to line 55 per grep
# Insert @api_error definition BEFORE the closing "}" of server 1
# Find line "^}\n" that comes after line with "error_page 502 503 504 =503 @api_error"
# Pattern: first standalone closing brace after @api_error usage
# Split into lines with byte offsets
lines = raw.split(b"\n")
# Find @api_error first usage
api_err_line = None
for i, ln in enumerate(lines):
if b"@api_error" in ln:
api_err_line = i
break
# Find first standalone "}" after that
close_line = None
for i in range(api_err_line, len(lines)):
if lines[i].strip() == b"}":
close_line = i
break
if close_line is None:
print("NO_CLOSE")
exit(1)
# Insert @api_error location before close_line
insert = [
b"",
b" # V88: named location @api_error - JSON error for API FastCGI failures",
b" location @api_error {",
b" default_type application/json;",
b" return 503 '{\"ok\":false,\"error\":\"api_error\",\"upstream\":\"fpm\",\"msg\":\"service temporarily unavailable\"}';",
b" }",
b"",
]
lines = lines[:close_line] + insert + lines[close_line:]
new_raw = b"\n".join(lines)
with open(path, "wb") as f:
f.write(new_raw)
print(f"Inserted @api_error at line {close_line}")
print(f"Size: {len(raw)}{len(new_raw)}")
# Also insert in server 2 (HTTPS) if same issue
# Find second @api_error
idx2 = new_raw.find(b"@api_error", new_raw.find(b"@api_error") + 15)
# Check if server 2 needs it too (we check if @api_error location is present in server 2)
# For safety, insert also in HTTPS block