Spaces:
Sleeping
Sleeping
File size: 2,056 Bytes
2636575 d5684b3 f92858b 2636575 d5684b3 81ec8d6 a4ef0c7 2636575 d5684b3 2636575 f92858b c553fd3 d5684b3 c553fd3 d5684b3 c553fd3 d5684b3 c553fd3 d5684b3 c553fd3 d5684b3 f92858b c553fd3 f92858b d5684b3 f92858b 81ec8d6 a4ef0c7 2636575 f92858b 2636575 f92858b 2636575 a4ef0c7 c553fd3 |
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 |
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.api.routers.transaction import transaction_router
from app.api.routers.income_statement import income_statement_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 as env
logger = logging.getLogger("uvicorn")
init_settings()
# init_observability()
def init_app(init_db: bool = True) -> FastAPI:
lifespan = None
if init_db:
postgresdb.init(env.SQLALCHEMY_DATABASE_URL, {"echo": True, "future": True})
@asynccontextmanager
async def lifespan(app: FastAPI):
yield
if postgresdb.get_engine() is not None:
await postgresdb.close()
app = FastAPI(lifespan=lifespan)
if env.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)
app.include_router(transaction_router)
app.include_router(income_statement_router)
return app
app = init_app()
# Redirect to documentation page when accessing base URL
@app.get("/")
async def redirect_to_docs():
return RedirectResponse(url="/docs")
if __name__ == "__main__":
uvicorn.run(app="main:app", host=env.APP_HOST, port=env.APP_PORT, reload=(env.ENVIRONMENT == "dev"))
|