Skip to content

Commit

Permalink
use Flask app factory create_app
Browse files Browse the repository at this point in the history
  • Loading branch information
HLFH committed Nov 16, 2022
1 parent 0770261 commit 5158b6e
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion alembic/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ DBURI = sqlite:////Users/hlfh/projects/automua/local/dev.sqlite
# Short message used when generating a new revision
MIGMSG = DAV server support
# Environment variables
FLASKDB = PYTHONPATH=.. FLASK_APP=automua.server:app flask db
FLASKDB = PYTHONPATH=.. FLASK_APP=automua.server:create_app flask db
# Shared Alembic options
ALOPTS = -d .

Expand Down
32 changes: 16 additions & 16 deletions automua/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
automua™ is a trademark of "Gaspard d'Hautefeuille" and may not be used
by third parties without the prior written permission of the author.
Copyright © 2022 Gaspard d'Hautefeuille: use Flask app factory create_app
Copyright © 2019-2022 Ralph Seichter
This file is part of automua.
Expand Down Expand Up @@ -37,8 +38,18 @@
MSOFT_ALTERNATE_ROUTE = '/AutoDiscover/AutoDiscover.xml'
MSOFT_CONFIG_ROUTE = '/autodiscover/autodiscover.xml'


def _proxy_fix():
def create_app():
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = config.db_uri()
app.config['SQLALCHEMY_ECHO'] = config.db_echo()
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.add_url_rule('/', view_func=SiteRoot.as_view('root'), methods=['GET'])
app.add_url_rule(APPLE_CONFIG_ROUTE, view_func=mobileconfig.AppleView.as_view('apple'), methods=['GET'])
app.add_url_rule(INITDB_ROUTE, view_func=InitDatabase.as_view('initdb'), methods=['DELETE', 'GET', 'POST'])
app.add_url_rule(MOZILLA_CONFIG_ROUTE, view_func=autoconfig.MozillaView.as_view('mozilla'), methods=['GET'])
app.add_url_rule(MSOFT_ALTERNATE_ROUTE, view_func=autodiscover.OutlookView.as_view('ms2'), methods=['POST'])
app.add_url_rule(MSOFT_CONFIG_ROUTE, view_func=autodiscover.OutlookView.as_view('ms1'), methods=['POST'])

"""Use a fix for Werkzeug if automua is running behind a proxy.
This enables support for X-Forwarded-* headers.
"""
Expand All @@ -47,18 +58,7 @@ def _proxy_fix():
# See https://werkzeug.palletsprojects.com/en/0.15.x/middleware/proxy_fix/
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=p, x_host=p, x_port=p, x_prefix=p, x_proto=p)

db.init_app(app)
migrate = Migrate(app, db)

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = config.db_uri()
app.config['SQLALCHEMY_ECHO'] = config.db_echo()
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.add_url_rule('/', view_func=SiteRoot.as_view('root'), methods=['GET'])
app.add_url_rule(APPLE_CONFIG_ROUTE, view_func=mobileconfig.AppleView.as_view('apple'), methods=['GET'])
app.add_url_rule(INITDB_ROUTE, view_func=InitDatabase.as_view('initdb'), methods=['DELETE', 'GET', 'POST'])
app.add_url_rule(MOZILLA_CONFIG_ROUTE, view_func=autoconfig.MozillaView.as_view('mozilla'), methods=['GET'])
app.add_url_rule(MSOFT_ALTERNATE_ROUTE, view_func=autodiscover.OutlookView.as_view('ms2'), methods=['POST'])
app.add_url_rule(MSOFT_CONFIG_ROUTE, view_func=autodiscover.OutlookView.as_view('ms1'), methods=['POST'])
_proxy_fix()

