50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
|
|
"""FastAPI application entry point."""
|
||
|
|
|
||
|
|
from contextlib import asynccontextmanager
|
||
|
|
|
||
|
|
from fastapi import FastAPI
|
||
|
|
from fastapi.middleware.cors import CORSMiddleware
|
||
|
|
|
||
|
|
from app.core.config import settings
|
||
|
|
from app.db.base import Base
|
||
|
|
from app.db.session import engine
|
||
|
|
from app.api.v1.router import api_v1_router
|
||
|
|
|
||
|
|
|
||
|
|
@asynccontextmanager
|
||
|
|
async def lifespan(app: FastAPI):
|
||
|
|
"""Application lifespan handler."""
|
||
|
|
# On startup: create tables if they don't exist (for dev convenience)
|
||
|
|
async with engine.begin() as conn:
|
||
|
|
await conn.run_sync(Base.metadata.create_all)
|
||
|
|
yield
|
||
|
|
# On shutdown: dispose engine
|
||
|
|
await engine.dispose()
|
||
|
|
|
||
|
|
|
||
|
|
app = FastAPI(
|
||
|
|
title=settings.APP_NAME,
|
||
|
|
version="0.1.0",
|
||
|
|
docs_url="/docs",
|
||
|
|
redoc_url="/redoc",
|
||
|
|
lifespan=lifespan,
|
||
|
|
)
|
||
|
|
|
||
|
|
# CORS middleware
|
||
|
|
app.add_middleware(
|
||
|
|
CORSMiddleware,
|
||
|
|
allow_origins=settings.cors_origin_list,
|
||
|
|
allow_credentials=True,
|
||
|
|
allow_methods=["*"],
|
||
|
|
allow_headers=["*"],
|
||
|
|
)
|
||
|
|
|
||
|
|
# Include API v1 router
|
||
|
|
app.include_router(api_v1_router, prefix=settings.API_V1_PREFIX)
|
||
|
|
|
||
|
|
|
||
|
|
@app.get("/")
|
||
|
|
async def root():
|
||
|
|
"""Root endpoint."""
|
||
|
|
return {"message": "Rentman Clone API", "version": "0.1.0"}
|