Базовая функциональность
This commit is contained in:
93
src/frontend/main.py
Normal file
93
src/frontend/main.py
Normal file
@@ -0,0 +1,93 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
import uvicorn
|
||||
from dotenv import load_dotenv
|
||||
from fastapi import FastAPI, HTTPException, Query
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
load_dotenv(Path(__file__).parent / ".env")
|
||||
|
||||
SCHEDULE_API = os.getenv("SCHEDULE_API", "http://127.0.0.1:8000").rstrip("/")
|
||||
STATIC_DIR = Path(__file__).parent / "static"
|
||||
|
||||
app = FastAPI(title="Расписание — фронт")
|
||||
|
||||
|
||||
def _proxy(path: str, params: dict):
|
||||
try:
|
||||
resp = requests.get(f"{SCHEDULE_API}/{path}", params=params, timeout=30)
|
||||
except requests.RequestException as exc:
|
||||
raise HTTPException(status_code=502, detail=f"Бэкенд недоступен: {exc}")
|
||||
if resp.status_code != 200:
|
||||
try:
|
||||
detail = resp.json().get("detail", resp.text)
|
||||
except Exception:
|
||||
detail = resp.text
|
||||
raise HTTPException(status_code=resp.status_code, detail=detail)
|
||||
return resp.json()
|
||||
|
||||
|
||||
@app.get("/api/groups")
|
||||
def api_groups(force: bool = Query(False)):
|
||||
return _proxy("groups", {"force": force})
|
||||
|
||||
|
||||
@app.get("/api/schedule/{group}")
|
||||
def api_schedule(group: str, force: bool = Query(False)):
|
||||
return _proxy(f"schedule/{group}", {"force": force})
|
||||
|
||||
|
||||
@app.get("/api/teachers")
|
||||
def api_teachers(force: bool = Query(False)):
|
||||
return _proxy("teachers", {"force": force})
|
||||
|
||||
|
||||
@app.get("/api/rooms")
|
||||
def api_rooms(force: bool = Query(False)):
|
||||
return _proxy("rooms", {"force": force})
|
||||
|
||||
|
||||
@app.get("/api/teacher/{name}")
|
||||
def api_teacher(name: str, force: bool = Query(False)):
|
||||
return _proxy(f"teacher/{name}", {"force": force})
|
||||
|
||||
|
||||
@app.get("/api/room/{label}")
|
||||
def api_room(label: str, force: bool = Query(False)):
|
||||
return _proxy(f"room/{label}", {"force": force})
|
||||
|
||||
|
||||
@app.get("/api/rooms-free")
|
||||
def api_rooms_free(day: str, period: int, force: bool = Query(False)):
|
||||
return _proxy("rooms-free", {"day": day, "period": period, "force": force})
|
||||
|
||||
|
||||
@app.get("/api/rooms-day")
|
||||
def api_rooms_day(day: str, force: bool = Query(False)):
|
||||
return _proxy("rooms-day", {"day": day, "force": force})
|
||||
|
||||
|
||||
@app.get("/api/week")
|
||||
def api_week(force: bool = Query(False)):
|
||||
return _proxy("week", {"force": force})
|
||||
|
||||
|
||||
@app.get("/api/status")
|
||||
def api_status(force: bool = Query(False)):
|
||||
return _proxy("status", {"force": force})
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def index():
|
||||
return FileResponse(STATIC_DIR / "index.html")
|
||||
|
||||
|
||||
app.mount("/", StaticFiles(directory=STATIC_DIR, html=True), name="static")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(app, host="127.0.0.1", port=8080)
|
||||
|
||||
Reference in New Issue
Block a user