134 lines
4.3 KiB
Python
134 lines
4.3 KiB
Python
import os
|
|
import toml
|
|
import collections.abc
|
|
|
|
from pathlib import Path
|
|
from logging.config import dictConfig
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
|
|
|
from flaschengeist import logger
|
|
|
|
# Default config:
|
|
config = {"DATABASE": {"engine": "mysql", "port": 3306}}
|
|
|
|
|
|
def update_dict(d, u):
|
|
for k, v in u.items():
|
|
if isinstance(v, collections.abc.Mapping):
|
|
d[k] = update_dict(d.get(k, {}), v)
|
|
else:
|
|
d[k] = v
|
|
return d
|
|
|
|
|
|
def read_configuration(test_config):
|
|
global config
|
|
paths = [Path(__file__).parent]
|
|
|
|
if not test_config:
|
|
paths.append(Path.home() / ".config")
|
|
if "FLASCHENGEIST_CONF" in os.environ:
|
|
paths.append(Path(os.environ.get("FLASCHENGEIST_CONF")))
|
|
|
|
for loc in paths:
|
|
try:
|
|
with (loc / "flaschengeist.toml").open() as source:
|
|
logger.warning(f"Reading config file from >{loc}<") # default root logger, goes to stderr
|
|
update_dict(config, toml.load(source))
|
|
except IOError:
|
|
pass
|
|
if test_config:
|
|
update_dict(config, test_config)
|
|
|
|
|
|
def configure_logger():
|
|
"""Configure the logger
|
|
|
|
force_console: Force a console handler
|
|
"""
|
|
|
|
def set_level(level):
|
|
# TRACE means even with werkzeug's request traces
|
|
if isinstance(level, str) and level.lower() == "trace":
|
|
level = "DEBUG"
|
|
logger_config["loggers"]["werkzeug"] = {"level": level}
|
|
logger_config["loggers"]["flaschengeist"] = {"level": level}
|
|
logger_config["handlers"]["wsgi"]["level"] = level
|
|
|
|
# Read default config
|
|
logger_config = toml.load(Path(__file__).parent / "logging.toml")
|
|
if "LOGGING" in config:
|
|
# Override with user config
|
|
update_dict(logger_config, config.get("LOGGING"))
|
|
# Check for shortcuts
|
|
if "level" in config["LOGGING"]:
|
|
set_level(config["LOGGING"]["level"])
|
|
|
|
# Override logging, used e.g. by CLI
|
|
if "FG_LOGGING" in os.environ:
|
|
set_level(os.environ.get("FG_LOGGING", "CRITICAL"))
|
|
|
|
dictConfig(logger_config)
|
|
|
|
|
|
def configure_app(app, test_config=None):
|
|
global config
|
|
read_configuration(test_config)
|
|
|
|
configure_logger()
|
|
|
|
# Always enable this builtin plugins!
|
|
update_dict(
|
|
config,
|
|
{
|
|
"auth": {"enabled": True},
|
|
"roles": {"enabled": True},
|
|
"users": {"enabled": True},
|
|
"scheduler": {"enabled": True},
|
|
},
|
|
)
|
|
|
|
if "secret_key" not in config["FLASCHENGEIST"]:
|
|
logger.critical("No secret key was configured, please configure one for production systems!")
|
|
raise RuntimeError("No secret key was configured")
|
|
|
|
app.config["SECRET_KEY"] = config["FLASCHENGEIST"]["secret_key"]
|
|
|
|
if test_config is not None:
|
|
config["DATABASE"]["engine"] = "sqlite"
|
|
|
|
if config["DATABASE"]["engine"] == "mysql":
|
|
engine = "mysql"
|
|
try:
|
|
# Try mysqlclient first
|
|
from MySQLdb import _mysql
|
|
except ModuleNotFoundError:
|
|
engine += "+pymysql"
|
|
options = "?charset=utf8mb4"
|
|
elif config["DATABASE"]["engine"] == "postgres":
|
|
engine = "postgresql+psycopg2"
|
|
options = "?client_encoding=utf8"
|
|
elif config["DATABASE"]["engine"] == "sqlite":
|
|
engine = "sqlite"
|
|
options = ""
|
|
host = ""
|
|
else:
|
|
logger.error(f"Invalid database engine configured. >{config['DATABASE']['engine']}< is unknown")
|
|
raise Exception
|
|
if config["DATABASE"]["engine"] in ["mysql", "postgresql"]:
|
|
host = "{user}:{password}@{host}:{port}".format(
|
|
user=config["DATABASE"]["user"],
|
|
password=config["DATABASE"]["password"],
|
|
host=config["DATABASE"]["host"],
|
|
port=config["DATABASE"]["port"],
|
|
)
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = f"{engine}://{host}/{config['DATABASE']['database']}{options}"
|
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
|
|
|
if "root" in config["FLASCHENGEIST"]:
|
|
logger.debug("Setting application root to >{}<".format(config["FLASCHENGEIST"]["root"]))
|
|
app.config["APPLICATION_ROOT"] = config["FLASCHENGEIST"]["root"]
|
|
if config["FLASCHENGEIST"].get("proxy", False):
|
|
logger.debug("Fixing wsgi_app for using behind a proxy server")
|
|
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
|