64 lines
1.5 KiB
Python
64 lines
1.5 KiB
Python
from pathlib import Path
|
|
from typing import Optional
|
|
from fastapi import FastAPI, HTTPException, Request, Response
|
|
from fastapi.staticfiles import StaticFiles
|
|
from typer import Typer, Argument
|
|
import uvicorn
|
|
from anime.routes import router
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
|
|
CURRENT_DIR = Path(__file__).parent
|
|
STATIC_DIR = CURRENT_DIR / "static"
|
|
|
|
cli = Typer()
|
|
|
|
|
|
async def response_formatter(request: Request, call_next):
|
|
try:
|
|
response = await call_next(request)
|
|
print(response)
|
|
except Exception as e:
|
|
response = Response(
|
|
status_code=500,
|
|
content={
|
|
"detail": str(e),
|
|
},
|
|
)
|
|
return response
|
|
|
|
|
|
@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_middleware(BaseHTTPMiddleware, dispatch=response_formatter)
|
|
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()
|