cbx / app.py
SalexAI's picture
Update app.py
83787dd verified
raw
history blame
1.14 kB
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, RedirectResponse
import urllib.parse
import os
app = FastAPI()
STORAGE_FILE = "data.txt"
# Initialize file
if not os.path.exists(STORAGE_FILE):
with open(STORAGE_FILE, "w") as f:
f.write("")
def read_data():
with open(STORAGE_FILE, "r") as f:
return f.read()
def write_data(data: str):
with open(STORAGE_FILE, "w") as f:
f.write(data)
@app.get("/store")
async def store(request: Request):
lists = request.query_params.get("lists")
if lists:
decoded = urllib.parse.unquote(lists)
write_data(decoded)
return RedirectResponse(url="/") # Redirect back to main page
return {"status": "error", "message": "Missing 'lists' query parameter."}
@app.get("/", response_class=HTMLResponse)
async def index():
stored_data = urllib.parse.quote(read_data())
with open("index.html", "r", encoding="utf-8") as f:
html = f.read()
# Inject the stored data into the ?lists= param
return HTMLResponse(content=html.replace("?lists=", f"?lists={stored_data}"), status_code=200)