gidb-server/main.py

68 lines
1.8 KiB
Python
Raw Normal View History

2023-11-12 04:48:15 +03:00
from json import loads
2023-11-12 00:26:15 +03:00
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
2023-11-18 15:40:34 +03:00
from fastapi.middleware.cors import CORSMiddleware
2023-11-12 00:26:15 +03:00
from fastapi.responses import JSONResponse
2023-11-12 04:48:15 +03:00
from pydantic import ValidationError
2023-11-12 00:26:15 +03:00
from src.errors import Response, NOT_FOUND
2023-11-18 15:40:34 +03:00
from src.routes import *
2023-11-12 00:26:15 +03:00
2023-11-12 19:22:49 +03:00
from src.routes.api import api
2023-11-12 00:26:15 +03:00
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()
)
2023-11-12 04:48:15 +03:00
@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,
2023-11-12 19:22:49 +03:00
tcg, api]
2023-11-12 04:48:15 +03:00
for router in routers:
app.include_router(router)
2023-11-12 00:26:15 +03:00
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
2023-11-18 15:40:34 +03:00
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)