-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
64 lines (56 loc) · 1.86 KB
/
main.py
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
from fastapi import FastAPI
from fastapi.routing import APIRoute
from starlette.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware
from app.models.init import (
User,
Heritage,
ChatSession,
ChatMessage,
HeritageBuilding,
HeritageBuildingImage,
HeritageRoute,
HeritageRouteBuilding,
HeritageType
)
from app.core.database import Base, engine
from app.core.config import settings
from app.router.api import api_router
from contextlib import asynccontextmanager
def custom_generate_unique_id(route: APIRoute) -> str:
if route.tags:
return f"{route.tags[0]}-{route.name}"
return route.name
@asynccontextmanager
async def app_lifespan(app: FastAPI):
# 애플리케이션 시작 시 실행될 로직
async with engine.begin() as conn:
# 모든 테이블 삭제
# await conn.run_sync(Base.metadata.drop_all)
# 모든 테이블 다시 생성
await conn.run_sync(Base.metadata.create_all)
yield
# 애플리케이션 종료 시 실행될 로직 (필요한 경우)
app = FastAPI(
lifespan= app_lifespan,
# 배포 시 swagger UI, Redoc 비활성화
# docs_url= None,
# redoc_url= None,
title = settings.PROJECT_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json",
generate_unique_id_function=custom_generate_unique_id
)
app.add_middleware(SessionMiddleware, secret_key=settings.BACKEND_SESSION_SECRET_KEY)
# Base.metadata.create_all(bind=engine)
# Set All CORS enabled origins
if settings.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=[
str(origin).strip("/") for origin in settings.BACKEND_CORS_ORIGINS
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
app.include_router(api_router, prefix=settings.API_V1_STR)