-
Notifications
You must be signed in to change notification settings - Fork 449
/
config.py
154 lines (113 loc) · 4.08 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
from datetime import timedelta
def get_mock_email_config() -> bool:
MOCK_EMAIL = os.getenv("MOCK_EMAIL")
# if MOCK_EMAIL env variable is set
if MOCK_EMAIL:
# MOCK_EMAIL is case insensitive
MOCK_EMAIL = MOCK_EMAIL.lower()
if MOCK_EMAIL == "true":
return True
elif MOCK_EMAIL == "false":
return False
else:
# if MOCK_EMAIL env variable is set a wrong value
raise ValueError(
"MOCK_EMAIL environment variable is optional if set, it has to be valued as either 'True' or 'False'"
)
else:
# Default behaviour is to send the email if MOCK_EMAIL is not set
return False
class BaseConfig(object):
"""Base configuration."""
DEBUG = False
TESTING = False
# SQLAlchemy settings
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_POOL_RECYCLE = 3600
# Example:
# MySQL: mysql+pymysql://{db_user}:{db_password}@{db_endpoint}/{db_name}
# SQLite: sqlite:///local_data.db
DB_TYPE = os.getenv("DB_TYPE")
DB_USERNAME = os.getenv("DB_USERNAME")
DB_PASSWORD = os.getenv("DB_PASSWORD")
DB_ENDPOINT = os.getenv("DB_ENDPOINT")
DB_NAME = os.getenv("DB_NAME")
UNVERIFIED_USER_THRESHOLD = 2592000 # 30 days
# Flask JWT settings
JWT_ACCESS_TOKEN_EXPIRES = timedelta(minutes=10)
JWT_REFRESH_TOKEN_EXPIRES = timedelta(weeks=4)
# Security
SECRET_KEY = os.getenv("SECRET_KEY", None)
# if not SECRET_KEY:
# raise ValueError('You need to export SECRET_KEY set for Flask application')
SECURITY_PASSWORD_SALT = os.getenv("SECURITY_PASSWORD_SALT")
BCRYPT_LOG_ROUNDS = 13
WTF_CSRF_ENABLED = True
DEBUG_TB_ENABLED = False
DEBUG_TB_INTERCEPT_REDIRECTS = False
# mail settings
MOCK_EMAIL = get_mock_email_config()
MAIL_SERVER = os.getenv("MAIL_SERVER")
MAIL_PORT = 465
MAIL_USE_TLS = False
MAIL_USE_SSL = True
# email authentication
MAIL_USERNAME = os.getenv("APP_MAIL_USERNAME")
MAIL_PASSWORD = os.getenv("APP_MAIL_PASSWORD")
# mail accounts
MAIL_DEFAULT_SENDER = os.getenv("MAIL_DEFAULT_SENDER")
@staticmethod
def build_db_uri(
db_type_arg=DB_TYPE,
db_user_arg=DB_USERNAME,
db_password_arg=DB_PASSWORD,
db_endpoint_arg=DB_ENDPOINT,
db_name_arg=DB_NAME,
):
"""Build remote database uri using specific environment variables."""
return "{db_type}://{db_user}:{db_password}@{db_endpoint}/{db_name}".format(
db_type=db_type_arg,
db_user=db_user_arg,
db_password=db_password_arg,
db_endpoint=db_endpoint_arg,
db_name=db_name_arg,
)
class ProductionConfig(BaseConfig):
"""Production configuration."""
SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()
MOCK_EMAIL = False
class DevelopmentConfig(BaseConfig):
"""Development configuration."""
DEBUG = True
SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()
class StagingConfig(BaseConfig):
"""Staging configuration."""
DEBUG = True
SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()
MOCK_EMAIL = False
class LocalConfig(BaseConfig):
"""Local configuration."""
DEBUG = True
# Using a local sqlite database
SQLALCHEMY_DATABASE_URI = "sqlite:///local_data.db"
class TestingConfig(BaseConfig):
"""Testing configuration."""
TESTING = True
MOCK_EMAIL = True
# Use in-memory SQLite database for testing
SQLALCHEMY_DATABASE_URI = "sqlite://"
def get_env_config() -> str:
flask_config_name = os.getenv("FLASK_ENVIRONMENT_CONFIG", "dev")
if flask_config_name not in ["prod", "test", "dev", "local", "stag"]:
raise ValueError(
"The environment config value has to be within these values: prod, dev, test, local, stag."
)
return CONFIGURATION_MAPPER[flask_config_name]
CONFIGURATION_MAPPER = {
"dev": "config.DevelopmentConfig",
"prod": "config.ProductionConfig",
"stag": "config.StagingConfig",
"local": "config.LocalConfig",
"test": "config.TestingConfig",
}