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

48 lines
1.5 KiB
Python

#!/usr/bin/env python3
path = "/etc/nginx/sites-enabled/weval-consulting"
with open(path, "rb") as f:
raw = f.read()
lines = raw.split(b"\n")
# Find server 1 end: last "}" BEFORE second "server {"
server_opens = []
for i, ln in enumerate(lines):
if ln.strip() == b"server {":
server_opens.append(i)
print(f"Server opens at lines: {[x+1 for x in server_opens]}")
# Server 1: lines[server_opens[0]] to last "}" before server_opens[1]
# Scan backwards from server_opens[1]-1 to find closing brace at col 0
server1_close = None
for i in range(server_opens[1]-1, server_opens[0], -1):
if lines[i].strip() == b"}":
server1_close = i
break
print(f"Server 1 close at line {server1_close+1}")
# Check if server 1 already has @api_error location
server1 = b"\n".join(lines[server_opens[0]:server1_close+1])
if b"location @api_error" in server1:
print("Server 1 already has @api_error - aborting")
exit(0)
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\",\"msg\":\"service temporarily unavailable\"}';",
b" }",
b"",
]
lines = lines[:server1_close] + insert + lines[server1_close:]
new_raw = b"\n".join(lines)
with open(path, "wb") as f:
f.write(new_raw)
print(f"Inserted @api_error at line {server1_close+1}")
print(f"Size: {len(raw)}{len(new_raw)}")