From ba4fbd327248db4d443e7591f7b3eb9065bc1ec1 Mon Sep 17 00:00:00 2001 From: mini Date: Sun, 20 Jul 2025 20:33:50 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20main.py=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=ED=97=AC=EC=8A=A4=EC=B2=B4=ED=81=AC=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/health.py | 9 +++++++++ app/main.py | 10 +++++----- 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 app/api/health.py diff --git a/app/api/health.py b/app/api/health.py new file mode 100644 index 0000000..f88e530 --- /dev/null +++ b/app/api/health.py @@ -0,0 +1,9 @@ +# app/api/health.py +from fastapi import APIRouter + +router = APIRouter() + + +@router.get("/health") +async def health_check(): + return {"status": "ok", "message": "Service is healthy"} diff --git a/app/main.py b/app/main.py index ea2ecbe..ac7a05a 100644 --- a/app/main.py +++ b/app/main.py @@ -5,19 +5,19 @@ import uvicorn from fastapi import FastAPI +from app.api import health # 헬스체크 + app = FastAPI() +# 헬스 체크 라우터 +app.include_router(health.router) + @app.get("/") async def read_root(): return {"message": "Hello, FastAPI Backend!"} -@app.get("/health") -async def health_check(): - return {"status": "ok", "message": "Service is healthy"} - - # 이 부분이 추가된 동적 포트 할당 로직입니다. if __name__ == "__main__": # 1. 환경 변수 'PORT'가 있으면 해당 포트를 사용합니다. From 8d1fad294abaf5327a7cd1ea7b1157169c9f7e43 Mon Sep 17 00:00:00 2001 From: mini Date: Sun, 20 Jul 2025 20:45:13 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20main.py=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EB=8F=99=EC=A0=81=20=ED=8F=AC=ED=8A=B8=20=ED=95=A0=EB=8B=B9=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/port.py | 23 +++++++++++++++++++++++ app/main.py | 23 ++++------------------- 2 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 app/core/port.py diff --git a/app/core/port.py b/app/core/port.py new file mode 100644 index 0000000..8305493 --- /dev/null +++ b/app/core/port.py @@ -0,0 +1,23 @@ +# app/core/port.py + +import os +import socket + + +def get_available_port(default: int = 8000) -> int: + """ + 환경변수 'PORT'가 존재하면 해당 포트를 사용하고, + 없다면 시스템이 할당한 사용 가능한 포트를 반환합니다. + """ + port_from_env = os.getenv("PORT") + + if port_from_env: + print(f"Using port from environment variable: {port_from_env}") + return int(port_from_env) + + # 포트 0 바인딩 → 시스템이 사용 가능한 포트 할당 + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind(("0.0.0.0", 0)) + assigned_port = s.getsockname()[1] + print(f"Dynamically assigned port: {assigned_port}") + return assigned_port diff --git a/app/main.py b/app/main.py index ac7a05a..66a4d9f 100644 --- a/app/main.py +++ b/app/main.py @@ -1,11 +1,10 @@ # main.py -import os -import socket # 소켓 모듈 임포트 import uvicorn from fastapi import FastAPI -from app.api import health # 헬스체크 +from app.api import health # 헬스 체크 +from app.core.port import get_available_port # 동적 포트 할당 app = FastAPI() @@ -18,22 +17,8 @@ async def read_root(): return {"message": "Hello, FastAPI Backend!"} -# 이 부분이 추가된 동적 포트 할당 로직입니다. if __name__ == "__main__": - # 1. 환경 변수 'PORT'가 있으면 해당 포트를 사용합니다. - # 2. 없으면 사용 가능한 임시 포트를 찾습니다. - port_from_env = os.getenv("PORT") - - if port_from_env: - port = int(port_from_env) - print(f"Using port from environment variable: {port}") - else: - # 시스템에서 사용 가능한 임시 포트를 찾습니다. - # 포트 0을 바인딩하면 운영체제가 사용 가능한 포트를 할당해 줍니다. - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - s.bind(("0.0.0.0", 0)) # 0.0.0.0에 포트 0을 바인딩 - port = s.getsockname()[1] # 할당된 포트 번호 가져오기 - print(f"Dynamically assigned port: {port}") - + # 동적 할당 로직 + port = get_available_port() # Uvicorn 서버를 시작합니다. uvicorn.run(app, host="0.0.0.0", port=port)