1
0
mirror of https://github.com/Mailu/Mailu.git synced 2024-12-12 10:45:38 +02:00
Mailu/setup/server.py

105 lines
3.0 KiB
Python
Raw Normal View History

import flask
import flask_bootstrap
import redis
import json
import os
import jinja2
import uuid
2018-04-22 11:54:10 +02:00
import string
import random
import ipaddress
app = flask.Flask(__name__)
flask_bootstrap.Bootstrap(app)
db = redis.StrictRedis(host='redis', port=6379, db=0)
2018-04-22 11:54:10 +02:00
def render_flavor(flavor, template, data):
return flask.render_template(
os.path.join(flavor, template),
2018-04-22 11:54:10 +02:00
**data
)
2018-04-22 16:08:43 +02:00
@app.add_template_global
2018-04-22 11:54:10 +02:00
def secret(length=16):
charset = string.ascii_uppercase + string.digits
return ''.join(
random.SystemRandom().choice(charset)
for _ in range(length)
)
2018-04-22 16:08:43 +02:00
def build_app(path):
2018-10-16 11:34:55 +02:00
#Hardcoded master as the only version for test purposes
2018-04-22 16:08:43 +02:00
versions = [
2018-10-16 11:34:55 +02:00
# version for version in os.listdir(path)
# if os.path.isdir(os.path.join(path, version))
"master"
2018-04-22 16:08:43 +02:00
]
2018-04-22 11:54:10 +02:00
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
2018-04-22 16:08:43 +02:00
@app.context_processor
def app_context():
return dict(versions=versions)
2018-04-22 16:08:43 +02:00
@app.route("/")
def index():
return flask.redirect(flask.url_for('{}.wizard'.format(versions[-1])))
2018-04-22 16:08:43 +02:00
for version in versions:
2018-03-10 16:25:52 +02:00
bp = flask.Blueprint(version, __name__)
bp.jinja_loader = jinja2.ChoiceLoader([
2018-04-22 16:08:43 +02:00
jinja2.FileSystemLoader(os.path.join(path, version, "templates")),
jinja2.FileSystemLoader(os.path.join(path, version, "flavors"))
2018-03-10 16:25:52 +02:00
])
2018-04-22 16:08:43 +02:00
@bp.context_processor
def bp_context(version=version):
return dict(version=version)
2018-03-10 16:25:52 +02:00
@bp.route("/")
def wizard():
return flask.render_template('wizard.html')
2018-10-18 11:56:16 +02:00
@bp.route("/submit_flavor", methods=["POST"])
def submit_flavor():
data = flask.request.form.copy()
2018-10-19 10:45:55 +02:00
steps = sorted(os.listdir(path + "/" + version + "/templates/steps/" + data["flavor"]))
return flask.render_template('wizard.html', flavor=data["flavor"], steps=steps)
2018-10-18 11:56:16 +02:00
@bp.route("/submit", methods=["POST"])
def submit():
2018-04-22 11:54:10 +02:00
data = flask.request.form.copy()
2018-04-22 16:08:43 +02:00
data['uid'] = str(uuid.uuid4())
data['dns'] = str(ipaddress.IPv4Network(data['subnet'])[-2])
2018-04-22 16:08:43 +02:00
db.set(data['uid'], json.dumps(data))
return flask.redirect(flask.url_for('.setup', uid=data['uid']))
@bp.route("/setup/<uid>", methods=["GET"])
def setup(uid):
data = json.loads(db.get(uid))
flavor = data.get("flavor", "compose")
2018-04-22 11:54:10 +02:00
rendered = render_flavor(flavor, "setup.html", data)
2018-03-10 16:25:52 +02:00
return flask.render_template("setup.html", contents=rendered)
@bp.route("/file/<uid>/<filepath>", methods=["GET"])
def file(uid, filepath):
data = json.loads(db.get(uid))
flavor = data.get("flavor", "compose")
return flask.Response(
2018-04-22 11:54:10 +02:00
render_flavor(flavor, filepath, data),
mimetype="application/text"
)
2018-03-10 16:25:52 +02:00
app.register_blueprint(bp, url_prefix="/{}".format(version))
if __name__ == "__main__":
2018-03-10 16:25:52 +02:00
build_app("/tmp/mailutest")
app.run(debug=True)