-
Notifications
You must be signed in to change notification settings - Fork 364
/
__init__.py
executable file
·45 lines (37 loc) · 1.3 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from datetime import timezone
import logging
from apscheduler.schedulers.background import BackgroundScheduler
from config import DOCS
from fastapi import FastAPI, Request, status
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi_responses import custom_openapi
app = FastAPI(
docs_url='/docs' if DOCS else None,
redoc_url='/redoc' if DOCS else None
)
app.openapi = custom_openapi(app)
scheduler = BackgroundScheduler({'apscheduler.job_defaults.max_instances': 5}, timezone='UTC')
logger = logging.getLogger('uvicorn.error')
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
from app import dashboard, jobs, views, telegram # noqa
@app.on_event("startup")
def startup():
scheduler.start()
@app.exception_handler(RequestValidationError)
def validation_exception_handler(request: Request, exc: RequestValidationError):
details = {}
for error in exc.errors():
details[error["loc"][1]] = error["msg"]
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder({"detail": details}),
)