-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
102 lines (75 loc) · 2.35 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# tullinge/booking
# https://github.com/tullinge/booking
from os import environ
from flask import Flask, render_template
from flask_minify import minify
from datetime import timedelta
from time import strftime
from version import version, commit_hash
# import session
from flask_session.__init__ import Session
# import limiter
from components.limiter_obj import limiter
# import blueprints
from routes.admin import admin_routes
from routes.student import student_routes
from routes.activity_leader import activity_leader_routes
# components
from components.db import dict_sql_query
# variables
from components.google import GOOGLE_CLIENT_ID, GSUITE_DOMAIN_NAME
# redis
import redis
# flask application
app = Flask(__name__)
# setup rate limit
app.config[
"RATELIMIT_STORAGE_URL"
] = f"redis://{environ.get('REDIS_HOST', 'localhost')}"
limiter.init_app(app)
# minify
minify(app=app)
# variables available across all templates
@app.context_processor
def inject_global_variables():
return dict(
version=version,
commit_hash=commit_hash[0:7],
generation_time=strftime("%Y-%m-%d %H:%M:%S"),
GOOGLE_CLIENT_ID=GOOGLE_CLIENT_ID,
GSUITE_DOMAIN_NAME=GSUITE_DOMAIN_NAME,
BOOKING_LOCKED=dict_sql_query(
"SELECT value FROM settings WHERE identifier='booking_locked'",
fetchone=True,
)["value"],
)
# error routes
@app.errorhandler(400)
def error_400(e):
return render_template("errors/400.html"), 400
@app.errorhandler(404)
def error_404(e):
return render_template("errors/404.html"), 404
@app.errorhandler(405)
def error_405(e):
return render_template("errors/405.html"), 405
@app.errorhandler(429)
def error_429(e):
return render_template("errors/429.html"), 429
@app.errorhandler(500)
def error_500(e):
return render_template("errors/500.html"), 500
# session setup
SESSION_TYPE = "redis"
SESSION_REDIS = redis.Redis(host=environ.get("REDIS_HOST", "localhost"), db=0)
SESSION_COOKIE_SECURE = True
SESSION_PERMANENT = True
PERMANENT_SESSION_LIFETIME = timedelta(hours=24)
app.config.from_object(__name__)
Session(app)
# register blueprints
app.register_blueprint(admin_routes, url_prefix="/admin")
app.register_blueprint(student_routes, url_prefix="/")
app.register_blueprint(activity_leader_routes, url_prefix="/leader")
if __name__ == "__main__":
app.run(host="0.0.0.0")