-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.py
62 lines (49 loc) · 1.56 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
__all__ = [
'BASE_DIR',
'Config',
'ORM_LINK_CONF',
'ORM_MIGRATE_CONF',
'ORM_TEST_MIGRATE_CONF',
'LogConfig',
'ServiceConfig',
'AuthenticConfig',
'RedisConfig'
]
import os
from pathlib import Path
from functools import lru_cache
import pytomlpp
from conf.const import EnvConst
from conf.settings import (
Setting, ORMSetting
)
BASE_DIR = Path(__file__).absolute().parent
@lru_cache()
def get_settings() -> Setting:
code_env = os.environ.get('CODE_ENV', EnvConst.LOCAL)
if code_env == EnvConst.TEST:
p = Path(BASE_DIR).joinpath('conf/test.local.toml')
if not p.is_file():
p = Path(BASE_DIR).joinpath('conf/test.toml')
elif code_env == EnvConst.DEV:
p = Path(BASE_DIR).joinpath('conf/test.local.toml')
if not p.is_file():
p = Path(BASE_DIR).joinpath('conf/test.toml')
elif code_env == EnvConst.PRD:
p = Path(BASE_DIR).joinpath('conf/product.local.toml')
if not p.is_file():
p = Path(BASE_DIR).joinpath('conf/product.toml')
else:
p = Path(BASE_DIR).joinpath('conf/local.toml')
if not p.is_file():
raise Exception('config no exists')
settings = Setting.model_validate(pytomlpp.load(p))
return settings
Config = get_settings()
ORM_LINK_CONF = ORMSetting(Config.db).orm_link_conf
ORM_MIGRATE_CONF = ORMSetting(Config.db).orm_migrate_conf
ORM_TEST_MIGRATE_CONF = ORMSetting(Config.db).orm_test_migrate_conf
LogConfig = Config.log
ServiceConfig = Config.service
AuthenticConfig = Config.authentic
RedisConfig = Config.redis