2023-05-21 02:20:55 +03:00
|
|
|
import os
|
2023-12-02 16:50:57 +03:00
|
|
|
from fastapi import FastAPI
|
2023-08-25 13:03:13 +03:00
|
|
|
|
2023-10-30 12:18:04 +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 sentry_sdk
|
2023-10-30 12:18:04 +03:00
|
|
|
from fastapi import FastAPI, HTTPException
|
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-12-01 00:29:28 +03:00
|
|
|
from modules.api_key.controller import api_key_router
|
|
|
|
from modules.contact_support.controller import contact_router
|
|
|
|
from modules.knowledge.controller import knowledge_router
|
|
|
|
from modules.misc.controller import misc_router
|
|
|
|
from modules.notification.controller import notification_router
|
|
|
|
from modules.onboarding.controller import onboarding_router
|
|
|
|
from modules.prompt.controller import prompt_router
|
|
|
|
from modules.upload.controller import upload_router
|
|
|
|
from modules.user.controller import user_router
|
2023-06-20 17:17:13 +03:00
|
|
|
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
|
2023-07-11 19:20:31 +03:00
|
|
|
from routes.subscription_routes import subscription_router
|
2023-12-02 16:50:57 +03:00
|
|
|
from logger import get_logger
|
|
|
|
from packages.utils import handle_request_validation_error
|
2023-11-27 12:08:00 +03:00
|
|
|
from sentry_sdk.integrations.fastapi import FastApiIntegration
|
2023-11-28 16:27:39 +03:00
|
|
|
from sentry_sdk.integrations.starlette import StarletteIntegration
|
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-10-30 12:18:04 +03:00
|
|
|
if os.getenv("DEV_MODE") == "true":
|
2023-10-09 16:23:13 +03:00
|
|
|
import debugpy
|
2023-10-30 12:18:04 +03:00
|
|
|
|
2023-10-09 16:23:13 +03:00
|
|
|
logger.debug("👨💻 Running in dev mode")
|
|
|
|
debugpy.listen(("0.0.0.0", 5678))
|
|
|
|
|
|
|
|
|
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-11-28 16:27:39 +03:00
|
|
|
sample_rate=0.1,
|
|
|
|
enable_tracing=True,
|
|
|
|
integrations=[
|
|
|
|
StarletteIntegration(transaction_style="endpoint"),
|
|
|
|
FastApiIntegration(transaction_style="endpoint"),
|
|
|
|
],
|
2023-07-01 22:12:13 +03:00
|
|
|
)
|
|
|
|
|
2023-12-03 03:35:01 +03:00
|
|
|
# if CREATE_FIRST_USER := os.getenv("CREATE_FIRST_USER", "False").lower() == "true":
|
|
|
|
# try:
|
|
|
|
# from supabase import create_client
|
2023-12-02 16:50:57 +03:00
|
|
|
|
2023-12-03 03:35:01 +03:00
|
|
|
# supabase_client_auth = create_client(
|
|
|
|
# os.getenv("SUPABASE_URL"), os.getenv("SUPABASE_SERVICE_KEY")
|
|
|
|
# )
|
|
|
|
# res = supabase_client_auth.from_('users').select('*').eq('email', "admin@quivr.app").execute()
|
|
|
|
# if len(res.data) == 0:
|
|
|
|
# supabase_client_auth.auth.admin.create_user({"email": "admin@quivr.app","email_confirm": True, "password": "admin"})
|
|
|
|
# logger.info("👨💻 Created first user")
|
|
|
|
# else:
|
|
|
|
# logger.info("👨💻 First user already exists")
|
|
|
|
# except Exception as e:
|
|
|
|
# logger.error("👨💻 Error while creating first user")
|
|
|
|
# logger.error(e)
|
2023-12-02 16:50:57 +03:00
|
|
|
|
|
|
|
|
2023-12-03 03:35:01 +03:00
|
|
|
# telemetry_disabled = os.getenv("TELEMETRY_DISABLED", "False").lower() == "true"
|
|
|
|
# if not telemetry_disabled:
|
|
|
|
# try:
|
|
|
|
# logger.info("👨💻 You can disable TELEMETRY by addind TELEMETRY_DISABLED=True to your env variables")
|
|
|
|
# logger.info("Telemetry is used to measure the usage of the app. No personal data is collected.")
|
|
|
|
# import os
|
|
|
|
# from supabase import create_client
|
|
|
|
# import uuid
|
|
|
|
# supabase_url = os.environ.get("SUPABASE_URL", "NOT_SET")
|
|
|
|
# supabase_client_telemetry = create_client("https://phcwncasycjransxnmbf.supabase.co","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBoY3duY2FzeWNqcmFuc3hubWJmIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDE0NDM5NDEsImV4cCI6MjAxNzAxOTk0MX0.0MDz2ETHdQve9yVy_YI79iGsrlpLXX1ObrjmnzyVKSo")
|
|
|
|
# ## insert in the usage table id as uuid of supabase_url
|
|
|
|
# uuid_from_string = uuid.uuid5(uuid.NAMESPACE_DNS, supabase_url)
|
|
|
|
# supabase_client_telemetry.table("usage").insert({"id": str(uuid_from_string)}).execute()
|
|
|
|
# except Exception as e:
|
|
|
|
# logger.error("Error while sending telemetry")
|
2023-12-01 19:02:46 +03:00
|
|
|
|
|
|
|
|
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-11-18 21:23:56 +03:00
|
|
|
# @app.on_event("startup")
|
|
|
|
# async def startup_event():
|
|
|
|
# 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)
|
2023-10-05 12:31:26 +03:00
|
|
|
app.include_router(onboarding_router)
|
2023-06-11 00:59:16 +03:00
|
|
|
app.include_router(misc_router)
|
2023-10-05 12:31:26 +03:00
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
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-10-23 18:58:45 +03:00
|
|
|
app.include_router(contact_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
|
|
|
|
|
|
|
|
|
|
|
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)
|