|
from fastapi import FastAPI, Request |
|
from fastapi.responses import HTMLResponse, RedirectResponse |
|
import urllib.parse |
|
import os |
|
|
|
app = FastAPI() |
|
STORAGE_FILE = "data.txt" |
|
|
|
|
|
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="/") |
|
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() |
|
|
|
return HTMLResponse(content=html.replace("?lists=", f"?lists={stored_data}"), status_code=200) |
|
|