2020-10-28 13:21:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import logging.config
							 | 
						
					
						
							
								
									
										
										
										
											2020-08-23 21:58:26 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import os
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-28 13:21:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import toml
							 | 
						
					
						
							
								
									
										
										
										
											2020-08-23 21:58:26 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								from pathlib import Path
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-03 15:56:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								from werkzeug.middleware.proxy_fix import ProxyFix
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								from .. import _module_path, logger
							 | 
						
					
						
							
								
									
										
										
										
											2020-08-23 21:58:26 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-28 13:21:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# Default config:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								config = {}
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-03 15:56:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								paths = [_module_path, Path.home() / ".config"]
							 | 
						
					
						
							
								
									
										
										
										
											2020-08-23 21:58:26 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								if "FLASCHENGEIST_CONF" in os.environ:
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-01 23:09:24 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    paths.append(Path(os.environ.get("FLASCHENGEIST_CONF")))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								for loc in paths:
							 | 
						
					
						
							
								
									
										
										
										
											2020-08-23 21:58:26 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    try:
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-28 13:21:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        with (loc / "flaschengeist.toml").open() as source:
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-07 16:11:38 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            logger.info("Reading config file from >{}<".format(loc))
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-28 13:21:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            config.update(toml.load(source))
							 | 
						
					
						
							
								
									
										
										
										
											2020-08-23 21:58:26 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    except IOError:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        pass
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-03 15:56:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# Always enable this builtin plugins!
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-28 13:21:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								config.update({"auth": {"enabled": True}, "roles": {"enabled": True}, "users": {"enabled": True}})
							 | 
						
					
						
							
								
									
										
										
										
											2020-08-23 21:58:26 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								def configure_app(app):
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-28 13:21:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    logger_config = toml.load(_module_path / "logging.toml")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if "LOGGING" in config:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if "level" in config["LOGGING"]:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            logger_config["loggers"]["flaschengeist"] = {"level": config["LOGGING"]["level"]}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if "file" in config["LOGGING"]:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            logger_config["root"]["handlers"].append("file")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            logger_config["handlers"]["file"]["filename"] = config["LOGGING"]["file"]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            path = Path(config["LOGGING"]["file"])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            path.parent.mkdir(parents=True, exist_ok=True)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    logging.config.dictConfig(logger_config)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if "secret_key" not in config["FLASCHENGEIST"]:
							 | 
						
					
						
							
								
									
										
										
										
											2020-08-23 21:58:26 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        logger.warn("No secret key was configured, please configure one for production systems!")
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-28 13:21:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        app.config["SECRET_KEY"] = "0a657b97ef546da90b2db91862ad4e29"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    else:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        app.config["SECRET_KEY"] = config["FLASCHENGEIST"]["secret_key"]
							 | 
						
					
						
							
								
									
										
										
										
											2020-08-23 21:58:26 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://{user}:{passwd}@{host}/{database}".format(
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-28 13:21:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        user=config["DATABASE"]["user"],
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        passwd=config["DATABASE"]["password"],
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        host=config["DATABASE"]["host"],
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        database=config["DATABASE"]["database"]
							 | 
						
					
						
							
								
									
										
										
										
											2020-08-23 21:58:26 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-03 15:56:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-28 13:21:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    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):
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-07 16:11:38 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        logger.debug("Fixing wsgi_app for using behind a proxy server")
							 | 
						
					
						
							
								
									
										
										
										
											2020-09-03 15:56:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
							 |