Files
AiDA_Python/app/main.py

56 lines
1.6 KiB
Python
Raw Normal View History

2024-03-20 11:44:15 +08:00
import logging.config
2024-06-13 14:31:14 +08:00
from http.client import HTTPException
from fastapi.responses import JSONResponse
from fastapi import FastAPI, HTTPException, Request
2024-03-20 11:44:15 +08:00
import uvicorn
from fastapi import FastAPI
from app.api.api_route import router
from app.core.config import settings
2024-09-23 10:47:34 +08:00
from app.core.record_api_count import count_api_calls
2024-06-13 14:31:14 +08:00
from app.schemas.response_template import ResponseModel
2024-03-20 11:44:15 +08:00
from logging_env import LOGGER_CONFIG_DICT
logging.config.dictConfig(LOGGER_CONFIG_DICT)
2024-06-03 11:57:13 +08:00
logging.getLogger("pika").setLevel(logging.WARNING)
2024-03-20 11:44:15 +08:00
from starlette.middleware.cors import CORSMiddleware
def get_application() -> FastAPI:
application = FastAPI(
title=settings.PROJECT_NAME, docs_url="/docs", redoc_url='/re-docs',
openapi_url=f"{settings.API_PREFIX}/openapi.json",
description='''
Base frame with FastAPI
- Super Resolution API
'''
)
application.add_middleware(
CORSMiddleware,
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
2024-09-23 10:47:34 +08:00
application.middleware("http")(count_api_calls)
2024-03-20 11:44:15 +08:00
application.include_router(router=router, prefix=settings.API_PREFIX)
return application
app = get_application()
2024-06-13 14:31:14 +08:00
@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
return JSONResponse(
status_code=exc.status_code,
content=ResponseModel(code=exc.status_code, msg=exc.detail, data=exc.detail).dict()
)
2024-03-20 11:44:15 +08:00
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000)