65 lines
1.6 KiB
Python
65 lines
1.6 KiB
Python
import contextlib
|
|
|
|
import taskiq_fastapi
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.middleware.gzip import GZipMiddleware
|
|
from fastapi.responses import ORJSONResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from starlette.responses import JSONResponse
|
|
|
|
import routers
|
|
from task_management import broker
|
|
from utils.auto_include_routers import auto_include_routers
|
|
from utils.exceptions import *
|
|
|
|
origins = ["http://localhost:3000"]
|
|
|
|
|
|
@contextlib.asynccontextmanager
|
|
async def lifespan(app):
|
|
if not broker.is_worker_process:
|
|
await broker.startup()
|
|
|
|
yield
|
|
|
|
if not broker.is_worker_process:
|
|
await broker.shutdown()
|
|
|
|
|
|
app = FastAPI(
|
|
separate_input_output_schemas=True,
|
|
default_response_class=ORJSONResponse,
|
|
root_path="/api",
|
|
# lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
app.add_middleware(
|
|
GZipMiddleware,
|
|
minimum_size=1_000,
|
|
)
|
|
|
|
taskiq_fastapi.init(broker, "main:app")
|
|
|
|
|
|
@app.exception_handler(ObjectNotFoundException)
|
|
async def not_found_exception_handler(request: Request, exc: ObjectNotFoundException):
|
|
return JSONResponse(status_code=404, content={"detail": exc.name})
|
|
|
|
|
|
@app.exception_handler(ForbiddenException)
|
|
async def forbidden_exception_handler(request: Request, exc: ForbiddenException):
|
|
return JSONResponse(status_code=403, content={"detail": exc.name})
|
|
|
|
|
|
auto_include_routers(app, routers, True)
|
|
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|