File tree 3 files changed +54
-5
lines changed
3 files changed +54
-5
lines changed Original file line number Diff line number Diff line change
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"
Original file line number Diff line number Diff line change
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 )()
Original file line number Diff line number Diff line change 1
- from typing import Optional
2
1
from fastapi import FastAPI
2
+ from config import cnf
3
3
4
4
app = FastAPI ()
5
5
6
6
@app .get ("/" )
7
7
def read_root ():
8
+ print (cnf .POSTGRESQL_HOST )
9
+ print (cnf .POSTGRESQL_USER )
10
+ print (cnf .POSTGRESQL_PASSWORD )
8
11
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 }
You can’t perform that action at this time.
0 commit comments