68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
from json import loads
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.openapi.utils import get_openapi
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import JSONResponse
|
|
from pydantic import ValidationError
|
|
|
|
from src.errors import Response, NOT_FOUND
|
|
from src.routes import *
|
|
|
|
from src.routes.api import api
|
|
|
|
app = FastAPI(title='Genshin Impact DB')
|
|
|
|
|
|
@app.exception_handler(404)
|
|
async def not_found(r, e):
|
|
return JSONResponse(
|
|
status_code=404,
|
|
content=Response(error=True, response=NOT_FOUND).model_dump()
|
|
)
|
|
|
|
|
|
@app.exception_handler(ValidationError)
|
|
async def validation_error(_, e: ValidationError):
|
|
return JSONResponse(
|
|
status_code=412,
|
|
content=Response(error=True, response=loads(e.json())).model_dump()
|
|
)
|
|
|
|
|
|
routers = [achievements, adventure_ranks, animals, artifacts, characters, crafts, domains, elements, enemies, foods,
|
|
geographies, materials, namecards, outfits, talent_material_types, weapon_material_types, weapons, gliders,
|
|
tcg, api]
|
|
|
|
for router in routers:
|
|
app.include_router(router)
|
|
|
|
|
|
def custom_openapi():
|
|
if app.openapi_schema:
|
|
return app.openapi_schema
|
|
|
|
openapi_schema = get_openapi(
|
|
title="Genshin Impact DB",
|
|
version="4.2.0",
|
|
summary="This is a very custom OpenAPI schema",
|
|
description="Here's a longer description of the custom **OpenAPI** schema",
|
|
routes=app.routes
|
|
)
|
|
openapi_schema["info"]["x-logo"] = {
|
|
"url": "https://upload.wikimedia.org/wikipedia/en/thumb/5/5d/Genshin_Impact_logo.svg/2560px-Genshin_Impact_logo.svg.png"
|
|
}
|
|
app.openapi_schema = openapi_schema
|
|
return app.openapi_schema
|
|
|
|
|
|
app.openapi = custom_openapi
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=['*'],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|