-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.py
61 lines (50 loc) · 1.97 KB
/
webserver.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
from flask import Flask, request, render_template, redirect
from re import match
import time
import uuid
import bleach
VERSION = "1.0.0"
def is_valid_uuid(val):
try:
uuid.UUID(str(val))
return True
except ValueError:
return False
def getWebServer(taskQueue):
# create and configure the app
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def index():
if request.method == "POST":
url = request.form['url']
if not url:
return render_template('index.html', time=time.strftime("%H:%M"), status="Error: URL was not provided.", version=VERSION)
if not match(r"(http|https):\/\/.*", url):
return render_template('index.html', time=time.strftime("%H:%M"), status="Error: URL is invalid.", version=VERSION)
task = taskQueue.add(url)
return redirect(f'/tasks/{task.id}', 303)
elif request.method == "GET":
return render_template('index.html', time=time.strftime("%H:%M"), version=VERSION)
@app.route("/status")
def status():
return render_template('status.html', version=VERSION)
@app.route("/tasks/<taskID>")
def taskStatus(taskID):
if not is_valid_uuid(taskID):
return redirect("/status", 302)
status = taskQueue.status(taskID)
if status == "not found":
return redirect("/status", 302)
return render_template('task.html', id=str(taskID), status=status.capitalize(), version=VERSION)
@app.route("/api/tasks/status/<taskID>")
def getTaskStatus(taskID):
if not is_valid_uuid(taskID):
return {"status": "error"}
status = taskQueue.status(taskID)
# Since the taskID is user input, we need to sanitize and escape it.
# Auto escaping is turned on for jinja2 templates, but not for REST routes.
return {
"id": bleach.clean(taskID),
"status": status
}
return app