File size: 2,062 Bytes
2636575
 
 
d5684b3
5753020
2636575
 
 
d5684b3
2636575
 
d5684b3
 
2636575
 
5753020
 
d5684b3
 
 
 
5753020
 
 
 
 
 
 
 
d5684b3
5753020
 
 
 
d5684b3
 
 
 
 
5753020
 
d5684b3
 
5753020
 
d5684b3
 
5753020
 
 
 
 
 
 
 
 
 
 
d5684b3
5753020
 
 
 
2636575
5753020
2636575
 
5753020
2636575
5753020
2636575
 
 
 
 
5753020
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
import os
import uvicorn
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse
from app.api.routers.chat import chat_router
from app.api.routers.user import user_router
from app.settings import init_settings
from fastapi.staticfiles import StaticFiles
from alembic.config import Config
from alembic import command


from app.engine.postgresdb import postgresdb
from config.index import config

logger = logging.getLogger("uvicorn")


def run_migrations(connection, cfg):
    cfg.attributes["connection"] = connection
    command.upgrade(cfg, "head")


async def run_async_migrations():
    logger.info("Running migrations...")
    async_engine = postgresdb.get_engine()

    async with async_engine.begin() as conn:
        await conn.run_sync(run_migrations, Config("alembic.ini"))

    logger.info("Finished running migrations")


@asynccontextmanager
async def lifespan(app_: FastAPI):
    yield
    if postgresdb.get_engine() is not None:
        await postgresdb.close()


init_settings()
# init_observability()


def init_app():
    app = FastAPI(lifespan=lifespan)
    if config.ENVIRONMENT == "dev":
        logger.warning("Running in development mode - allowing CORS for all origins")
        app.add_middleware(
            CORSMiddleware,
            allow_origins=["*"],
            allow_credentials=True,
            allow_methods=["*"],
            allow_headers=["*"],
        )

    if os.path.exists("data"):
        app.mount("/api/data", StaticFiles(directory="data"), name="static")
    app.include_router(chat_router, prefix="/api/chat")
    app.include_router(user_router)

    return app


app = init_app()

if __name__ == "__main__":
    # Redirect to documentation page when accessing base URL
    @app.get("/")
    async def redirect_to_docs():
        return RedirectResponse(url="/docs")

    uvicorn.run(app="main:app", host=config.APP_HOST, port=config.APP_PORT, reload=(config.ENVIRONMENT == "dev"))