Merge pull request #670 from kaiyou/refactor-config
Refactor the admin architecture and configuration managementmaster
commit
3d98124bcd
@ -1,140 +1,57 @@
|
||||
import flask
|
||||
import flask_sqlalchemy
|
||||
import flask_bootstrap
|
||||
import flask_login
|
||||
import flask_script
|
||||
import flask_migrate
|
||||
import flask_babel
|
||||
import flask_limiter
|
||||
|
||||
import os
|
||||
import docker
|
||||
import socket
|
||||
import uuid
|
||||
|
||||
from werkzeug.contrib import fixers, profiler
|
||||
|
||||
# Create application
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
default_config = {
|
||||
# Specific to the admin UI
|
||||
'SQLALCHEMY_DATABASE_URI': 'sqlite:////data/main.db',
|
||||
'SQLALCHEMY_TRACK_MODIFICATIONS': False,
|
||||
'DOCKER_SOCKET': 'unix:///var/run/docker.sock',
|
||||
'BABEL_DEFAULT_LOCALE': 'en',
|
||||
'BABEL_DEFAULT_TIMEZONE': 'UTC',
|
||||
'BOOTSTRAP_SERVE_LOCAL': True,
|
||||
'RATELIMIT_STORAGE_URL': 'redis://redis/2',
|
||||
'QUOTA_STORAGE_URL': 'redis://redis/1',
|
||||
'DEBUG': False,
|
||||
'DOMAIN_REGISTRATION': False,
|
||||
# Statistics management
|
||||
'INSTANCE_ID_PATH': '/data/instance',
|
||||
'STATS_ENDPOINT': '0.{}.stats.mailu.io',
|
||||
# Common configuration variables
|
||||
'SECRET_KEY': 'changeMe',
|
||||
'DOMAIN': 'mailu.io',
|
||||
'HOSTNAMES': 'mail.mailu.io,alternative.mailu.io,yetanother.mailu.io',
|
||||
'POSTMASTER': 'postmaster',
|
||||
'TLS_FLAVOR': 'cert',
|
||||
'AUTH_RATELIMIT': '10/minute;1000/hour',
|
||||
'DISABLE_STATISTICS': 'False',
|
||||
# Mail settings
|
||||
'DMARC_RUA': None,
|
||||
'DMARC_RUF': None,
|
||||
'WELCOME': 'False',
|
||||
'WELCOME_SUBJECT': 'Dummy welcome topic',
|
||||
'WELCOME_BODY': 'Dummy welcome body',
|
||||
'DKIM_SELECTOR': 'dkim',
|
||||
'DKIM_PATH': '/dkim/{domain}.{selector}.key',
|
||||
'DEFAULT_QUOTA': 1000000000,
|
||||
# Web settings
|
||||
'SITENAME': 'Mailu',
|
||||
'WEBSITE': 'https://mailu.io',
|
||||
'WEB_ADMIN': '/admin',
|
||||
'WEB_WEBMAIL': '/webmail',
|
||||
'RECAPTCHA_PUBLIC_KEY': '',
|
||||
'RECAPTCHA_PRIVATE_KEY': '',
|
||||
# Advanced settings
|
||||
'PASSWORD_SCHEME': 'BLF-CRYPT',
|
||||
# Host settings
|
||||
'HOST_IMAP': 'imap',
|
||||
'HOST_POP3': 'imap',
|
||||
'HOST_SMTP': 'smtp',
|
||||
'HOST_WEBMAIL': 'webmail',
|
||||
'HOST_FRONT': 'front',
|
||||
'HOST_AUTHSMTP': os.environ.get('HOST_SMTP', 'smtp'),
|
||||
'POD_ADDRESS_RANGE': None
|
||||
}
|
||||
|
||||
# Load configuration from the environment if available
|
||||
for key, value in default_config.items():
|
||||
app.config[key] = os.environ.get(key, value)
|
||||
|
||||
# Base application
|
||||
flask_bootstrap.Bootstrap(app)
|
||||
db = flask_sqlalchemy.SQLAlchemy(app)
|
||||
migrate = flask_migrate.Migrate(app, db)
|
||||
limiter = flask_limiter.Limiter(app, key_func=lambda: current_user.username)
|
||||
|
||||
# Debugging toolbar
|
||||
if app.config.get("DEBUG"):
|
||||
import flask_debugtoolbar
|
||||
toolbar = flask_debugtoolbar.DebugToolbarExtension(app)
|
||||
|
||||
# Profiler
|
||||
if app.config.get("DEBUG"):
|
||||
app.wsgi_app = profiler.ProfilerMiddleware(app.wsgi_app, restrictions=[30])
|
||||
|
||||
# Manager commnad
|
||||
manager = flask_script.Manager(app)
|
||||
manager.add_command('db', flask_migrate.MigrateCommand)
|
||||
|
||||
# Babel configuration
|
||||
babel = flask_babel.Babel(app)
|
||||
translations = list(map(str, babel.list_translations()))
|
||||
|
||||
@babel.localeselector
|
||||
def get_locale():
|
||||
return flask.request.accept_languages.best_match(translations)
|
||||
|
||||
# Login configuration
|
||||
login_manager = flask_login.LoginManager()
|
||||
login_manager.init_app(app)
|
||||
login_manager.login_view = "ui.login"
|
||||
|
||||
@login_manager.unauthorized_handler
|
||||
def handle_needs_login():
|
||||
return flask.redirect(
|
||||
flask.url_for('ui.login', next=flask.request.endpoint)
|
||||
)
|
||||
|
||||
@app.context_processor
|
||||
def inject_defaults():
|
||||
signup_domains = models.Domain.query.filter_by(signup_enabled=True).all()
|
||||
return dict(
|
||||
current_user=flask_login.current_user,
|
||||
signup_domains=signup_domains,
|
||||
config=app.config
|
||||
)
|
||||
|
||||
# Import views
|
||||
from mailu import ui, internal
|
||||
app.register_blueprint(ui.ui, url_prefix='/ui')
|
||||
app.register_blueprint(internal.internal, url_prefix='/internal')
|
||||
|
||||
# Create the prefix middleware
|
||||
class PrefixMiddleware(object):
|
||||
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
|
||||
def __call__(self, environ, start_response):
|
||||
prefix = environ.get('HTTP_X_FORWARDED_PREFIX', '')
|
||||
if prefix:
|
||||
environ['SCRIPT_NAME'] = prefix
|
||||
return self.app(environ, start_response)
|
||||
from mailu import utils, debug, models, manage, configuration
|
||||
|
||||
|
||||
app.wsgi_app = PrefixMiddleware(fixers.ProxyFix(app.wsgi_app))
|
||||
def create_app_from_config(config):
|
||||
""" Create a new application based on the given configuration
|
||||
"""
|
||||
app = flask.Flask(__name__)
|
||||
app.app_context().push()
|
||||
app.cli.add_command(manage.mailu)
|
||||
|
||||
# Bootstrap is used for basic JS and CSS loading
|
||||
# TODO: remove this and use statically generated assets instead
|
||||
app.bootstrap = flask_bootstrap.Bootstrap(app)
|
||||
|
||||
# Initialize application extensions
|
||||
config.init_app(app)
|
||||
models.db.init_app(app)
|
||||
utils.limiter.init_app(app)
|
||||
utils.babel.init_app(app)
|
||||
utils.login.init_app(app)
|
||||
utils.login.user_loader(models.User.get)
|
||||
utils.proxy.init_app(app)
|
||||
utils.migrate.init_app(app, models.db)
|
||||
|
||||
# Initialize debugging tools
|
||||
if app.config.get("DEBUG"):
|
||||
debug.toolbar.init_app(app)
|
||||
# TODO: add a specific configuration variable for profiling
|
||||
# debug.profiler.init_app(app)
|
||||
|
||||
# Inject the default variables in the Jinja parser
|
||||
# TODO: move this to blueprints when needed
|
||||
@app.context_processor
|
||||
def inject_defaults():
|
||||
signup_domains = models.Domain.query.filter_by(signup_enabled=True).all()
|
||||
return dict(
|
||||
signup_domains=signup_domains,
|
||||
config=app.config
|
||||
)
|
||||
|
||||
# Import views
|
||||
from mailu import ui, internal
|
||||
app.register_blueprint(ui.ui, url_prefix='/ui')
|
||||
app.register_blueprint(internal.internal, url_prefix='/internal')
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def create_app():
|
||||
""" Create a new application based on the config module
|
||||
"""
|
||||
config = configuration.ConfigManager()
|
||||
return create_app_from_config(config)
|
||||
|
||||
|
@ -0,0 +1,90 @@
|
||||
import os
|
||||
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
# Specific to the admin UI
|
||||
'SQLALCHEMY_DATABASE_URI': 'sqlite:////data/main.db',
|
||||
'SQLALCHEMY_TRACK_MODIFICATIONS': False,
|
||||
'DOCKER_SOCKET': 'unix:///var/run/docker.sock',
|
||||
'BABEL_DEFAULT_LOCALE': 'en',
|
||||
'BABEL_DEFAULT_TIMEZONE': 'UTC',
|
||||
'BOOTSTRAP_SERVE_LOCAL': True,
|
||||
'RATELIMIT_STORAGE_URL': 'redis://redis/2',
|
||||
'QUOTA_STORAGE_URL': 'redis://redis/1',
|
||||
'DEBUG': False,
|
||||
'DOMAIN_REGISTRATION': False,
|
||||
'TEMPLATES_AUTO_RELOAD': True,
|
||||
# Statistics management
|
||||
'INSTANCE_ID_PATH': '/data/instance',
|
||||
'STATS_ENDPOINT': '0.{}.stats.mailu.io',
|
||||
# Common configuration variables
|
||||
'SECRET_KEY': 'changeMe',
|
||||
'DOMAIN': 'mailu.io',
|
||||
'HOSTNAMES': 'mail.mailu.io,alternative.mailu.io,yetanother.mailu.io',
|
||||
'POSTMASTER': 'postmaster',
|
||||
'TLS_FLAVOR': 'cert',
|
||||
'AUTH_RATELIMIT': '10/minute;1000/hour',
|
||||
'DISABLE_STATISTICS': 'False',
|
||||
# Mail settings
|
||||
'DMARC_RUA': None,
|
||||
'DMARC_RUF': None,
|
||||
'WELCOME': 'False',
|
||||
'WELCOME_SUBJECT': 'Dummy welcome topic',
|
||||
'WELCOME_BODY': 'Dummy welcome body',
|
||||
'DKIM_SELECTOR': 'dkim',
|
||||
'DKIM_PATH': '/dkim/{domain}.{selector}.key',
|
||||
'DEFAULT_QUOTA': 1000000000,
|
||||
# Web settings
|
||||
'SITENAME': 'Mailu',
|
||||
'WEBSITE': 'https://mailu.io',
|
||||
'WEB_ADMIN': '/admin',
|
||||
'WEB_WEBMAIL': '/webmail',
|
||||
'RECAPTCHA_PUBLIC_KEY': '',
|
||||
'RECAPTCHA_PRIVATE_KEY': '',
|
||||
# Advanced settings
|
||||
'PASSWORD_SCHEME': 'BLF-CRYPT',
|
||||
# Host settings
|
||||
'HOST_IMAP': 'imap',
|
||||
'HOST_POP3': 'imap',
|
||||
'HOST_SMTP': 'smtp',
|
||||
'HOST_WEBMAIL': 'webmail',
|
||||
'HOST_FRONT': 'front',
|
||||
'HOST_AUTHSMTP': os.environ.get('HOST_SMTP', 'smtp'),
|
||||
'POD_ADDRESS_RANGE': None
|
||||
}
|
||||
|
||||
|
||||
class ConfigManager(dict):
|
||||
""" Naive configuration manager that uses environment only
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.config = dict()
|
||||
|
||||
def init_app(self, app):
|
||||
self.config.update(app.config)
|
||||
self.config.update({
|
||||
key: os.environ.get(key, value)
|
||||
for key, value in DEFAULT_CONFIG.items()
|
||||
})
|
||||
app.config = self
|
||||
|
||||
def setdefault(self, key, value):
|
||||
if key not in self.config:
|
||||
self.config[key] = value
|
||||
return self.config[key]
|
||||
|
||||
def get(self, *args):
|
||||
return self.config.get(*args)
|
||||
|
||||
def keys(self):
|
||||
return self.config.keys()
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.config.get(key)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self.config[key] = value
|
||||
|
||||
def __contains__(self, key):
|
||||
return key in self.config
|
@ -0,0 +1,17 @@
|
||||
import flask_debugtoolbar
|
||||
|
||||
from werkzeug.contrib import profiler as werkzeug_profiler
|
||||
|
||||
|
||||
# Debugging toolbar
|
||||
toolbar = flask_debugtoolbar.DebugToolbarExtension()
|
||||
|
||||
|
||||
# Profiler
|
||||
class Profiler(object):
|
||||
def init_app(self, app):
|
||||
app.wsgi_app = werkzeug_profiler.ProfilerMiddleware(
|
||||
app.wsgi_app, restrictions=[30]
|
||||
)
|
||||
|
||||
profiler = Profiler()
|
@ -1,26 +0,0 @@
|
||||
from mailu import app
|
||||
|
||||
import docker
|
||||
import signal
|
||||
|
||||
|
||||
# Connect to the Docker socket
|
||||
cli = docker.Client(base_url=app.config['DOCKER_SOCKET'])
|
||||
|
||||
|
||||
def get(*names):
|
||||
result = {}
|
||||
all_containers = cli.containers(all=True)
|
||||
for brief in all_containers:
|
||||
if brief['Image'].startswith('mailu/'):
|
||||
container = cli.inspect_container(brief['Id'])
|
||||
container['Image'] = cli.inspect_image(container['Image'])
|
||||
name = container['Config']['Labels']['com.docker.compose.service']
|
||||
if not names or name in names:
|
||||
result[name] = container
|
||||
return result
|
||||
|
||||
|
||||
def reload(*names):
|
||||
for name, container in get(*names).items():
|
||||
cli.kill(container["Id"], signal.SIGHUP.value)
|
@ -0,0 +1,53 @@
|
||||
from mailu import models
|
||||
|
||||
import flask
|
||||
import flask_login
|
||||
import flask_script
|
||||
import flask_migrate
|
||||
import flask_babel
|
||||
import flask_limiter
|
||||
|
||||
from werkzeug.contrib import fixers
|
||||
|
||||
|
||||
# Login configuration
|
||||
login = flask_login.LoginManager()
|
||||
login.login_view = "ui.login"
|
||||
|
||||
@login.unauthorized_handler
|
||||
def handle_needs_login():
|
||||
return flask.redirect(
|
||||
flask.url_for('ui.login', next=flask.request.endpoint)
|
||||
)
|
||||
|
||||
|
||||
# Request rate limitation
|
||||
limiter = flask_limiter.Limiter(key_func=lambda: current_user.username)
|
||||
|
||||
|
||||
# Application translation
|
||||
babel = flask_babel.Babel()
|
||||
|
||||
@babel.localeselector
|
||||
def get_locale():
|
||||
translations = list(map(str, babel.list_translations()))
|
||||
return flask.request.accept_languages.best_match(translations)
|
||||
|
||||
|
||||
# Proxy fixer
|
||||
class PrefixMiddleware(object):
|
||||
def __call__(self, environ, start_response):
|
||||
prefix = environ.get('HTTP_X_FORWARDED_PREFIX', '')
|
||||
if prefix:
|
||||
environ['SCRIPT_NAME'] = prefix
|
||||
return self.app(environ, start_response)
|
||||
|
||||
def init_app(self, app):
|
||||
self.app = fixers.ProxyFix(app.wsgi_app)
|
||||
app.wsgi_app = self
|
||||
|
||||
proxy = PrefixMiddleware()
|
||||
|
||||
|
||||
# Data migrate
|
||||
migrate = flask_migrate.Migrate()
|
@ -0,0 +1,25 @@
|
||||
""" Add a configuration table
|
||||
|
||||
Revision ID: cd79ed46d9c2
|
||||
Revises: 25fd6c7bcb4a
|
||||
Create Date: 2018-10-17 21:44:48.924921
|
||||
|
||||
"""
|
||||
|
||||
revision = 'cd79ed46d9c2'
|
||||
down_revision = '3b281286c7bd'
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table('config',
|
||||
sa.Column('name', sa.String(length=255), nullable=False),
|
||||
sa.Column('value', sa.String(length=255), nullable=True),
|
||||
sa.PrimaryKeyConstraint('name')
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('config')
|
@ -1,53 +1,46 @@
|
||||
alembic==0.9.9
|
||||
alembic==1.0.2
|
||||
asn1crypto==0.24.0
|
||||
Babel==2.5.3
|
||||
Babel==2.6.0
|
||||
bcrypt==3.1.4
|
||||
blinker==1.4
|
||||
certifi==2018.4.16
|
||||
cffi==1.11.5
|
||||
chardet==3.0.4
|
||||
click==6.7
|
||||
cryptography==2.2.2
|
||||
Click==7.0
|
||||
cryptography==2.3.1
|
||||
decorator==4.3.0
|
||||
dnspython==1.15.0
|
||||
docker-py==1.10.6
|
||||
docker-pycreds==0.2.2
|
||||
dominate==2.3.1
|
||||
Flask==0.12.2
|
||||
Flask-Babel==0.11.2
|
||||
dominate==2.3.4
|
||||
Flask==1.0.2
|
||||
Flask-Babel==0.12.2
|
||||
Flask-Bootstrap==3.3.7.1
|
||||
Flask-DebugToolbar==0.10.1
|
||||
Flask-Limiter==1.0.1
|
||||
Flask-Login==0.4.1
|
||||
Flask-Migrate==2.1.1
|
||||
Flask-Migrate==2.3.0
|
||||
Flask-Script==2.0.6
|
||||
Flask-SQLAlchemy==2.3.2
|
||||
Flask-WTF==0.14.2
|
||||
gunicorn==19.7.1
|
||||
idna==2.6
|
||||
gunicorn==19.9.0
|
||||
idna==2.7
|
||||
infinity==1.4
|
||||
intervals==0.8.1
|
||||
itsdangerous==0.24
|
||||
itsdangerous==1.1.0
|
||||
Jinja2==2.10
|
||||
limits==1.3
|
||||
Mako==1.0.7
|
||||
MarkupSafe==1.0
|
||||
MarkupSafe==1.1.0
|
||||
passlib==1.7.1
|
||||
pycparser==2.18
|
||||
pyOpenSSL==17.5.0
|
||||
python-dateutil==2.7.2
|
||||
pycparser==2.19
|
||||
pyOpenSSL==18.0.0
|
||||
python-dateutil==2.7.5
|
||||
python-editor==1.0.3
|
||||
pytz==2018.4
|
||||
PyYAML==3.12
|
||||
pytz==2018.7
|
||||
PyYAML==3.13
|
||||
redis==2.10.6
|
||||
requests==2.18.4
|
||||
six==1.11.0
|
||||
SQLAlchemy==1.2.6
|
||||
SQLAlchemy==1.2.13
|
||||
tabulate==0.8.2
|
||||
urllib3==1.22
|
||||
validators==0.12.1
|
||||
validators==0.12.2
|
||||
visitor==0.1.3
|
||||
websocket-client==0.47.0
|
||||
Werkzeug==0.14.1
|
||||
WTForms==2.1
|
||||
WTForms==2.2.1
|
||||
WTForms-Components==0.10.3
|
||||
|
@ -1,7 +0,0 @@
|
||||
import os
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.environ["DEBUG"] = "True"
|
||||
from mailu import app
|
||||
app.run()
|
Loading…
Reference in New Issue