This repository has been archived by the owner on Nov 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
95 lines (76 loc) · 2.45 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# coding=utf-8
import logging
import os
from dotenv import load_dotenv
_DOT_ENV_PATH = os.path.join(os.path.dirname(__file__), '.env')
load_dotenv(_DOT_ENV_PATH)
__author__ = 'Kien' + ''
_logger = logging.getLogger(__name__)
ROOT_DIR = os.path.abspath(os.path.join(
os.path.dirname(__file__)
))
# The environment to run this config. This value will affect to the
# configuration loading
#
# it can be: dev, test, stag, prod
ENV_MODE = os.environ.get('ENV_MODE', '').upper()
DEBUG = True
TESTING = False
LOGGING_CONFIG_FILE = os.path.join(ROOT_DIR, 'etc', 'logging.ini')
FLASK_APP_SECRET_KEY = os.getenv('SECRET_KEY', 'my_precious_secret_key')
MYSQL_DATABASE = os.getenv('MYSQL_DATABASE')
MYSQL_HOST = os.getenv('MYSQL_HOST')
MYSQL_USER = os.getenv('MYSQL_USER')
MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD')
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://{0}:{1}@{2}:{3}/{4}'.format(
MYSQL_USER, MYSQL_PASSWORD, MYSQL_HOST, 3306, MYSQL_DATABASE
)
SQLALCHEMY_TRACK_MODIFICATIONS = True
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
MAIL_SERVER = os.getenv('MAIL_SERVER')
MAIL_PORT = os.getenv('MAIL_PORT')
MAIL_USE_SSL = os.getenv('MAIL_USE_SSL')
MAIL_USERNAME = os.getenv('MAIL_USERNAME')
MAIL_PASSWORD = os.getenv('MAIL_PASSWORD')
MAIL_DEFAULT_SENDER = os.getenv('MAIL_DEFAULT_SENDER')
def _env(name, default):
""" Get configuration from environment in priorities:
1. the env var with prefix of $ENV_MODE
2. the env var with the same name (in upper case)
3. the default value
:param str name: configuration name
:param default: default value
"""
def _bool(val):
if not val:
return False
return val not in ('0', 'false', 'no')
# make sure configuration name is upper case
name = name.upper()
# try to get value from env vars
val = default
for env_var in ('%s_%s' % (ENV_MODE, name), name):
try:
val = os.environ[env_var]
break
except KeyError:
pass
else:
env_var = None
# convert to the right types
if isinstance(default, bool):
val = _bool(val)
return env_var, val
_IGNORED_CONFIG = (
'ROOT_DIR',
'STATIC_DIR',
'ENV_MODE',
)
# rewrite all configuration with environment variables
_vars = list(locals().keys())
for name in _vars:
if name in _IGNORED_CONFIG:
continue
if not name.startswith('_') and name.isupper():
env_var, val = _env(name, locals()[name])
locals()[name] = val