53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
from fastapi import FastAPI, Request
|
|
from fastapi.responses import HTMLResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.templating import Jinja2Templates
|
|
from pathlib import Path
|
|
from contextlib import asynccontextmanager
|
|
|
|
from app.config import settings, UNIVERSES, BASE_DIR
|
|
from app.routers import api, proxy
|
|
from app.proxy import close_http_client
|
|
from app.personas import ensure_dirs
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
ensure_dirs()
|
|
yield
|
|
await close_http_client()
|
|
|
|
app = FastAPI(
|
|
title=settings.app_name,
|
|
version="1.0.0",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# Mount static files
|
|
static_dir = BASE_DIR / "static"
|
|
static_dir.mkdir(parents=True, exist_ok=True)
|
|
app.mount("/static", StaticFiles(directory=str(static_dir)), name="static")
|
|
|
|
# Templates
|
|
templates_dir = BASE_DIR / "app" / "templates"
|
|
templates = Jinja2Templates(directory=str(templates_dir))
|
|
|
|
# Include Routers
|
|
app.include_router(api.router)
|
|
app.include_router(proxy.router)
|
|
|
|
@app.get("/", response_class=HTMLResponse)
|
|
@app.head("/", response_class=HTMLResponse)
|
|
async def index_view(request: Request):
|
|
return templates.TemplateResponse(
|
|
request=request,
|
|
name="index.html",
|
|
context={
|
|
"universes": UNIVERSES,
|
|
"app_name": settings.app_name
|
|
}
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run("app.main:app", host=settings.host, port=settings.port, reload=settings.debug)
|