2019-12-02 03:32:03 +00:00
|
|
|
import os
|
2019-12-10 10:08:43 +00:00
|
|
|
import logging
|
2019-12-02 03:32:03 +00:00
|
|
|
from flask import Flask
|
2019-12-21 14:43:03 +00:00
|
|
|
from flask_mail import Mail
|
2019-12-10 10:08:43 +00:00
|
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
2021-01-24 08:09:53 +00:00
|
|
|
from flask_session import Session
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
from .lib import utils
|
|
|
|
|
|
|
|
|
|
|
|
def create_app(config=None):
|
|
|
|
from . import models, routes, services
|
|
|
|
from .assets import assets
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2020-01-26 23:20:32 +00:00
|
|
|
# Read log level from environment variable
|
|
|
|
log_level_name = os.environ.get('PDNS_ADMIN_LOG_LEVEL', 'WARNING')
|
|
|
|
log_level = logging.getLevelName(log_level_name.upper())
|
2019-12-10 10:08:43 +00:00
|
|
|
# Setting logger
|
|
|
|
logging.basicConfig(
|
2020-01-26 23:20:32 +00:00
|
|
|
level=log_level,
|
2019-12-10 10:08:43 +00:00
|
|
|
format=
|
|
|
|
"[%(asctime)s] [%(filename)s:%(lineno)d] %(levelname)s - %(message)s")
|
|
|
|
|
|
|
|
# If we use Docker + Gunicorn, adjust the
|
|
|
|
# log handler
|
|
|
|
if "GUNICORN_LOGLEVEL" in os.environ:
|
|
|
|
gunicorn_logger = logging.getLogger("gunicorn.error")
|
|
|
|
app.logger.handlers = gunicorn_logger.handlers
|
|
|
|
app.logger.setLevel(gunicorn_logger.level)
|
|
|
|
|
2019-12-02 03:32:03 +00:00
|
|
|
# Proxy
|
|
|
|
app.wsgi_app = ProxyFix(app.wsgi_app)
|
|
|
|
|
2019-12-04 04:50:46 +00:00
|
|
|
# Load config from env variables if using docker
|
|
|
|
if os.path.exists(os.path.join(app.root_path, 'docker_config.py')):
|
|
|
|
app.config.from_object('powerdnsadmin.docker_config')
|
|
|
|
else:
|
|
|
|
# Load default configuration
|
|
|
|
app.config.from_object('powerdnsadmin.default_config')
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2019-12-04 04:50:46 +00:00
|
|
|
# Load config file from FLASK_CONF env variable
|
2019-12-02 03:32:03 +00:00
|
|
|
if 'FLASK_CONF' in os.environ:
|
|
|
|
app.config.from_envvar('FLASK_CONF')
|
|
|
|
|
2022-05-27 10:53:19 +00:00
|
|
|
# Load app specified configuration
|
2019-12-02 03:32:03 +00:00
|
|
|
if config is not None:
|
|
|
|
if isinstance(config, dict):
|
|
|
|
app.config.update(config)
|
|
|
|
elif config.endswith('.py'):
|
|
|
|
app.config.from_pyfile(config)
|
|
|
|
|
2019-12-04 04:50:46 +00:00
|
|
|
# HSTS
|
|
|
|
if app.config.get('HSTS_ENABLED'):
|
|
|
|
from flask_sslify import SSLify
|
2019-12-08 11:23:36 +00:00
|
|
|
_sslify = SSLify(app) # lgtm [py/unused-local-variable]
|
2019-12-04 04:50:46 +00:00
|
|
|
|
2021-01-24 08:09:53 +00:00
|
|
|
# Load Flask-Session
|
|
|
|
if app.config.get('FILESYSTEM_SESSIONS_ENABLED'):
|
|
|
|
app.config['SESSION_TYPE'] = 'filesystem'
|
|
|
|
sess = Session()
|
|
|
|
sess.init_app(app)
|
|
|
|
|
2019-12-21 14:43:03 +00:00
|
|
|
# SMTP
|
|
|
|
app.mail = Mail(app)
|
|
|
|
|
2019-12-02 03:32:03 +00:00
|
|
|
# Load app's components
|
|
|
|
assets.init_app(app)
|
|
|
|
models.init_app(app)
|
|
|
|
routes.init_app(app)
|
|
|
|
services.init_app(app)
|
|
|
|
|
|
|
|
# Register filters
|
|
|
|
app.jinja_env.filters['display_record_name'] = utils.display_record_name
|
|
|
|
app.jinja_env.filters['display_master_name'] = utils.display_master_name
|
|
|
|
app.jinja_env.filters['display_second_to_time'] = utils.display_time
|
2023-02-19 13:52:00 +00:00
|
|
|
app.jinja_env.filters['display_setting_state'] = utils.display_setting_state
|
2021-03-16 18:37:05 +00:00
|
|
|
app.jinja_env.filters['pretty_domain_name'] = utils.pretty_domain_name
|
2023-02-19 13:52:00 +00:00
|
|
|
app.jinja_env.filters['format_datetime_local'] = utils.format_datetime
|
2023-02-19 14:48:43 +00:00
|
|
|
app.jinja_env.filters['format_zone_type'] = utils.format_zone_type
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
# Register context proccessors
|
|
|
|
from .models.setting import Setting
|
|
|
|
|
|
|
|
@app.context_processor
|
|
|
|
def inject_sitename():
|
|
|
|
setting = Setting().get('site_name')
|
|
|
|
return dict(SITE_NAME=setting)
|
|
|
|
|
|
|
|
@app.context_processor
|
|
|
|
def inject_setting():
|
|
|
|
setting = Setting()
|
|
|
|
return dict(SETTING=setting)
|
|
|
|
|
2020-01-27 12:38:38 +00:00
|
|
|
return app
|