-
Notifications
You must be signed in to change notification settings - Fork 33
/
config.py
81 lines (63 loc) · 2.47 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# config.py: configuration data of this app
# - other modules should read config from here
# - the config is first read from a json file
# - env variables may override (in docker setup etc)
import sys
import os
import redis
import json
import time
# first load config from a json file,
srvconf = json.load(open(os.environ["PYSRV_CONFIG_PATH"]))
# then override with env variables
for k, v in os.environ.items():
if k.startswith("PYSRV_"):
print("env override ", k)
srvconf[k] = v
# grand switch to production!
IS_PRODUCTION = bool(srvconf['PYSRV_IS_PRODUCTION'] or False)
# local dev flag
IS_LOCAL_DEV = os.environ.get("FLASK_ENV") == "development" and not IS_PRODUCTION
# IS_LOCAL_DEV = False
print("\nCONFIG: prod={},localdev={} ({})\n".format(
IS_PRODUCTION, IS_LOCAL_DEV, srvconf["name"]))
# database config
DATABASE_HOST = srvconf['PYSRV_DATABASE_HOST']
DATABASE_PORT = srvconf['PYSRV_DATABASE_PORT']
DATABASE_NAME = srvconf['PYSRV_DATABASE_NAME']
DATABASE_USER = srvconf['PYSRV_DATABASE_USER']
DATABASE_PASSWORD = srvconf['PYSRV_DATABASE_PASSWORD']
IS_SQLITE = DATABASE_HOST.startswith("/")
# Flask + session config
# http://flask.pocoo.org/docs/1.0/config/
# https://pythonhosted.org/Flask-Session/
redishost = srvconf['PYSRV_REDIS_HOST']
flask_config = dict(
# app config
TESTING = IS_LOCAL_DEV,
SECRET_KEY = None, # we have server-side sessions
# session config - hardcoded to Redis
SESSION_TYPE = 'redis',
SESSION_REDIS = redis.from_url('redis://{}'.format(redishost)),
SESSION_COOKIE_NAME = "mycookie",
SESSION_COOKIE_SECURE = srvconf['PYSRV_COOKIE_HTTPS_ONLY'] if not IS_LOCAL_DEV else False, # require https?
SESSION_COOKIE_HTTPONLY = True, # don't allow JS cookie access
SESSION_KEY_PREFIX = 'pysrv',
PERMANENT_SESSION_LIFETIME = 60*60*24*30, # 1 month
SESSION_COOKIE_DOMAIN = srvconf['PYSRV_DOMAIN_NAME'] or None if not IS_LOCAL_DEV else None,
)
# dump sql statements in log file?
PYSRV_LOG_SQL = srvconf.get('PYSRV_LOG_SQL')
# allow API access to this domain
CORS_ALLOW_ORIGIN = srvconf.get('PYSRV_CORS_ALLOW_ORIGIN', '*')
START_TIME = int(time.time())
def started_ago(as_string=False):
"""Returns how many seconds ago the server was started. Or as a string."""
ago = int(time.time()) - START_TIME
if as_string:
return "{}d {:02d}:{:02d}:{:02d}".format(int(ago/60/60/24),
int(ago/60/60)%24, int(ago/60)%60, ago%60)
else:
return ago