2020-05-27 12:52:41 +02:00
|
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
2019-07-03 10:31:23 +02:00
|
|
|
import os
|
2019-09-11 14:50:29 +02:00
|
|
|
import logging
|
2019-07-03 10:31:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Config:
|
2019-09-05 10:12:40 +02:00
|
|
|
''' ### Flask ### '''
|
|
|
|
SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'
|
|
|
|
|
|
|
|
''' ### Flask-Mail ### '''
|
2019-08-26 09:57:26 +02:00
|
|
|
MAIL_SERVER = os.environ.get('MAIL_SERVER')
|
|
|
|
MAIL_PORT = int(os.environ.get('MAIL_PORT'))
|
|
|
|
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS').lower() == 'true'
|
2019-07-08 11:29:11 +02:00
|
|
|
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
|
|
|
|
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
|
2019-09-05 10:12:40 +02:00
|
|
|
|
|
|
|
''' ### Flask-SQLAlchemy ### '''
|
2020-06-05 14:42:04 +02:00
|
|
|
SQLALCHEMY_DATABASE_URI = 'postgresql://{}:{}@db/{}'.format(
|
|
|
|
os.environ.get('POSTGRES_USER'),
|
|
|
|
os.environ.get('POSTGRES_PASSWORD'),
|
|
|
|
os.environ.get('POSTGRES_DB_NAME'))
|
2020-04-20 14:24:36 +02:00
|
|
|
SQLALCHEMY_RECORD_QUERIES = True
|
2019-07-05 14:47:35 +02:00
|
|
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
2019-07-03 15:40:09 +02:00
|
|
|
|
2020-04-20 14:24:36 +02:00
|
|
|
''' ### nopaque ### '''
|
2019-12-02 11:34:28 +01:00
|
|
|
NOPAQUE_ADMIN = os.environ.get('NOPAQUE_ADMIN')
|
2020-05-19 14:19:23 +02:00
|
|
|
NOPAQUE_CONTACT = os.environ.get('NOPAQUE_CONTACT')
|
2020-04-20 14:24:36 +02:00
|
|
|
NOPAQUE_MAIL_SENDER = os.environ.get('NOPAQUE_MAIL_SENDER')
|
|
|
|
NOPAQUE_MAIL_SUBJECT_PREFIX = '[nopaque]'
|
2020-06-17 09:26:37 +02:00
|
|
|
NOPAQUE_PROTOCOL = os.environ.get('NOPAQUE_PROTOCOL')
|
2020-04-09 07:57:52 +02:00
|
|
|
NOPAQUE_STORAGE = os.environ.get('NOPAQUE_STORAGE')
|
2019-09-05 10:12:40 +02:00
|
|
|
|
2020-05-27 11:03:50 +02:00
|
|
|
os.makedirs('logs', exist_ok=True)
|
|
|
|
logging.basicConfig(filename='logs/nopaque.log',
|
|
|
|
format='[%(asctime)s] %(levelname)s in '
|
2020-07-10 11:36:54 +02:00
|
|
|
'%(pathname)s:%(lineno)d - %(message)s',
|
2020-05-27 11:03:50 +02:00
|
|
|
datefmt='%Y-%m-%d %H:%M:%S', filemode='w')
|
|
|
|
|
2020-06-17 09:26:37 +02:00
|
|
|
''' ### Security enhancements ### '''
|
|
|
|
if NOPAQUE_PROTOCOL == 'https':
|
|
|
|
''' ### Flask ### '''
|
|
|
|
SESSION_COOKIE_SECURE = True
|
|
|
|
|
|
|
|
''' ### Flask-Login ### '''
|
|
|
|
REMEMBER_COOKIE_HTTPONLY = True
|
|
|
|
REMEMBER_COOKIE_SECURE = True
|
|
|
|
|
2019-07-03 15:40:09 +02:00
|
|
|
@staticmethod
|
|
|
|
def init_app(app):
|
2020-05-27 12:52:41 +02:00
|
|
|
proxy_fix_kwargs = {
|
|
|
|
'x_for': 1,
|
|
|
|
'x_host': 1,
|
|
|
|
'x_port': 1,
|
|
|
|
'x_proto': 1,
|
|
|
|
}
|
|
|
|
app.wsgi_app = ProxyFix(app.wsgi_app, **proxy_fix_kwargs)
|
2019-07-03 15:40:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DevelopmentConfig(Config):
|
2019-09-05 10:12:40 +02:00
|
|
|
''' ### Flask ### '''
|
2019-07-03 15:40:09 +02:00
|
|
|
DEBUG = True
|
|
|
|
|
2020-05-27 11:03:50 +02:00
|
|
|
''' ### nopaque ### '''
|
|
|
|
NOPAQUE_LOG_LEVEL = os.environ.get('NOPAQUE_LOG_LEVEL') or 'DEBUG'
|
|
|
|
logging.basicConfig(level=NOPAQUE_LOG_LEVEL)
|
2019-07-03 15:40:09 +02:00
|
|
|
|
|
|
|
|
2019-09-05 14:49:59 +02:00
|
|
|
class TestingConfig(Config):
|
2019-09-05 16:39:50 +02:00
|
|
|
''' ### Flask ### '''
|
|
|
|
TESTING = True
|
|
|
|
|
2019-09-05 14:49:59 +02:00
|
|
|
''' ### Flask-SQLAlchemy ### '''
|
|
|
|
SQLALCHEMY_DATABASE_URI = 'sqlite://'
|
|
|
|
|
2019-09-05 16:39:50 +02:00
|
|
|
''' ### Flask-WTF ### '''
|
|
|
|
WTF_CSRF_ENABLED = False
|
|
|
|
|
2019-09-05 14:49:59 +02:00
|
|
|
|
2019-09-05 10:12:40 +02:00
|
|
|
class ProductionConfig(Config):
|
2020-05-27 11:03:50 +02:00
|
|
|
''' ### nopaque ### '''
|
|
|
|
NOPAQUE_LOG_LEVEL = os.environ.get('NOPAQUE_LOG_LEVEL') or 'ERROR'
|
|
|
|
logging.basicConfig(level=NOPAQUE_LOG_LEVEL)
|
2019-07-03 15:40:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
config = {
|
|
|
|
'development': DevelopmentConfig,
|
2019-07-08 10:52:36 +02:00
|
|
|
'testing': TestingConfig,
|
2019-09-05 10:12:40 +02:00
|
|
|
'production': ProductionConfig,
|
2020-05-27 11:03:50 +02:00
|
|
|
'default': DevelopmentConfig,
|
2019-07-03 15:40:09 +02:00
|
|
|
}
|