Skip to content

Commit 400d074

Browse files
committed
fix: retry deploy
1 parent db090ac commit 400d074

3 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/configs/database.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@ def get_db_connection():
2222
logger.error(f"데이터베이스 연결 실패: {str(e)}")
2323
raise e
2424

25+
def check_db_connection():
26+
"""
27+
DB 서버의 생존 여부를 확인합니다. (Health Check용)
28+
"""
29+
conn = None
30+
try:
31+
conn = get_db_connection()
32+
with conn.cursor() as cursor:
33+
# 가장 가벼운 쿼리로 실제 응답 확인
34+
cursor.execute("SELECT 1")
35+
result = cursor.fetchone()
36+
if result:
37+
return True
38+
except Exception as e:
39+
logger.error(f"DB Health Check 실패: {str(e)}")
40+
return False
41+
finally:
42+
if conn:
43+
conn.close()
44+
return False
45+
2546
def get_db_cursor():
2647
"""
2748
FastAPI Depends에서 사용할 수 있는 DB 커서 생성기입니다.

src/configs/origins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from src.configs.setting import MAGIC_EYE_PORT, GAME_HUB_PORT, DISCOVEREX_PORT, APP_PORT, SERVER_URL, GAME_HUB_URL, DISCOVEREX_URL, MAGIC_EYE_URL
1+
from src.configs.setting import MAGIC_EYE_PORT, GAME_HUB_PORT, DISCOVEREX_PORT, APP_PORT, GAME_HUB_URL, DISCOVEREX_URL, MAGIC_EYE_URL
22

33
origins = [
44
f"http://localhost:{APP_PORT}",

src/main.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from common.dtos.common_response import CustomJSONResponse
88
from configs.api_routers import API_ROUTERS
9+
from configs.database import check_db_connection
910
from configs.logging_config import LOGGING_CONFIG
1011
from configs.origins import origins
1112
from configs.setting import APP_ENV, APP_PORT, REMOTE_HOST
@@ -47,10 +48,15 @@ async def error_logging_middleware(request: Request, call_next):
4748
# 커스덤 에러 핸들러 초기화
4849
init_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

5561
app.add_middleware(
5662
CORSMiddleware,
@@ -76,10 +82,9 @@ def read_root():
7682
# health check
7783
@app.get("/health")
7884
def 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

Comments
 (0)