Merge pull request #262 from Mailu/feature-xforwarded-prefix

Stop hardcoding URL prefix in containers
master
kaiyou 7 years ago committed by GitHub
commit 914427f852

@ -34,43 +34,66 @@ default_config = {
'FRONTEND': 'none', 'FRONTEND': 'none',
'TLS_FLAVOR': 'cert', 'TLS_FLAVOR': 'cert',
'CERTS_PATH': '/certs', 'CERTS_PATH': '/certs',
'PASSWORD_SCHEME': 'SHA512-CRYPT' 'PASSWORD_SCHEME': 'SHA512-CRYPT',
} }
# Load configuration from the environment if available # Load configuration from the environment if available
for key, value in default_config.items(): for key, value in default_config.items():
app.config[key] = os.environ.get(key, value) app.config[key] = os.environ.get(key, value)
# Setup components # Base application
flask_bootstrap.Bootstrap(app) flask_bootstrap.Bootstrap(app)
db = flask_sqlalchemy.SQLAlchemy(app) db = flask_sqlalchemy.SQLAlchemy(app)
migrate = flask_migrate.Migrate(app, db) migrate = flask_migrate.Migrate(app, db)
login_manager = flask_login.LoginManager()
login_manager.init_app(app)
babel = flask_babel.Babel(app)
translations = list(map(str, babel.list_translations()))
scheduler = background.BackgroundScheduler({
'apscheduler.timezone': 'UTC'
})
# Manager commnad # Manager commnad
manager = flask_script.Manager(app) manager = flask_script.Manager(app)
manager.add_command('db', flask_migrate.MigrateCommand) manager.add_command('db', flask_migrate.MigrateCommand)
# Task scheduling # Task scheduling
scheduler = background.BackgroundScheduler({
'apscheduler.timezone': 'UTC'
})
if not app.debug or os.environ.get('WERKZEUG_RUN_MAIN') == 'true': if not app.debug or os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
scheduler.start() scheduler.start()
from mailu import tlstasks from mailu import tlstasks
# Babel configuration # Babel configuration
babel = flask_babel.Babel(app)
translations = list(map(str, babel.list_translations()))
@babel.localeselector @babel.localeselector
def get_locale(): def get_locale():
return flask.request.accept_languages.best_match(translations) return flask.request.accept_languages.best_match(translations)
# Finally setup the blueprint and redirect / # Login configuration
from mailu import admin login_manager = flask_login.LoginManager()
app.register_blueprint(admin.app, url_prefix='/admin') login_manager.init_app(app)
login_manager.login_view = ".login"
@app.route("/") @login_manager.unauthorized_handler
def index(): def handle_needs_login():
return flask.redirect("/webmail/") return flask.redirect(
flask.url_for('.login', next=flask.request.endpoint)
)
@app.context_processor
def inject_user():
return dict(current_user=flask_login.current_user)
# Import views
from mailu.views import *
# 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)
app.wsgi_app = PrefixMiddleware(app.wsgi_app)

@ -1,4 +1,4 @@
from mailu.admin import db, models, forms from mailu import db, models, forms
import flask import flask
import flask_login import flask_login

@ -1,33 +0,0 @@
from flask import Blueprint
from mailu import login_manager, db
import flask_login
app = Blueprint(
'admin', __name__,
template_folder='templates',
static_folder='static')
# Import models
from mailu.admin import models
# Register the login components
login_manager.login_view = "admin.login"
login_manager.user_loader(models.User.query.get)
@app.context_processor
def inject_user():
return dict(current_user=flask_login.current_user)
# Import views
from mailu.admin.views import \
admins, \
managers, \
base, \
aliases, \
users, \
domains, \
relays, \
alternatives, \
fetches

@ -1,5 +1,4 @@
from mailu.admin import db, dkim from mailu import app, db, dkim, login_manager
from mailu import app
from sqlalchemy.ext import declarative from sqlalchemy.ext import declarative
from passlib import context from passlib import context
@ -236,6 +235,8 @@ class User(Base, Email):
user = cls.query.get(email) user = cls.query.get(email)
return user if (user and user.check_password(password)) else None return user if (user and user.check_password(password)) else None
login_manager.user_loader(User.query.get)
class Alias(Base, Email): class Alias(Base, Email):
""" An alias is an email address that redirects to some destination. """ An alias is an email address that redirects to some destination.

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save