Skip to content

Commit bec3f51

Browse files
committed
설정로드 추가 #1
1 parent 2fac9d0 commit bec3f51

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

app/.env

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ENV_STATE="dev"
2+
3+
DEV_POSTGRESQL_HOST="127.0.0.1"
4+
DEV_POSTGRESQL_USER="root"
5+
DEV_POSTGRESQL_PASSWORD="pass"
6+
7+
PROD_POSTGRESQL_HOST="127.0.0.1"
8+
PROD_POSTGRESQL_USER="root"
9+
PROD_POSTGRESQL_PASSWORD="pass"

app/config.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import Optional
2+
from pydantic import BaseSettings, Field
3+
4+
5+
class GlobalConfig(BaseSettings):
6+
"""공통 설정"""
7+
ENV_STATE: Optional[str] = Field(None, env="ENV_STATE")
8+
9+
POSTGRESQL_HOST: Optional[str] = None
10+
POSTGRESQL_USER: Optional[str] = None
11+
POSTGRESQL_PASSWORD: Optional[str] = None
12+
13+
class Config:
14+
"""BaseSettings 설정"""
15+
env_file: str = ".env"
16+
17+
class DevConfig(GlobalConfig):
18+
"""개발 설정"""
19+
class Config:
20+
"""BaseSettings 설정"""
21+
env_prefix: str = "DEV_"
22+
23+
class ProdConfig(GlobalConfig):
24+
"""운영 설정"""
25+
class Config:
26+
"""BaseSettings 설정"""
27+
env_prefix: str = "PROD_"
28+
29+
class FactoryConfig:
30+
"""설정 로드"""
31+
def __init__(self, env_state: Optional[str]):
32+
self.env_state = env_state
33+
34+
def __call__(self):
35+
if self.env_state == "dev":
36+
return DevConfig()
37+
38+
elif self.env_state == "prod":
39+
return ProdConfig()
40+
41+
cnf = FactoryConfig(GlobalConfig().ENV_STATE)()

app/main.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
from typing import Optional
21
from fastapi import FastAPI
2+
from config import cnf
33

44
app = FastAPI()
55

66
@app.get("/")
77
def read_root():
8+
print(cnf.POSTGRESQL_HOST)
9+
print(cnf.POSTGRESQL_USER)
10+
print(cnf.POSTGRESQL_PASSWORD)
811
return {"Hello": "World"}
9-
10-
@app.get("/items/{item_id}")
11-
def read_item(item_id: int, q: Optional[str] = None):
12-
return {"item_id": item_id, "q": q}

0 commit comments

Comments
 (0)