LD-FDPI/site/add_ld_converter_route.py
2026-11-23 03:45:09 +03:00

40 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import shutil
from pathlib import Path
p = Path('/opt/tylorindar-safelink/main.py')
if not p.exists():
print('FastAPI main.py not found at expected path.')
raise SystemExit(1)
t = p.read_text(encoding='utf-8')
if 'async def ld_converter_page' in t:
print('LD Converter route already present')
raise SystemExit(0)
needle = '@app.get("/{code}")\nasync def redirect_shortlink'
insert = '''@app.get("/ld-converter", include_in_schema=False)
@app.get("/ld-converter/", include_in_schema=False)
async def ld_converter_page():
page = os.path.join(static_dir, "ld-converter.html")
if os.path.exists(page):
return FileResponse(
page,
media_type="text/html",
headers={"Cache-Control": "no-store, max-age=0"},
)
raise HTTPException(status_code=404, detail="LD Converter sayfası bulunamadı.")
'''
if needle not in t:
print('Catch-all route needle not found in main.py')
raise SystemExit(1)
backup_path = Path('/opt/tylorindar-safelink/backups/main.py.bak-ld-converter-page')
backup_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(p, backup_path)
t = t.replace(needle, insert + needle, 1)
p.write_text(t, encoding='utf-8')
print('LD Converter route successfully added to main.py')