-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
120 lines (103 loc) · 3.8 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify, abort, send_from_directory
import os
from json import load
from werkzeug.exceptions import HTTPException
from jinja2 import TemplateNotFound
import logging, time
from flask_compress import Compress
logging.basicConfig(filename="logs.log", level=logging.DEBUG)
logging.basicConfig(format='%(asctime)s %(message)s\n', datefmt='%m/%d/%Y %I:%M:%S %p')
# load config
with open('settings.json') as config_file:
config = load(config_file)
app = Flask(__name__, root_path=os.path.dirname(os.path.abspath(__file__)))
Compress(app)
app.config['COMPRESS_ALGORITHM'] = config['compression-algorithm']
# inject headers defined in config
@app.after_request
def add_headers(r):
"""
Adds headers from settings.json's http-headers
"""
for header, value in config['http-headers'].items():
r.headers[header] = value
return r
@app.route('/')
def index():
return render_template('index.html')
@app.route('/returnerror/<errorcode>')
def returnerror(errorcode):
try:
abort(int(errorcode))
except LookupError:
return 'LookupError while processing request', 500
@app.route('/throwdebuggingexception')
def throwdebugexception():
if app.debug:
raise Exception
else:
return 'app is not debuggable, as debug is not true'
@app.route('/teapot')
def teapot():
return """<!DOCTYPE html>
<h3 style="text-align: center">I'm a little teapot<br>
Short and stout<br>
Here is my handle<br>
Here is my spout<br>
When I get all steamed up<br>
I just shout<br>
Tip me over and pour me out<br>
I'm a very special pot<br>
It's true<br>
Here's an example of what I can do<br>
I can turn my handle into a spout<br>
Tip me over and pour me out<br>
I'm a little teapot<br>
Short and stout<br>
Here is my handle<br>
Here is my spout<br>
When I get all steamed up<br>
I just shout<br>
Tip me over and pour me out<br>
I'm a very special pot<br>
It's true<br>
Here's an example of what I can do<br>
I can turn my handle into a spout<br>
Tip me over and pour me out</h3>""", 418
# if customerrorpages is true, return error from templates/error.html. pass error and request page
if config['custom-error-pages']:
@app.errorhandler(HTTPException)
def page_not_found(e):
logging.warning('HTTP error ' + str(e.code))
return render_template('error.html', error=e, request=request.path, websiteuri=request.base_url, statuscode=e.code), e.code
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/<page>')
def dynamicpage(page):
if config['disallow-reading-redir'] is True:
if page.endswith('.redir'):
abort(403)
pages = []
if '.' in page:
return render_template(page)
# if page dosn't have a period in it
else:
# scan for pages with same filename, but if there are multiple, return 422
for f in os.listdir(os.path.join(app.root_path, 'templates')):
if f.startswith(page):
if f.endswith('.redir'):
with open("templates/" + f, 'r') as redirfile:
return redirect(redirfile.read())
pages.append(f)
if len(pages) > 1:
abort(422)
elif len(pages) == 1:
page = pages[0]
return render_template(page)
# if there are no files with the same name, return 404
#if not page in os.listdir(os.path.join(app.root_path, 'templates')):
#abort(404)
if __name__ == '__main__' and config['werkzeug-server']:
app.run(host='0.0.0.0', port=80, debug=config['werkzeug-debug'])