-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fac9d0
commit bec3f51
Showing
3 changed files
with
54 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |