2023-08-18 11:18:29 +03:00
|
|
|
import os
|
2023-08-21 13:25:16 +03:00
|
|
|
|
2023-08-18 11:18:29 +03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
# import needed here when running main.py to debug backend
|
|
|
|
# you will need to run pip install python-dotenv
|
2023-08-25 13:03:13 +03:00
|
|
|
from dotenv import load_dotenv # type: ignore
|
2023-08-21 13:25:16 +03:00
|
|
|
|
2023-08-18 11:18:29 +03:00
|
|
|
load_dotenv()
|
|
|
|
import sentry_sdk
|
|
|
|
from fastapi import FastAPI, HTTPException, Request, status
|
|
|
|
from fastapi.exceptions import RequestValidationError
|
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from logger import get_logger
|
|
|
|
from middlewares.cors import add_cors_middleware
|
2023-08-19 13:31:15 +03:00
|
|
|
from routes.chat_routes import chat_router
|
2023-08-21 13:45:32 +03:00
|
|
|
from routes.misc_routes import misc_router
|
2023-08-18 11:18:29 +03:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
sentry_dsn = os.getenv("SENTRY_DSN")
|
|
|
|
if sentry_dsn:
|
|
|
|
sentry_sdk.init(
|
|
|
|
dsn=sentry_dsn,
|
|
|
|
traces_sample_rate=1.0,
|
|
|
|
)
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
add_cors_middleware(app)
|
|
|
|
|
|
|
|
app.include_router(chat_router)
|
|
|
|
app.include_router(misc_router)
|
|
|
|
|
|
|
|
|
|
|
|
@app.exception_handler(HTTPException)
|
|
|
|
async def http_exception_handler(_, exc):
|
|
|
|
return JSONResponse(
|
|
|
|
status_code=exc.status_code,
|
|
|
|
content={"detail": exc.detail},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# log more details about validation errors (422)
|
|
|
|
def handle_request_validation_error(app: FastAPI):
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
|
|
async def validation_exception_handler(
|
2023-08-21 13:45:32 +03:00
|
|
|
request: Request, exc: RequestValidationError
|
2023-08-18 11:18:29 +03:00
|
|
|
):
|
|
|
|
exc_str = f"{exc}".replace("\n", " ").replace(" ", " ")
|
|
|
|
logger.error(request, exc_str)
|
|
|
|
content = {
|
|
|
|
"status_code": status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
|
|
"message": exc_str,
|
|
|
|
"data": None,
|
|
|
|
}
|
|
|
|
return JSONResponse(
|
|
|
|
content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
handle_request_validation_error(app)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# run main.py to debug backend
|
|
|
|
import uvicorn
|
2023-08-19 13:31:15 +03:00
|
|
|
|
2023-08-21 13:25:16 +03:00
|
|
|
uvicorn.run(app, host="0.0.0.0", port=5050)
|