Complete bootstrap base frontend prototype.

- login page and playlist setting page frontend templates.
- fast API route.
- reading and saving config files
- mock login logic for UI interaction demo
This commit is contained in:
2025-04-04 20:16:09 +09:00
parent 662bc11821
commit e9fcaf508e
7 changed files with 263 additions and 0 deletions
+68
View File
@@ -0,0 +1,68 @@
import os
from app.utils.config import load_config, save_config
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
app = FastAPI()
templates = Jinja2Templates(directory=os.path.join(os.path.dirname(__file__), "templates"))
# mount static files
# 这里的路径是相对于 main.py 文件所在的目录
app.mount("/static", StaticFiles(directory=os.path.join(os.path.dirname(__file__), "static")), name="static")
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
config = load_config()
theme = config.get("theme", "auto")
return templates.TemplateResponse("login.html", {"request": request, "theme": theme, "path": "/login"})
@app.get("/login", response_class=HTMLResponse)
async def login_page(request: Request):
config = load_config()
theme = config.get("theme", "auto")
return templates.TemplateResponse("login.html", {"request": request, "theme": theme, "path": "/login"})
@app.post("/login", response_class=HTMLResponse)
async def login(
request: Request,
user: str = Form(...),
pw: str = Form(...),
url: str = Form(...),
port: str = Form(...),
library: str = Form(...)
):
config = load_config()
theme = config.get("theme", "auto")
# demo:假装连接成功
if user == "admin":
return templates.TemplateResponse("login.html", {"request": request, "message": "连接成功", "success": True, "theme": theme, "path": "/login"})
else:
return templates.TemplateResponse("login.html", {"request": request, "message": "连接失败:用户名错误", "success": False, "theme": theme, "path": "/login"})
@app.get("/playlist", response_class=HTMLResponse)
async def get_playlist(request: Request):
config = load_config()
theme = config.get("theme", "auto")
return templates.TemplateResponse("playlist.html", {"request": request, "theme": theme, "path": "/playlist"})
@app.post("/playlist", response_class=HTMLResponse)
async def set_playlist(request: Request, address: str = Form(...), interval: str = Form(...)):
config = load_config()
theme = config.get("theme", "auto")
# demo:返回提交的设置
return templates.TemplateResponse("playlist.html", {
"request": request,
"message": f"设置成功:地址 {address},间隔 {interval} 分钟",
"theme": theme,
"path": "/playlist"
})
@app.post("/set-theme")
async def set_theme(theme: str = Form(...)):
config = load_config()
config["theme"] = theme
save_config(config)
return RedirectResponse("/login", status_code=303)