-
Notifications
You must be signed in to change notification settings - Fork 0
/
saudade.py
44 lines (37 loc) · 1.2 KB
/
saudade.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
import bottle
import jinja2
import json
class StripPathMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, e, h):
e['PATH_INFO'] = e['PATH_INFO'].rstrip('/')
return self.app(e,h)
app = bottle.Bottle()
app.jinja2_env = jinja2.Environment(loader=jinja2.FileSystemLoader("views/"))
def renderTemplate(name, jsonPayload):
return app.jinja2_env.get_template(name + '.html').render(jsonPayload)
@app.route('/static/<path:path>')
def callback(path):
return bottle.static_file(path, root='static')
@app.route('<url:re:.+>')
def index(url):
try:
json_data = open("routes.json")
data = json.load(json_data)
if(url in data):
if("file" in data[url]):
template = data[url]["file"]
if("data" in data[url]):
payload = data[url]["data"]
else:
payload = iter(())
return renderTemplate(template, payload)
else:
raise bottle.HTTPError(404, "Template file for %s not specified in routes.json" % url)
else:
raise bottle.HTTPError(404, "Url %s was not found on routes.json" % url)
except IOError:
raise bottle.HTTPError(404, "The file routes.json could not be found")
myapp = StripPathMiddleware(app)
bottle.run(host='localhost', port=8080, app=myapp)