nopaque/config.py

169 lines
7.1 KiB
Python
Raw Normal View History

from dotenv import load_dotenv
from flask import Flask
2021-09-15 15:58:17 +00:00
from logging.handlers import RotatingFileHandler
from werkzeug.middleware.proxy_fix import ProxyFix
2019-09-11 12:50:29 +00:00
import logging
import os
2019-07-03 08:31:23 +00:00
basedir = os.path.abspath(os.path.dirname(__file__))
2023-09-25 08:11:11 +00:00
load_dotenv(os.path.join(basedir, 'nopaque.env'))
2019-09-05 08:12:40 +00:00
2020-05-27 09:03:50 +00:00
class Config:
2022-09-02 11:24:14 +00:00
''' APIFairy '''
APIFAIRY_TITLE = 'nopaque'
APIFAIRY_VERSION = '0.0.1'
APIFAIRY_UI = 'swagger_ui'
APIFAIRY_APISPEC_PATH = '/api/apispec.json'
APIFAIRY_UI_PATH = '/api'
''' # Flask # '''
PREFERRED_URL_SCHEME = os.environ.get('PREFERRED_URL_SCHEME', 'http')
SECRET_KEY = os.environ.get('SECRET_KEY', 'hard to guess string')
2022-05-05 13:07:36 +00:00
SERVER_NAME = os.environ.get('SERVER_NAME', 'localhost:5000')
SESSION_COOKIE_SECURE = \
os.environ.get('SESSION_COOKIE_SECURE', 'false').lower() == 'true'
2022-05-05 13:07:36 +00:00
''' # Flask-APScheduler # '''
JOBS = []
2022-05-05 13:07:36 +00:00
''' # Flask-Assets '''
ASSETS_DEBUG = os.environ.get('ASSETS_DEBUG', 'false').lower() == 'true'
2021-12-08 10:25:33 +00:00
''' # Flask-Hashids '''
2023-09-25 08:11:11 +00:00
HASHIDS_MIN_LENGTH = int(os.environ.get('HASHIDS_MIN_LENGTH', '16'))
HASHIDS_SALT=os.environ.get('HASHIDS_SALT', 'hard to guess string')
2021-12-08 10:25:33 +00:00
''' # Flask-Login # '''
REMEMBER_COOKIE_SECURE = \
os.environ.get('REMEMBER_COOKIE_SECURE', 'false').lower() == 'true'
''' # Flask-Mail # '''
MAIL_DEFAULT_SENDER = os.environ.get('MAIL_DEFAULT_SENDER')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
MAIL_SERVER = os.environ.get('MAIL_SERVER')
2022-05-05 13:07:36 +00:00
MAIL_PORT = int(os.environ.get('MAIL_PORT'))
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_USE_SSL = os.environ.get('MAIL_USE_SSL', 'false').lower() == 'true'
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS', 'false').lower() == 'true'
''' # Flask-SQLAlchemy # '''
2022-05-05 13:07:36 +00:00
SQLALCHEMY_DATABASE_URI = \
os.environ.get('SQLALCHEMY_DATABASE_URI') \
or f'sqlite:///{os.path.join(basedir, "data.sqlite")}'
SQLALCHEMY_RECORD_QUERIES = True
SQLALCHEMY_TRACK_MODIFICATIONS = False
''' # nopaque # '''
NOPAQUE_ADMIN = os.environ.get('NOPAQUE_ADMIN')
2022-02-08 14:39:15 +00:00
NOPAQUE_DATA_DIR = \
2023-09-25 08:11:11 +00:00
os.path.abspath(os.environ.get('NOPAQUE_DATA_PATH', '/mnt/nopaque'))
2022-05-05 13:07:36 +00:00
NOPAQUE_IS_PRIMARY_INSTANCE = \
os.environ.get('NOPAQUE_IS_PRIMARY_INSTANCE', 'true').lower() == 'true'
NOPAQUE_MAIL_SUBJECT_PREFIX = '[nopaque]'
NOPAQUE_SERVICE_DESK = 'gitlab-ub-incoming+sfb1288inf-nopaque-1324-issue-@jura.uni-bielefeld.de' # noqa
NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI = \
os.environ.get('NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI')
2022-09-02 11:24:14 +00:00
NOPAQUE_JOB_EXPIRATION_ENABLED = os.environ.get('NOPAQUE_JOB_EXPIRATION_ENABLED', 'true').lower() == 'true'
NOPAQUE_JOB_EXPIRATION_TIME = int(os.environ.get('NOPAQUE_JOB_EXPIRATION_TIME', '120'))
2021-11-15 13:34:07 +00:00
NOPAQUE_DOCKER_REGISTRY = 'gitlab.ub.uni-bielefeld.de:4567'
NOPAQUE_DOCKER_IMAGE_PREFIX = f'{NOPAQUE_DOCKER_REGISTRY}/sfb1288inf/'
2023-09-25 08:11:11 +00:00
NOPAQUE_DOCKER_NETWORK_NAME = \
os.environ.get('DOCKER_NETWORK_NAME', 'nopaque')
2021-11-15 13:34:07 +00:00
NOPAQUE_DOCKER_REGISTRY_USERNAME = \
os.environ.get('NOPAQUE_DOCKER_REGISTRY_USERNAME')
NOPAQUE_DOCKER_REGISTRY_PASSWORD = \
os.environ.get('NOPAQUE_DOCKER_REGISTRY_PASSWORD')
NOPAQUE_LOG_DATE_FORMAT = \
os.environ.get('NOPAQUE_LOG_DATE_FORMAT', '%Y-%m-%d %H:%M:%S')
2021-09-15 15:58:17 +00:00
NOPAQUE_LOG_FORMAT = os.environ.get(
'NOPAQUE_LOG_DATE_FORMAT',
'[%(asctime)s] %(levelname)s in '
'%(pathname)s (function: %(funcName)s, line: %(lineno)d): %(message)s'
)
NOPAQUE_LOG_LEVEL = os.environ.get('NOPAQUE_LOG_LEVEL', 'INFO')
NOPAQUE_LOG_FILE_ENABLED = \
os.environ.get('NOPAQUE_LOG_FILE_ENABLED', 'true').lower() == 'true'
NOPAQUE_LOG_FILE_DIR = \
2023-09-25 08:11:11 +00:00
os.environ.get('NOPAQUE_LOGS_PATH', os.path.join(basedir, 'logs'))
NOPAQUE_LOG_FILE_LEVEL = \
os.environ.get('NOPAQUE_LOG_FILE_LEVEL', NOPAQUE_LOG_LEVEL)
NOPAQUE_LOG_STDERR_ENABLED = \
os.environ.get('NOPAQUE_LOG_STDERR_ENABLED', 'false').lower() == 'true'
NOPAQUE_LOG_STDERR_LEVEL = \
os.environ.get('NOPAQUE_LOG_STDERR_LEVEL', NOPAQUE_LOG_LEVEL)
2021-09-15 15:58:17 +00:00
2022-05-05 13:07:36 +00:00
NOPAQUE_PROXY_FIX_ENABLED = \
os.environ.get('NOPAQUE_PROXY_FIX_ENABLED', 'false').lower() == 'true'
2021-09-15 15:58:17 +00:00
NOPAQUE_PROXY_FIX_X_FOR = \
int(os.environ.get('NOPAQUE_PROXY_FIX_X_FOR', '0'))
NOPAQUE_PROXY_FIX_X_HOST = \
int(os.environ.get('NOPAQUE_PROXY_FIX_X_HOST', '0'))
NOPAQUE_PROXY_FIX_X_PORT = \
int(os.environ.get('NOPAQUE_PROXY_FIX_X_PORT', '0'))
NOPAQUE_PROXY_FIX_X_PREFIX = \
int(os.environ.get('NOPAQUE_PROXY_FIX_X_PREFIX', '0'))
NOPAQUE_PROXY_FIX_X_PROTO = \
int(os.environ.get('NOPAQUE_PROXY_FIX_X_PROTO', '0'))
NOPAQUE_TRANSKRIBUS_ENABLED = \
2022-05-05 13:07:36 +00:00
os.environ.get('NOPAQUE_TRANSKRIBUS_ENABLED', 'false').lower() == 'true'
NOPAQUE_READCOOP_USERNAME = os.environ.get('NOPAQUE_READCOOP_USERNAME')
NOPAQUE_READCOOP_PASSWORD = os.environ.get('NOPAQUE_READCOOP_PASSWORD')
2022-05-05 13:07:36 +00:00
@staticmethod
def init_app(app: Flask):
# Set up logging according to the corresponding (NOPAQUE_LOG_*)
2021-09-15 15:58:17 +00:00
# configurations
2022-05-05 13:07:36 +00:00
app.logger.setLevel(app.config['NOPAQUE_LOG_LEVEL'])
# Remove existing handlers
2021-09-15 15:58:17 +00:00
for handler in app.logger.handlers:
app.logger.removeHandler(handler)
# Setup handlers
2021-09-15 15:58:17 +00:00
formatter = logging.Formatter(
2022-05-05 13:07:36 +00:00
fmt=app.config['NOPAQUE_LOG_FORMAT'],
datefmt=app.config['NOPAQUE_LOG_DATE_FORMAT']
2021-09-15 15:58:17 +00:00
)
if app.config['NOPAQUE_IS_PRIMARY_INSTANCE']:
app.config['JOBS'].append(
{
"id": "daemon",
"func": "app.daemon:daemon",
"args": (app,),
"trigger": "interval",
"seconds": 3,
}
)
2022-05-05 13:07:36 +00:00
if app.config['NOPAQUE_LOG_STDERR_ENABLED']:
2021-09-15 15:58:17 +00:00
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
2022-05-05 13:07:36 +00:00
stream_handler.setLevel(app.config['NOPAQUE_LOG_STDERR_LEVEL'])
2021-09-15 15:58:17 +00:00
app.logger.addHandler(stream_handler)
2022-05-05 13:07:36 +00:00
if app.config['NOPAQUE_LOG_FILE_ENABLED']:
if not os.path.exists(app.config['NOPAQUE_LOG_FILE_DIR']):
os.mkdir(app.config['NOPAQUE_LOG_FILE_DIR'])
2021-09-15 15:58:17 +00:00
rotating_file_handler = RotatingFileHandler(
2022-05-05 13:07:36 +00:00
os.path.join(app.config['NOPAQUE_LOG_FILE_DIR'], 'nopaque.log'), # noqa
maxBytes=10_240,
2021-09-15 15:58:17 +00:00
backupCount=10
)
rotating_file_handler.setFormatter(formatter)
2022-05-05 13:07:36 +00:00
rotating_file_handler.setLevel(app.config['NOPAQUE_LOG_FILE_LEVEL']) # noqa
2021-09-15 15:58:17 +00:00
app.logger.addHandler(rotating_file_handler)
2022-05-05 13:07:36 +00:00
if app.config['NOPAQUE_PROXY_FIX_ENABLED']:
# Set up and apply the ProxyFix middleware according to the
# corresponding (NOPAQUE_PROXY_FIX_*) configurations
app.wsgi_app = ProxyFix(
app.wsgi_app,
x_for=app.config['NOPAQUE_PROXY_FIX_X_FOR'],
x_host=app.config['NOPAQUE_PROXY_FIX_X_HOST'],
x_port=app.config['NOPAQUE_PROXY_FIX_X_PORT'],
x_prefix=app.config['NOPAQUE_PROXY_FIX_X_PREFIX'],
x_proto=app.config['NOPAQUE_PROXY_FIX_X_PROTO']
)