-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
66 lines (54 loc) · 1.82 KB
/
app.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
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
from pathlib import Path
import archiver
from pprint import pprint
app = Flask(__name__)
CORS(app)
class LoggingMiddleware(object):
def __init__(self, app):
self._app = app
def __call__(self, env, resp):
errorlog = env['wsgi.errors']
pprint(('REQUEST', env), stream=errorlog)
def log_response(status, headers, *args):
pprint(('RESPONSE', status, headers), stream=errorlog)
return resp(status, headers, *args)
return self._app(env, log_response)
@app.route("/")
def homepage():
logs_path = Path("logs", "archiver.log")
if not logs_path.exists():
return render_template("homepage.html", data=None)
with Path("logs", "archiver.log").open(mode="r") as f:
lines = f.read().splitlines()
data = []
for line in lines:
columns = [col.strip() for col in line.split(" - ") if col]
data.append(columns)
return render_template("homepage.html", data=data)
@app.post("/archive")
def archive_post():
values = request.get_json()
if not values:
response = {
"message": "No data found."
}
return jsonify(response), 400
if "url" not in values:
response = {
"message": "No URL data found."
}
return jsonify(response), 400
archive_url = values.get("url")
if archive_url == "about:blank":
response = {
"message": "URL is about:blank"
}
return jsonify(response), 400
archiver.archive_url(archive_url)
return f"Archived url at {archive_url}"
if __name__ == "__main__":
# app.wsgi_app = LoggingMiddleware(app.wsgi_app)
app.run(debug=True, host="127.0.0.1", port=5000)
# app.run(debug=True, host="192.168.56.1",)