Skip to content

Commit

Permalink
설정로드 추가 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
choisungwook committed Apr 27, 2022
1 parent 2fac9d0 commit bec3f51
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
9 changes: 9 additions & 0 deletions app/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ENV_STATE="dev"

DEV_POSTGRESQL_HOST="127.0.0.1"
DEV_POSTGRESQL_USER="root"
DEV_POSTGRESQL_PASSWORD="pass"

PROD_POSTGRESQL_HOST="127.0.0.1"
PROD_POSTGRESQL_USER="root"
PROD_POSTGRESQL_PASSWORD="pass"
41 changes: 41 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import Optional
from pydantic import BaseSettings, Field


class GlobalConfig(BaseSettings):
"""공통 설정"""
ENV_STATE: Optional[str] = Field(None, env="ENV_STATE")

POSTGRESQL_HOST: Optional[str] = None
POSTGRESQL_USER: Optional[str] = None
POSTGRESQL_PASSWORD: Optional[str] = None

class Config:
"""BaseSettings 설정"""
env_file: str = ".env"

class DevConfig(GlobalConfig):
"""개발 설정"""
class Config:
"""BaseSettings 설정"""
env_prefix: str = "DEV_"

class ProdConfig(GlobalConfig):
"""운영 설정"""
class Config:
"""BaseSettings 설정"""
env_prefix: str = "PROD_"

class FactoryConfig:
"""설정 로드"""
def __init__(self, env_state: Optional[str]):
self.env_state = env_state

def __call__(self):
if self.env_state == "dev":
return DevConfig()

elif self.env_state == "prod":
return ProdConfig()

cnf = FactoryConfig(GlobalConfig().ENV_STATE)()
9 changes: 4 additions & 5 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import Optional
from fastapi import FastAPI
from config import cnf

app = FastAPI()

@app.get("/")
def read_root():
print(cnf.POSTGRESQL_HOST)
print(cnf.POSTGRESQL_USER)
print(cnf.POSTGRESQL_PASSWORD)
return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
return {"item_id": item_id, "q": q}

0 comments on commit bec3f51

Please sign in to comment.