2023-05-21 02:20:55 +03:00
|
|
|
import os
|
2023-08-25 13:03:13 +03:00
|
|
|
|
2023-08-08 18:01:31 +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-08 18:01:31 +03:00
|
|
|
load_dotenv()
|
2023-07-01 22:12:13 +03:00
|
|
|
import pypandoc
|
|
|
|
import sentry_sdk
|
2023-08-01 10:24:57 +03:00
|
|
|
from fastapi import FastAPI, HTTPException, Request, status
|
|
|
|
from fastapi.exceptions import RequestValidationError
|
2023-06-22 18:50:06 +03:00
|
|
|
from fastapi.responses import JSONResponse
|
2023-05-30 14:02:48 +03:00
|
|
|
from logger import get_logger
|
2023-06-04 00:12:42 +03:00
|
|
|
from middlewares.cors import add_cors_middleware
|
2023-06-20 17:17:13 +03:00
|
|
|
from routes.api_key_routes import api_key_router
|
|
|
|
from routes.brain_routes import brain_router
|
2023-06-11 00:59:16 +03:00
|
|
|
from routes.chat_routes import chat_router
|
|
|
|
from routes.crawl_routes import crawl_router
|
|
|
|
from routes.explore_routes import explore_router
|
2023-09-20 10:35:37 +03:00
|
|
|
from routes.knowledge_routes import knowledge_router
|
2023-06-11 00:59:16 +03:00
|
|
|
from routes.misc_routes import misc_router
|
2023-09-12 19:00:46 +03:00
|
|
|
from routes.notification_routes import notification_router
|
2023-08-03 10:53:38 +03:00
|
|
|
from routes.prompt_routes import prompt_router
|
2023-07-11 19:20:31 +03:00
|
|
|
from routes.subscription_routes import subscription_router
|
2023-06-11 00:59:16 +03:00
|
|
|
from routes.upload_routes import upload_router
|
|
|
|
from routes.user_routes import user_router
|
2023-05-31 14:51:23 +03:00
|
|
|
|
2023-05-22 09:39:55 +03:00
|
|
|
logger = get_logger(__name__)
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-07-14 22:02:26 +03:00
|
|
|
sentry_dsn = os.getenv("SENTRY_DSN")
|
|
|
|
if sentry_dsn:
|
2023-07-01 22:12:13 +03:00
|
|
|
sentry_sdk.init(
|
2023-07-14 22:02:26 +03:00
|
|
|
dsn=sentry_dsn,
|
2023-07-01 22:12:13 +03:00
|
|
|
traces_sample_rate=1.0,
|
|
|
|
)
|
|
|
|
|
2023-05-21 02:20:55 +03:00
|
|
|
app = FastAPI()
|
|
|
|
|
2023-06-04 00:12:42 +03:00
|
|
|
add_cors_middleware(app)
|
2023-06-20 22:53:04 +03:00
|
|
|
|
2023-08-25 13:03:13 +03:00
|
|
|
|
2023-05-21 09:15:31 +03:00
|
|
|
@app.on_event("startup")
|
|
|
|
async def startup_event():
|
2023-07-14 22:02:26 +03:00
|
|
|
if not os.path.exists(pypandoc.get_pandoc_path()):
|
|
|
|
pypandoc.download_pandoc()
|
2023-05-21 09:15:31 +03:00
|
|
|
|
2023-08-18 15:49:48 +03:00
|
|
|
|
2023-06-20 17:17:13 +03:00
|
|
|
app.include_router(brain_router)
|
2023-06-11 00:59:16 +03:00
|
|
|
app.include_router(chat_router)
|
|
|
|
app.include_router(crawl_router)
|
|
|
|
app.include_router(explore_router)
|
|
|
|
app.include_router(misc_router)
|
|
|
|
app.include_router(upload_router)
|
|
|
|
app.include_router(user_router)
|
2023-06-14 22:21:13 +03:00
|
|
|
app.include_router(api_key_router)
|
2023-07-11 19:20:31 +03:00
|
|
|
app.include_router(subscription_router)
|
2023-08-03 10:53:38 +03:00
|
|
|
app.include_router(prompt_router)
|
2023-09-12 19:00:46 +03:00
|
|
|
app.include_router(notification_router)
|
2023-09-20 10:35:37 +03:00
|
|
|
app.include_router(knowledge_router)
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-07-14 22:02:26 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
@app.exception_handler(HTTPException)
|
2023-06-23 11:36:55 +03:00
|
|
|
async def http_exception_handler(_, exc):
|
2023-06-22 18:50:06 +03:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=exc.status_code,
|
|
|
|
content={"detail": exc.detail},
|
|
|
|
)
|
2023-08-01 10:24:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
# log more details about validation errors (422)
|
|
|
|
def handle_request_validation_error(app: FastAPI):
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
|
|
async def validation_exception_handler(
|
|
|
|
request: Request, exc: RequestValidationError
|
|
|
|
):
|
|
|
|
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)
|
2023-08-08 18:01:31 +03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# run main.py to debug backend
|
|
|
|
import uvicorn
|
|
|
|
|
2023-08-25 13:03:13 +03:00
|
|
|
uvicorn.run(app, host="0.0.0.0", port=5050)
|