db.init_app(app)
migrate = Migrate(app, db)
return app
2 changes: 1 addition & 1 deletion contrib/OpenRC/init_d
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ depend() {
start_pre() {
export AUTOMUA_CONF
export EPYTHON="python3.9"
export FLASK_APP="automua.server:app"
export FLASK_APP="automua.server:create_app"
export FLASK_ENV="production"
}
2 changes: 1 addition & 1 deletion contrib/automua.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Description=Auto MUA configuration service
Documentation=https://hlfh.github.io/automua/

[Service]
Environment=FLASK_APP=automua.server:app
Environment=FLASK_APP=automua.server:create_app
Environment=FLASK_CONFIG=production
ExecStart=/srv/http/automua/.venv/bin/python /srv/http/automua/.venv/bin/flask run --host=127.0.0.1 --port=4243
Restart=always
Expand Down
2 changes: 1 addition & 1 deletion contrib/flask.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export FLASK_CONFIG='production'

# User configurable section -- END

export FLASK_APP='automua.server:app'
export FLASK_APP='automua.server:create_app'
flask "$@"
Binary file modified docs/automua.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/dbupgrade.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ You should see output similar to the following:
[source]
[.small]
----
PYTHONPATH=.. FLASK_APP=automua.server:app flask db upgrade -d .
PYTHONPATH=.. FLASK_APP=automua.server:create_app flask db upgrade -d .
Running automua version 2021.6
INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
Expand Down
14 changes: 7 additions & 7 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,8 @@
<div id="header">
<h1>automua: Email client configuration made easy</h1>
<div class="details">
<span id="revnumber">version 2022.6.8,</span>
<span id="revdate">2022-11-08</span>
<span id="revnumber">version 2022.7,</span>
<span id="revdate">2022-11-10</span>
</div>
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
Expand Down Expand Up @@ -1345,7 +1345,7 @@ <h3 id="_alembic"><a class="anchor" href="#_alembic"></a>7.5. Alembic</h3>
</div>
<div class="listingblock small">
<div class="content">
<pre class="rouge highlight"><code>PYTHONPATH=.. FLASK_APP=automua.server:app flask db upgrade -d .
<pre class="rouge highlight"><code>PYTHONPATH=.. FLASK_APP=automua.server:create_app flask db upgrade -d .
Running automua version 2021.6
INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
Expand Down Expand Up @@ -1475,7 +1475,7 @@ <h3 id="_as_a_openrc_service"><a class="anchor" href="#_as_a_openrc_service"></a
start_pre<span class="o">()</span> <span class="o">{</span>
<span class="nb">export </span>AUTOMUA_CONF
<span class="nb">export </span><span class="nv">EPYTHON</span><span class="o">=</span><span class="s2">"python3.9"</span>
<span class="nb">export </span><span class="nv">FLASK_APP</span><span class="o">=</span><span class="s2">"automua.server:app"</span>
<span class="nb">export </span><span class="nv">FLASK_APP</span><span class="o">=</span><span class="s2">"automua.server:create_app"</span>
<span class="nb">export </span><span class="nv">FLASK_ENV</span><span class="o">=</span><span class="s2">"production"</span>
<span class="o">}</span></code></pre>
</div>
Expand Down Expand Up @@ -1512,7 +1512,7 @@ <h3 id="_as_a_systemd_service"><a class="anchor" href="#_as_a_systemd_service"><
<span class="py">Documentation</span><span class="p">=</span><span class="s">https://hlfh.github.io/automua/</span>

<span class="nn">[Service]</span>
<span class="py">Environment</span><span class="p">=</span><span class="s">FLASK_APP=automua.server:app</span>
<span class="py">Environment</span><span class="p">=</span><span class="s">FLASK_APP=automua.server:create_app</span>
<span class="py">Environment</span><span class="p">=</span><span class="s">FLASK_CONFIG=production</span>
<span class="py">ExecStart</span><span class="p">=</span><span class="s">/srv/http/automua/.venv/bin/python /srv/http/automua/.venv/bin/flask run --host=127.0.0.1 --port=4243</span>
<span class="py">Restart</span><span class="p">=</span><span class="s">always</span>
Expand Down Expand Up @@ -1781,8 +1781,8 @@ <h2 id="sponsorship"><a class="anchor" href="#sponsorship"></a>12. Sponsorship</
</div>
<div id="footer">
<div id="footer-text">
Version 2022.6.8<br>
Last updated 2022-11-08 11:11:07 UTC
Version 2022.7<br>
Last updated 2022-11-10 20:45:37 UTC
</div>
</div>
</body>
Expand Down
3 changes: 2 additions & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
automua™ is a trademark of "Gaspard d'Hautefeuille" and may not be used
by third parties without the prior written permission of the author.
Copyright © 2022 Gaspard d'Hautefeuille: use Flask app factory create_app
Copyright © 2019-2022 Ralph Seichter
This file is part of automua.
Expand Down Expand Up @@ -36,7 +37,7 @@
from automua.server import APPLE_CONFIG_ROUTE
from automua.server import MOZILLA_CONFIG_ROUTE
from automua.server import MSOFT_CONFIG_ROUTE
from automua.server import app
from automua.server import create_app as app
from automua.util import from_environ
from automua.views import CONTENT_TYPE_XML
from automua.views import EMAIL_MOZILLA
Expand Down

0 comments on commit 5158b6e

Please sign in to comment.