-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
config.py
295 lines (218 loc) · 8.33 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""Config management for the Ambuda Flask app.
All of Ambuda's interesting config values are defined in the project's `.env`
file. Here, we parse that file and map its values to specific config objects,
which we then load into Flask.
Most config values are documented on :class:`BaseConfig`, from which all other
classes inherit. Production-specific config values, which are mainly replated
to deployment, are defined on :class:`ProductionConfig`.
It's considered a best practice to keep this module outside the application
package: From the Flask docs (emphasis added):
Configuration becomes more useful if you can store it in a separate file,
*ideally located outside the actual application package*.
"""
import logging
import os
from pathlib import Path
from dotenv import load_dotenv
from flask import Flask
# Load dotenv early so that `_env` will work in the class definitions below.
load_dotenv()
#: The test environment. For unit tests only.
TESTING = "testing"
#: The development environment. For local development.
DEVELOPMENT = "development"
#: The build environment. For build on github.
BUILD = "build"
#: The staging environment. For testing on staging.
STAGING = "staging"
#: The production environment. For production serving.
PRODUCTION = "production"
def _make_path(path: Path):
"""Create a path if it doesn't exist already.
If possible, avoid using this function so that our config code has fewer
side effects. Currently, we use this function only to set up unit tests.
"""
path.mkdir(parents=True, exist_ok=True)
return path
def _env(key: str, default=None) -> str | None:
"""Fetch a value from the local environment.
:param key: the environment variable to fetch
:return: a value, or ``None`` if the variable is undefined.
"""
return os.environ.get(key, default)
class BaseConfig:
"""Base settings for all configs."""
# Core settings
# -------------
#: The Flask app environment ("production", "development", etc.). We set
#: this explicitly so that Celery can have access to it and load an
#: appropriate application context.
AMBUDA_ENVIRONMENT = None
#: Internal secret key for encrypting sensitive data.
SECRET_KEY = _env("SECRET_KEY")
#: URI for the Ambuda database. This URI (also called a URL in some docs)
#: has the following basic format:
#:
#: dialect+driver://username:password@host:port/database
#:
#: For more information, see:
#:
#: https://docs.sqlalchemy.org/en/14/core/engines.html#database-urls
SQLALCHEMY_DATABASE_URI = _env("SQLALCHEMY_DATABASE_URI")
#: Where to store user uploads (PDFs, images, etc.).
UPLOAD_FOLDER = _env("FLASK_UPLOAD_FOLDER")
#: Logger setup
LOG_LEVEL = logging.INFO
# Extensions
# ----------
# Flask-Babel
#: Default locale. This is "en" by default, but declare it here to be
#: explicit.
BABEL_DEFAULT_LOCALE = "en"
# Flask-Mail
#: URL for mail server.
MAIL_SERVER = _env("MAIL_SERVER")
#: Port for mail server.
MAIL_PORT = _env("MAIL_PORT")
#: If ``True``, use TLS for email encryption.
MAIL_USE_TLS = True
#: Username for mail server.
MAIL_USERNAME = _env("MAIL_USERNAME")
#: Password for mail server.
MAIL_PASSWORD = _env("MAIL_PASSWORD")
#: Default sender for site emails.
MAIL_DEFAULT_SENDER = _env("MAIL_DEFAULT_SENDER")
# Flask-WTF
#: If True, enable cross-site request forgery (CSRF) protection.
#: This must be True in production.
WTF_CSRF_ENABLED = True
# Services
# --------
#: ReCAPTCHA public key.
RECAPTCHA_PUBLIC_KEY = _env("RECAPTCHA_PUBLIC_KEY")
#: ReCAPTCHA private key.
RECAPTCHA_PRIVATE_KEY = _env("RECAPTCHA_PRIVATE_KEY")
#: Sentry data source name (DSN).
#: We use Sentry to get notifications about server errors.
SENTRY_DSN = _env("SENTRY_DSN")
# Test-only
# ---------
#: If ``True``, enable the Flask debugger.
DEBUG = False
#: If ``True``, enable testing mode.
TESTING = False
# Environment variables
# ---------------------
# AMBUDA_BOT_PASSWORD is the password we use for the "ambuda-bot" account.
# We set this account as an envvar because we need to create this user as
# part of database seeding.
# GOOGLE_APPLICATION_CREDENTIALS contains credentials for the Google Vision
# API, but these credentials are fetched by the Google API implicitly,
# so we don't need to define it on the Config object here.
class UnitTestConfig(BaseConfig):
"""For unit tests."""
AMBUDA_ENVIRONMENT = TESTING
TESTING = True
SECRET_KEY = "insecure unit test secret"
SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:"
UPLOAD_FOLDER = _make_path(Path(__file__).parent / "data" / "file-uploads")
#: Logger setup
LOG_LEVEL = logging.DEBUG
#: Disable CSRF protection for unit tests, since the Flask test runner
#: doesn't have good support for it.
WTF_CSRF_ENABLED = False
RECAPTCHA_PUBLIC_KEY = "re-public"
RECAPTCHA_PRIVATE_KEY = "re-private"
class DevelopmentConfig(BaseConfig):
"""For local development."""
AMBUDA_ENVIRONMENT = DEVELOPMENT
DEBUG = True
#: If set, automatically reload Flask templates (including imports) when
#: they change on disk.
TEMPLATES_AUTO_RELOAD = True
#: Logger setup
LOG_LEVEL = logging.INFO
class BuildConfig(BaseConfig):
"""For build on GitHub."""
AMBUDA_ENVIRONMENT = BUILD
DEBUG = True
#: If set, automatically reload Flask templates (including imports) when
#: they change on disk.
TEMPLATES_AUTO_RELOAD = False
#: Logger setup
LOG_LEVEL = logging.INFO
class StagingConfig(BaseConfig):
"""For staging."""
AMBUDA_ENVIRONMENT = STAGING
DEBUG = True
#: If set, automatically reload Flask templates (including imports) when
#: they change on disk.
TEMPLATES_AUTO_RELOAD = False
#: Logger setup
LOG_LEVEL = logging.INFO
class ProductionConfig(BaseConfig):
"""For production."""
AMBUDA_ENVIRONMENT = PRODUCTION
#: Logger setup
LOG_LEVEL = logging.INFO
# Deployment credentials
# ----------------------
#: Which directory to use on the production machine.
SERVER_APP_DIRECTORY = _env("SERVER_APP_DIRECTORY")
#: Server username.
SERVER_USER = _env("SERVER_USER")
#: Server host.
SERVER_HOST = _env("SERVER_HOST")
def _validate_config(config: BaseConfig):
"""Examine the given config and fail if the config is malformed.
:param config: the config to test
"""
assert config.AMBUDA_ENVIRONMENT in {
TESTING,
DEVELOPMENT,
BUILD,
STAGING,
PRODUCTION,
}
if not config.SQLALCHEMY_DATABASE_URI:
raise ValueError("This config does not define SQLALCHEMY_DATABASE_URI")
if not config.UPLOAD_FOLDER:
raise ValueError("This config does not define UPLOAD_FOLDER.")
if not Path(config.UPLOAD_FOLDER).is_absolute():
raise ValueError("UPLOAD_FOLDER must be an absolute path.")
# Production-specific validation.
if config.AMBUDA_ENVIRONMENT == PRODUCTION:
# All keys must be set.
for key in dir(config):
if key.isupper():
value = getattr(config, key)
assert value is not None, f"Config param {key} must not be `None`"
# App must not be in debug/test mode.
assert config.WTF_CSRF_ENABLED
assert not config.DEBUG
assert not config.TESTING
# Google credentials must be set and exist.
google_creds = _env("GOOGLE_APPLICATION_CREDENTIALS")
assert google_creds
assert Path(google_creds).exists()
def load_config_object(name: str):
"""Load and validate an application config."""
config_map = {
TESTING: UnitTestConfig,
DEVELOPMENT: DevelopmentConfig,
BUILD: BuildConfig,
STAGING: StagingConfig,
PRODUCTION: ProductionConfig,
}
config = config_map[name]
_validate_config(config)
return config
def create_config_only_app(config_name: str):
"""Create the application with just its config options set.
We use this function in Celery to get access to the app context while
avoiding any other setup work related to the application.
"""
load_dotenv(".env")
app = Flask(__name__)
app.config.from_object(load_config_object(config_name))
return app