forked from anitab-org/bridge-in-tech-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
129 lines (101 loc) · 3.84 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
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):
DEBUG = False
TESTING = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
# Flask JWT settings
JWT_ACCESS_TOKEN_EXPIRES = timedelta(weeks=1)
JWT_REFRESH_TOKEN_EXPIRES = timedelta(weeks=4)
PROPAGATE_EXCEPTION = True
# Security
SECRET_KEY = os.getenv("SECRET_KEY", None)
BCRYPT_LOG_ROUNDS = 13
WTF_CSRF_ENABLED = True
# mail settings
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")
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")
DB_TEST_NAME = os.getenv("DB_TEST_NAME")
@staticmethod
def build_db_uri(
db_type_arg = os.getenv("DB_TYPE"),
db_user_arg = os.getenv("DB_USERNAME"),
db_password_arg = os.getenv("DB_PASSWORD"),
db_endpoint_arg = os.getenv("DB_ENDPOINT"),
db_name_arg = os.getenv("DB_NAME"),
):
return f"{db_type_arg}://{db_user_arg}:{db_password_arg}@{db_endpoint_arg}/{db_name_arg}"
@staticmethod
def build_db_test_uri(
db_type_arg = os.getenv("DB_TYPE"),
db_user_arg = os.getenv("DB_USERNAME"),
db_password_arg = os.getenv("DB_PASSWORD"),
db_endpoint_arg = os.getenv("DB_ENDPOINT"),
db_name_arg = os.getenv("DB_TEST_NAME"),
):
return f"{db_type_arg}://{db_user_arg}:{db_password_arg}@{db_endpoint_arg}/{db_name_arg}"
class LocalConfig(BaseConfig):
"""Local configuration."""
DEBUG = True
# Using a local postgre database
SQLALCHEMY_DATABASE_URI = "postgresql:///bit_schema"
# SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()
class DevelopmentConfig(BaseConfig):
DEBUG = True
SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()
class TestingConfig(BaseConfig):
TESTING = True
MOCK_EMAIL = True
# Using a local postgre database
SQLALCHEMY_DATABASE_URI = "postgresql:///bit_schema_test"
# SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_test_uri()
class StagingConfig(BaseConfig):
"""Staging configuration."""
DEBUG = True
SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()
class ProductionConfig(BaseConfig):
SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()
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",
}