forked from cs50/cs50.readthedocs.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
41 lines (36 loc) · 1.14 KB
/
application.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
import os
from flask import Flask, abort, redirect, request, send_file
from raven.contrib.flask import Sentry
# application
app = Flask(__name__)
# monitoring
sentry = Sentry(app)
# path to _site
_site = os.path.realpath("_site")
# GET /*
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def index(path):
p = os.path.realpath(os.path.join(_site, path))
if not p.startswith(_site):
abort(403)
if os.path.isfile(p):
basename = os.path.basename(path)
if basename == "index.html":
return redirect(os.path.dirname(path))
elif basename.endswith(".html"):
return redirect(os.path.splitext(path)[0])
else:
return send_file(p)
elif os.path.isfile(p + ".html") and os.path.basename(p) != "index":
return send_file(p + ".html")
elif os.path.isdir(p):
if os.path.isfile(os.path.join(p, "index.html")):
if path.endswith("/") and path != "/":
return redirect(path[:-1])
else:
return send_file(os.path.join(p, "index.html"))
else:
abort(403)
else:
abort(404)