forked from DarwinSenior/catalog-viewer
/
app.py
89 lines (68 loc) · 2.69 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
import tornado.escape
import tornado.ioloop
import tornado.web
import staticdata as data
import json
class tileHandler(tornado.web.RequestHandler):
def get(self, _id, z, x, y):
x, y, z = int(x), int(y), int(z)
the_data = data.readRegion(_id , (x, y, z))
content = json.dumps({
"position": [x, y, z],
"count": len(the_data),
"data": the_data,
"bound" : data.areaboundWithId(_id, (x, y, z))
});
self.set_status(200)
self.set_header('Content-Type', 'application/json')
self.write(content)
class mapHandler(tornado.web.RequestHandler):
def get(self, _id):
content = json.dumps(data.getMetaMap(_id))
self.set_status(200)
self.set_header('Content-Type', 'application/json')
self.write(content)
def post(self, _id):
info = {}
request_data = json.loads(self.request.body)
info['NAME'] = request_data.get('name')
info['DESCRIPTION'] = request_data.get('description')
data.modifyMap(request.form.get('id'), info)
content = json.dumps(data.getMaps())
self.set_status(200)
self.set_header('Content-Type', 'application/json')
self.write(content)
def put(self, _id):
request_data = json.loads(self.request.body)
name = request_data.get("name") or "untitled"
description = request_data.get("description") or ""
info = {"NAME": name, "DESCRIPTION" : description}
newmeta = data.createMap(info)
content = json.dumps(newmeta)
self.set_status(200)
self.set_header('Content-Type', 'application/json')
self.write(content)
class mainHandler(tornado.web.RequestHandler):
def get(self, *args):
self.set_status(200)
self.render('./static/index.html')
class redirectHandler(tornado.web.RequestHandler):
def get(self):
self.redirect('/viewmap/')
class mapsHandler(tornado.web.RequestHandler):
def get(self, *args):
content = json.dumps(data.getMaps())
self.set_status(200)
self.set_header('Content-Type', 'application/json')
self.write(content)
application = tornado.web.Application([
(r'/static/(.*)', tornado.web.StaticFileHandler, {'path': './static'}),
(r'/api/tile/([0-9a-z]+)/(-?[0-9]+)/(-?[0-9]+)/(-?[0-9]+).json', tileHandler),
(r'/api/map/([0-9a-z]+)', mapHandler),
(r'/viewmap/(.*)', mainHandler),
(r'/', redirectHandler),
(r'/api/maps', mapsHandler),
], debug=True)
if __name__ == '__main__':
application.listen('4000')
tornado.ioloop.IOLoop.instance().start()