|
8 | 8 | render_template,
|
9 | 9 | request,
|
10 | 10 | current_app,
|
| 11 | + url_for, |
11 | 12 | abort,
|
12 | 13 | )
|
13 | 14 | from flask_wtf.csrf import CSRFError
|
@@ -46,6 +47,18 @@ def set_locale(locale):
|
46 | 47 | return response
|
47 | 48 |
|
48 | 49 |
|
| 50 | +@bp.route("/status", methods=["GET"]) |
| 51 | +def status(): |
| 52 | + return "OK" |
| 53 | + |
| 54 | + |
| 55 | +@bp.route("/service-unavailable", methods=["GET"]) |
| 56 | +def service_unavailable_page(): |
| 57 | + if not current_app.config["SERVICE_UNAVAILABLE"]: |
| 58 | + return redirect(url_for("main.index")) |
| 59 | + abort(503) |
| 60 | + |
| 61 | + |
49 | 62 | @bp.route("/accessibility", methods=["GET"])
|
50 | 63 | def accessibility():
|
51 | 64 | return render_template("accessibility.html")
|
@@ -100,3 +113,22 @@ def http_exception(error):
|
100 | 113 | def csrf_error(error):
|
101 | 114 | flash("The form you were submitting has expired. Please try again.")
|
102 | 115 | return redirect(request.full_path)
|
| 116 | + |
| 117 | + |
| 118 | +@bp.before_request |
| 119 | +def service_unavailable_middleware(): |
| 120 | + if not current_app.config["SERVICE_UNAVAILABLE"]: |
| 121 | + return |
| 122 | + |
| 123 | + service_unavailable_url = url_for("main.service_unavailable_page") |
| 124 | + exempt_urls = [ |
| 125 | + service_unavailable_url, |
| 126 | + url_for("main.status"), |
| 127 | + url_for("main.cookies"), |
| 128 | + url_for("main.accessibility"), |
| 129 | + url_for("main.privacy"), |
| 130 | + url_for("main.set_locale", locale="en"), |
| 131 | + url_for("main.set_locale", locale="cy"), |
| 132 | + ] |
| 133 | + if request.path not in exempt_urls: |
| 134 | + return redirect(service_unavailable_url) |
0 commit comments