61 lines
1.3 KiB
Python
61 lines
1.3 KiB
Python
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from typer import Argument, Typer
|
|
|
|
from anime.routes import router
|
|
|
|
CURRENT_DIR = Path(__file__).parent
|
|
STATIC_DIR = CURRENT_DIR / "static"
|
|
|
|
cli = Typer()
|
|
|
|
|
|
async def default_exception_handler(request, exc):
|
|
return JSONResponse(
|
|
{
|
|
"detail": str(exc),
|
|
},
|
|
status_code=500,
|
|
)
|
|
|
|
|
|
@cli.command()
|
|
def run_app(
|
|
host: str = "0.0.0.0",
|
|
port: int = 8000,
|
|
anime_dir: Optional[Path] = Argument(None, help="Directory where anime is stored."),
|
|
):
|
|
app = FastAPI(
|
|
title="Anime Watcher",
|
|
description="API for RCE",
|
|
openapi_url="/api/openapi.json",
|
|
docs_url="/api/docs",
|
|
)
|
|
app.include_router(router, prefix="/api")
|
|
app.mount(
|
|
"/",
|
|
StaticFiles(
|
|
directory=STATIC_DIR,
|
|
html=True,
|
|
check_dir=False,
|
|
),
|
|
)
|
|
app.add_exception_handler(Exception, default_exception_handler)
|
|
app.state.anime_dir = anime_dir
|
|
app.state.pid = None
|
|
|
|
uvicorn.run(app, host=host, port=port, workers=1)
|
|
|
|
|
|
def main():
|
|
cli()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|