-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (44 loc) · 1.18 KB
/
main.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
import json
from flask import Flask, g, send_from_directory
import db
app = Flask('world-weaver')
def api_response(func):
def wrapped(*args, **kwargs):
out = func(*args, **kwargs)
if out and out.body:
out = out.body
elif out and out._fetch:
out = out._fetch()
return json.dumps(out)
return wrapped
def use_db(func):
def wrapped(*args, **kwargs):
conn = getattr(g, '_database', None)
if not conn:
conn = g._database = db.setup()
return func(conn, *args, **kwargs)
return wrapped
@app.route('/')
def index():
with open('static/index.html') as f:
return f.read()
@app.route('/nodes/root')
@api_response
@use_db
def root_node(conn):
return db.get_root_node(conn)
@app.route('/js/<path:path>')
def send_js(path):
return send_from_directory('static/js', path)
@app.route('/css/<path:path>')
def send_css(path):
return send_from_directory('static/css', path)
@app.teardown_appcontext
def close_db(exception):
conn = getattr(g, '_database', None)
if conn:
conn.close()
if __name__ == '__main__':
db.setup()
app.debug = True
app.run()