-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
466 lines (358 loc) · 17.5 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
"""Flask configuration variables."""
from os import environ, path
from pathlib import Path
import os
import typing
from dotenv import load_dotenv
import logging
from enum import Enum
# for complete flask_sqlachemy config parameters and session handling,
# read: file flask_sqlalchemy/__init__.py AND flask/config.py
'''
app.config.setdefault('SQLALCHEMY_DATABASE_URI', 'sqlite:///:memory:')
app.config.setdefault('SQLALCHEMY_BINDS', None)
app.config.setdefault('SQLALCHEMY_NATIVE_UNICODE', None)
app.config.setdefault('SQLALCHEMY_ECHO', False)
app.config.setdefault('SQLALCHEMY_RECORD_QUERIES', None)
app.config.setdefault('SQLALCHEMY_POOL_SIZE', None)
app.config.setdefault('SQLALCHEMY_POOL_TIMEOUT', None)
app.config.setdefault('SQLALCHEMY_POOL_RECYCLE', None)
app.config.setdefault('SQLALCHEMY_MAX_OVERFLOW', None)
app.config.setdefault('SQLALCHEMY_COMMIT_ON_TEARDOWN', False)
'''
class ExtendedEnum(Enum):
"""
enum that supports list() to print allowed values
Thanks: https://stackoverflow.com/questions/29503339/how-to-get-all-values-from-python-enum-class
"""
@classmethod
def list(cls):
return list(map(lambda c: c.value, cls))
class OptLocking(ExtendedEnum):
IGNORED = "ignored"
OPTIONAL = "optional"
REQUIRED = "required"
basedir = path.abspath(path.dirname(__file__))
load_dotenv(path.join(basedir, "default.env"))
app_logger = logging.getLogger('api_logic_server_app')
def is_docker() -> bool:
""" running docker? dir exists: /home/api_logic_server """
path = '/home/api_logic_server'
path_result = os.path.isdir(path) # this *should* exist only on docker
env_result = "DOCKER" == os.getenv('APILOGICSERVER_RUNNING')
# assert path_result == env_result
return path_result
class Config:
"""
Set default Flask configuration from .env file.
These values are overridden by api_logic_server_run cli args, and APILOGICPROJECT_ env variables.
Code should therefore access these ONLY as described in Args, below.
"""
# Project Creation Defaults (overridden from args, env variables)
CREATED_API_PREFIX = "/api"
CREATED_FLASK_HOST = "localhost"
""" where clients find the API (eg, cloud server addr)"""
CREATED_SWAGGER_HOST = "localhost"
""" where swagger (and other clients) find the API """
if CREATED_SWAGGER_HOST == "":
CREATED_SWAGGER_HOST = CREATED_FLASK_HOST #
if is_docker and CREATED_FLASK_HOST == "localhost":
CREATED_FLASK_HOST = "0.0.0.0" # enables docker run.sh (where there are no args)
CREATED_PORT = "5656"
CREATED_SWAGGER_PORT = CREATED_PORT
""" for codespaces - see values in launch config """
CREATED_HTTP_SCHEME = "http"
# General Config
SECRET_KEY = environ.get("SECRET_KEY")
FLASK_APP = environ.get("FLASK_APP")
FLASK_ENV = environ.get("FLASK_ENV")
DEBUG = environ.get("DEBUG")
running_at = Path(__file__)
project_abs_dir = running_at.parent.absolute()
# Database
SQLALCHEMY_DATABASE_URI : typing.Optional[str] = f"postgresql://postgres:p@localhost:5432/postgres"
# override SQLALCHEMY_DATABASE_URI here as required
app_logger.debug(f'config.py - SQLALCHEMY_DATABASE_URI: {SQLALCHEMY_DATABASE_URI}')
# as desired, use env variable: export SQLALCHEMY_DATABASE_URI='sqlite:////Users/val/dev/servers/docker_api_logic_project/database/db.sqliteXX'
if os.getenv('SQLALCHEMY_DATABASE_URI'): # e.g. export SECURITY_ENABLED=true
SQLALCHEMY_DATABASE_URI = os.getenv('SQLALCHEMY_DATABASE_URI')
app_logger.debug(f'.. overridden from env variable: {SQLALCHEMY_DATABASE_URI}')
SECURITY_ENABLED = True # you must also: ApiLogicServer add-db --db_url=auth --bind_key=authentication
SECURITY_PROVIDER = None
if os.getenv('SECURITY_ENABLED'): # e.g. export SECURITY_ENABLED=true
security_export = os.getenv('SECURITY_ENABLED') # type: ignore # type: str
security_export = security_export.lower() # type: ignore
if security_export in ["false", "no"]: # NO SEC
SECURITY_ENABLED = False
else:
SECURITY_ENABLED = True
app_logger.debug(f'Security .. overridden from env variable: {SECURITY_ENABLED}')
if SECURITY_ENABLED:
from security.authentication_provider.sql.sqlite.auth_provider import Authentication_Provider
SECURITY_PROVIDER = Authentication_Provider
app_logger.debug(f'config.py - security enabled')
else:
app_logger.info(f'config.py - security disabled')
# Begin Multi-Database URLs (from ApiLogicServer add-db...)
SQLALCHEMY_DATABASE_URI_AUTHENTICATION = 'postgresql://postgres:p@localhost/authdb'
app_logger.info(f'config.py - SQLALCHEMY_DATABASE_URI_AUTHENTICATION: {SQLALCHEMY_DATABASE_URI_AUTHENTICATION}\n')
# as desired, use env variable: export SQLALCHEMY_DATABASE_URI='sqlite:////Users/val/dev/servers/docker_api_logic_project/database/db.sqliteXX'
if os.getenv('SQLALCHEMY_DATABASE_URI_AUTHENTICATION'):
SQLALCHEMY_DATABASE_URI_AUTHENTICATION = os.getenv('SQLALCHEMY_DATABASE_URI_AUTHENTICATION') # type: ignore # type: str
app_logger.debug(f'.. overridden from env variable: SQLALCHEMY_DATABASE_URI_AUTHENTICATION')
# End Multi-Database URLs (from ApiLogicServer add-db...)
# SQLALCHEMY_ECHO = environ.get("SQLALCHEMY_ECHO")
SQLALCHEMY_TRACK_MODIFICATIONS = False
PROPAGATE_EXCEPTIONS = False
OPT_LOCKING = "optional"
if os.getenv('OPT_LOCKING'): # e.g. export OPT_LOCKING=required
opt_locking_export = os.getenv('OPT_LOCKING') # type: ignore # type: str
opt_locking = opt_locking_export.lower() # type: ignore
if opt_locking in OptLocking.list():
OPT_LOCKING = opt_locking
else:
print(f'\n{__name__}: Invalid OPT_LOCKING.\n..Valid values are {OptLocking.list()}')
exit(1)
app_logger.debug(f'Opt Locking .. overridden from env variable: {OPT_LOCKING}')
class Args():
"""
Singleton class - typed accessors for flask_app.config values.
The source of truth is the flask_app.config.
Set from created values in Config, overwritten by cli args, then APILOGICPROJECT_ env variables.
This class provides **typed** access.
"""
values = None
def __new__(cls, flask_app):
if not hasattr(cls, 'instance'):
cls.instance = super(Args, cls).__new__(cls)
return cls.instance
def __init__(self, flask_app):
"""
Create args object with CREATED_ defaults.
Defaults assigned to flask_app.config[name]
1. flask_app.config names do not have suffix (e.g. flask_app.config["FLASK_HOST"])
2. Override with CLI arguments (use -h to see names)
3. Then override with env variables (prefixed, e.g., APILOGICPROJECT_FLASK_HOST)
Args:
flask_app (_type_): created flask_app
"""
self.flask_app = flask_app
self.api_prefix = Config.CREATED_API_PREFIX
self.flask_host = Config.CREATED_FLASK_HOST
self.swagger_host = Config.CREATED_SWAGGER_HOST
self.port = Config.CREATED_PORT
self.swagger_port = Config.CREATED_PORT
self.http_scheme = Config.CREATED_HTTP_SCHEME
self.verbose = False
self.create_and_run = False
@property
def port(self) -> str:
""" port to which flask will be bound """
return self.flask_app.config["PORT"] # if "PORT" in self.flask_app.config else self.__port
@port.setter
def port(self, a):
self.flask_app.config["PORT"] = a
@property
def swagger_port(self) -> str:
""" swagger port (eg, 443 for codespaces) """
return self.flask_app.config["SWAGGER_PORT"]
@swagger_port.setter
def swagger_port(self, a):
self.flask_app.config["SWAGGER_PORT"] = a
@property
def swagger_host(self) -> str:
""" ip clients use to access API """
return self.flask_app.config["SWAGGER_HOST"]
@swagger_host.setter
def swagger_host(self, a):
self.flask_app.config["SWAGGER_HOST"] = a
@property
def flask_host(self) -> str:
""" ip to which flask will be bound """
return self.flask_app.config["FLASK_HOST"]
@flask_host.setter
def flask_host(self, a):
self.flask_app.config["FLASK_HOST"] = a
@property
def security_enabled(self) -> bool:
""" is security enabled. Stored as string, returned as bool """
return_security = self.flask_app.config["SECURITY_ENABLED"]
if isinstance(return_security, str):
security = return_security.lower() # type: ignore
if security in ["false", "no"]: # NO SEC
return_security = False
else:
return_security = True
return return_security
@security_enabled.setter
def security_enabled(self, a):
self.flask_app.config["SECURITY_ENABLED"] = a
@property
def security_provider(self):
""" class for auth provider (unused - see auth_provider) """
return self.flask_app.config["SECURITY_PROVIDER"]
@security_provider.setter
def security_provider(self, a):
raise Exception("Sorry, security_provider must be specified in the Config class")
@property
def api_logic_server_home(self):
""" location of ApiLogicServer-src (for admin_loader) """
return self.flask_app.config["APILOGICSERVER_HOME"]
@api_logic_server_home.setter
def api_logic_server_home(self, a):
self.flask_app.config["APILOGICSERVER_HOME"] = a
@property
def opt_locking(self) -> str:
""" values: ignored, optional, required """
return self.flask_app.config["OPT_LOCKING"]
@opt_locking.setter
def opt_locking(self, a):
opt_locking_export = self.flask_app.config('OPT_LOCKING') # type: ignore # type: str
opt_locking = opt_locking_export.lower() # type: ignore
if opt_locking in OptLocking.list():
OPT_LOCKING = opt_locking
else:
print(f'\n{__name__}: Invalid APILOGICPROJECT_OPT_LOCKING.\n..Valid values are {OptLocking.list()}')
exit(1)
app_logger.debug(f'Opt Locking .. overridden from env variable: {OPT_LOCKING}')
self.flask_app.config["OPT_LOCKING"] = a
@property
def api_prefix(self) -> str:
""" uri node for this project (e.g, /api) """
return self.flask_app.config["API_PREFIX"]
@api_prefix.setter
def api_prefix(self, a):
self.flask_app.config["API_PREFIX"] = a
@property
def http_scheme(self) -> str:
""" http or https """
return self.flask_app.config["HTTP_SCHEME"]
@http_scheme.setter
def http_scheme(self, a):
self.flask_app.config["HTTP_SCHEME"] = a
@property
def create_and_run(self):
""" internal use: ApiLogicServer create-and-run """
return self.flask_app.config["CREATE_AND_RUN"]
@create_and_run.setter
def create_and_run(self, a):
self.flask_app.config["CREATE_AND_RUN"] = a
@property
def verbose(self):
""" activate key loggers for debug """
return self.flask_app.config["VERBOSE"]
@verbose.setter
def verbose(self, a):
self.flask_app.config["VERBOSE"] = a
@property
def client_uri(self):
""" in prod env, port might be omitted (e.g., nginx) """
return self.flask_app.config["CLIENT_URI"] if "CLIENT_URI" in self.flask_app.config \
else None
@client_uri.setter
def client_uri(self, a):
self.flask_app.config["CLIENT_URI"] = a
def __str__(self) -> str:
rtn = f'.. flask_host: {self.flask_host}, port: {self.port}, \n'\
f'.. swagger_host: {self.swagger_host}, swagger_port: {self.swagger_port}, \n'\
f'.. client_uri: {self.client_uri}, \n'\
f'.. http_scheme: {self.http_scheme}, api_prefix: {self.api_prefix}, \n'\
f'.. | verbose: {self.verbose}, create_and_run: {self.create_and_run}'
return rtn
def get_cli_args(self, args: 'Args', dunder_name: str):
"""
returns tuple of start args:
(flask_host, swagger_host, port, swagger_port, http_scheme, verbose, create_and_run)
"""
import socket
import warnings
import sys
# global flask_host, swagger_host, port, swagger_port, http_scheme, verbose, create_and_run
network_diagnostics = True
hostname = socket.gethostname()
try:
local_ip = socket.gethostbyname(hostname)
except:
local_ip = f"Warning - Failed local_ip = socket.gethostbyname(hostname) with hostname: {hostname}"
app_logger.debug(f"Failed local_ip = socket.gethostbyname(hostname) with hostname: {hostname}")
app_logger.debug(f"Getting cli args, with hostname={hostname} on local_ip={local_ip}")
args.verbose = False
args.create_and_run = False
def make_wide(formatter, w=120, h=36):
""" Return a wider HelpFormatter, if possible."""
try:
# https://stackoverflow.com/a/5464440
# beware: "Only the name of this class is considered a public API."
kwargs = {'width': w, 'max_help_position': h}
formatter(None, **kwargs)
return lambda prog: formatter(prog, **kwargs)
except TypeError:
warnings.warn("argparse help formatter failed, falling back.")
return formatter
if dunder_name != "__main__":
app_logger.debug(f"WSGI - no args, using creation default host/port.. sys.argv = {sys.argv}\n")
else: # gunicorn-friendly host/port settings ()
# thanks to https://www.geeksforgeeks.org/command-line-arguments-in-python/#argparse
import argparse
# Initialize parser
if len(sys.argv) == 1:
app_logger.debug("No arguments - using creation default host/port")
else:
msg = "API Logic Project"
parser = argparse.ArgumentParser(
formatter_class=make_wide(argparse.ArgumentDefaultsHelpFormatter))
parser.add_argument("--port",
help = f'port (Flask)', default = args.port)
parser.add_argument("--flask_host",
help = f'ip to which flask will be bound',
default = args.flask_host)
parser.add_argument("--swagger_host",
help = f'ip clients use to access API',
default = args.swagger_host)
parser.add_argument("--swagger_port",
help = f'swagger port (eg, 443 for codespaces)',
default = args.port)
parser.add_argument("--http_scheme",
help = f'http or https',
default = "http")
parser.add_argument("--verbose",
help = f'for more logging',
default = False)
parser.add_argument("--create_and_run",
help = f'system use - log how to open project',
default = False)
parser.add_argument("flask_host_p", nargs='?', default = args.flask_host)
parser.add_argument("port_p", nargs='?', default = args.port)
parser.add_argument("swagger_host_p", nargs='?', default = args.swagger_host)
parse_args = parser.parse_args()
"""
accepting both positional (compatibility) and keyword args...
cases that matter:
no args
kw only: argv[1] starts with -
pos only
positional values always override keyword, so decide which parsed values to use...
"""
if sys.argv[1].startswith("-"): # keyword arguments
args.port = parse_args.port
args.flask_host = parse_args.flask_host
args.swagger_host = parse_args.swagger_host
args.swagger_port = parse_args.swagger_port
args.http_scheme = parse_args.http_scheme
args.verbose = parse_args.verbose in ["True", "true"]
args.create_and_run = parse_args.create_and_run
else: # positional arguments (compatibility)
args.port = parse_args.port_p
args.flask_host = parse_args.flask_host_p
args.swagger_host = parse_args.swagger_host_p
if args.swagger_host.startswith("https://"):
args.swagger_host = args.swagger_host[8:]
if args.swagger_host.endswith("/"):
args.swagger_host = args.swagger_host[0:len(args.swagger_host)-1]
use_codespace_defaulting = True # experimental support to run default launch config
if use_codespace_defaulting and os.getenv('CODESPACES') and args.swagger_host == 'localhost':
app_logger.info('\n Applying Codespaces default port settings')
args.swagger_host = os.getenv('CODESPACE_NAME') + '-5656.githubpreview.dev'
args.swagger_port = 443
args.http_scheme = 'https'
return