2016-06-25 12:57:47 +02:00
|
|
|
import flask
|
|
|
|
import flask_sqlalchemy
|
|
|
|
import flask_bootstrap
|
|
|
|
import flask_login
|
|
|
|
import flask_script
|
|
|
|
import flask_migrate
|
2016-10-02 14:23:44 +02:00
|
|
|
import flask_babel
|
2016-02-20 14:57:26 +02:00
|
|
|
|
2016-02-20 21:11:59 +02:00
|
|
|
import os
|
2016-04-20 22:37:17 +02:00
|
|
|
import docker
|
2016-02-20 14:57:26 +02:00
|
|
|
|
2016-11-06 18:00:10 +02:00
|
|
|
from apscheduler.schedulers import background
|
|
|
|
|
2016-02-20 14:57:26 +02:00
|
|
|
|
|
|
|
# Create application
|
2016-08-13 20:39:36 +02:00
|
|
|
app = flask.Flask(__name__, static_url_path='/admin/app_static')
|
2016-02-20 14:57:26 +02:00
|
|
|
|
2016-02-20 21:11:59 +02:00
|
|
|
default_config = {
|
2016-10-29 13:42:39 +02:00
|
|
|
'SQLALCHEMY_DATABASE_URI': 'sqlite:////data/main.db',
|
2016-02-20 21:11:59 +02:00
|
|
|
'SQLALCHEMY_TRACK_MODIFICATIONS': False,
|
2016-04-20 22:37:17 +02:00
|
|
|
'SECRET_KEY': 'changeMe',
|
|
|
|
'DOCKER_SOCKET': 'unix:///var/run/docker.sock',
|
2016-10-29 13:42:39 +02:00
|
|
|
'HOSTNAME': 'mail.mailu.io',
|
|
|
|
'DOMAIN': 'mailu.io',
|
2016-06-25 14:51:02 +02:00
|
|
|
'POSTMASTER': 'postmaster',
|
2016-06-25 15:50:05 +02:00
|
|
|
'DEBUG': False,
|
2016-08-13 20:39:36 +02:00
|
|
|
'BOOTSTRAP_SERVE_LOCAL': True,
|
2016-06-25 15:50:05 +02:00
|
|
|
'DKIM_PATH': '/dkim/{domain}.{selector}.key',
|
2016-10-02 14:23:44 +02:00
|
|
|
'DKIM_SELECTOR': 'dkim',
|
2017-09-10 19:35:43 +02:00
|
|
|
'DMARC_RUA': None,
|
|
|
|
'DMARC_RUF': None,
|
2016-10-02 14:23:44 +02:00
|
|
|
'BABEL_DEFAULT_LOCALE': 'en',
|
2016-11-09 17:18:16 +02:00
|
|
|
'BABEL_DEFAULT_TIMEZONE': 'UTC',
|
2017-09-10 18:53:41 +02:00
|
|
|
'FRONTEND': 'none',
|
|
|
|
'TLS_FLAVOR': 'cert',
|
2017-09-02 17:36:22 +02:00
|
|
|
'CERTS_PATH': '/certs',
|
|
|
|
'PASSWORD_SCHEME': 'SHA512-CRYPT'
|
2016-02-20 21:11:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Load configuration from the environment if available
|
|
|
|
for key, value in default_config.items():
|
|
|
|
app.config[key] = os.environ.get(key, value)
|
|
|
|
|
2017-09-17 14:36:44 +02:00
|
|
|
# Base application
|
2016-06-25 12:57:47 +02:00
|
|
|
flask_bootstrap.Bootstrap(app)
|
|
|
|
db = flask_sqlalchemy.SQLAlchemy(app)
|
|
|
|
migrate = flask_migrate.Migrate(app, db)
|
2016-03-19 21:37:48 +02:00
|
|
|
|
2016-06-25 12:57:47 +02:00
|
|
|
# Manager commnad
|
|
|
|
manager = flask_script.Manager(app)
|
|
|
|
manager.add_command('db', flask_migrate.MigrateCommand)
|
|
|
|
|
2016-11-06 18:00:10 +02:00
|
|
|
# Task scheduling
|
2017-09-17 14:36:44 +02:00
|
|
|
scheduler = background.BackgroundScheduler({
|
|
|
|
'apscheduler.timezone': 'UTC'
|
|
|
|
})
|
2016-11-06 18:00:10 +02:00
|
|
|
if not app.debug or os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
|
|
|
|
scheduler.start()
|
2017-09-10 18:53:41 +02:00
|
|
|
from mailu import tlstasks
|
2016-11-06 18:00:10 +02:00
|
|
|
|
2016-11-06 13:48:24 +02:00
|
|
|
# Babel configuration
|
2017-09-17 14:36:44 +02:00
|
|
|
babel = flask_babel.Babel(app)
|
|
|
|
translations = list(map(str, babel.list_translations()))
|
|
|
|
|
2016-11-06 13:48:24 +02:00
|
|
|
@babel.localeselector
|
|
|
|
def get_locale():
|
|
|
|
return flask.request.accept_languages.best_match(translations)
|
|
|
|
|
2017-09-17 14:36:44 +02:00
|
|
|
# Login configuration
|
|
|
|
login_manager = flask_login.LoginManager()
|
|
|
|
login_manager.init_app(app)
|
2017-09-17 14:37:10 +02:00
|
|
|
login_manager.login_view = ".login"
|
2017-09-17 14:36:44 +02:00
|
|
|
|
|
|
|
@app.context_processor
|
|
|
|
def inject_user():
|
|
|
|
return dict(current_user=flask_login.current_user)
|
2016-08-21 15:01:07 +02:00
|
|
|
|
2017-09-17 14:36:44 +02:00
|
|
|
# Import views
|
|
|
|
from mailu.views import *
|