66
77from common .dtos .common_response import CustomJSONResponse
88from configs .api_routers import API_ROUTERS
9+ from configs .database import check_db_connection
910from configs .logging_config import LOGGING_CONFIG
1011from configs .origins import origins
1112from configs .setting import APP_ENV , APP_PORT , REMOTE_HOST
@@ -47,10 +48,15 @@ async def error_logging_middleware(request: Request, call_next):
4748# 커스덤 에러 핸들러 초기화
4849init_exception_handlers (app )
4950
50- if APP_ENV == "local" :
51- allow_origins = ["*" ]
52- else :
53- allow_origins = origins
51+ try :
52+ if APP_ENV == "local" :
53+ allow_origins = ["*" ]
54+ else :
55+ # origins 리스트가 비어있거나 None이 포함되어 있는지 검증
56+ allow_origins = [o for o in origins if o ]
57+ except Exception as e :
58+ print (f"CORS origins loading error: { e } " )
59+ allow_origins = ["*" ] # 실패 시 fallback
5460
5561app .add_middleware (
5662 CORSMiddleware ,
@@ -76,10 +82,9 @@ def read_root():
7682# health check
7783@app .get ("/health" )
7884def health_check () -> Dict [str , str ]:
79- health_status = {"status" : "ok" , "db" : "connected" , "redis" : "connected" }
85+ health_status = {"status" : "ok" , "db" : "connected" }
8086 try :
81- # check_db_connection()
82- # check_redis_connection()
87+ check_db_connection ()
8388 return health_status
8489 except Exception as e :
8590 # 하나라도 실패하면 503 에러 반환
@@ -98,11 +103,11 @@ def health_check() -> Dict[str, str]:
98103
99104 if APP_ENV == "local" :
100105 effective_port = APP_PORT
106+ effective_host = "127.0.0.1"
101107 else :
102- # env_port가 있으면 사용하고, 없으면 APP_PORT를 사용 (둘 다 없으면 8080)
103- effective_port = int (env_port ) if env_port else (APP_PORT or 8080 )
104-
105- effective_host = "127.0.0.1" if APP_ENV == "local" else "0.0.0.0"
108+ # 프로덕션에서는 Cloud Run이 주입하는 PORT를 최우선으로, 없으면 8080
109+ effective_port = int (os .environ .get ("PORT" , 8080 ))
110+ effective_host = "0.0.0.0"
106111
107112 LOGGING_CONFIG ["handlers" ]["default" ]["stream" ] = "ext://sys.stdout"
108113 LOGGING_CONFIG ["handlers" ]["access" ]["stream" ] = "ext://sys.stdout"
0 commit comments