-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathapp.py
64 lines (47 loc) · 1.55 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
import os
import json
import tornado.ioloop
import tornado.web
# Going to save the posts in memory just for testing.
class DB(object):
def __init__(self):
self.posts = []
def save(self,obj):
self.posts.append(obj)
return obj
def all(self):
return self.posts
db = DB()
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html')
class Posts(tornado.web.RequestHandler):
def get(self):
self.write(json.dumps(db.all()))
class UpdateHandler(tornado.web.RequestHandler):
def post(self):
#Overly verbose
data = {}
for name in ['type', 'original_url', 'url', 'title', 'description',
'favicon_url', 'provider_url', 'provider_display', 'safe',
'html', 'thumbnail_url', 'object_type', 'image_url']:
data[name] = self.get_argument(name, None)
# This also works
# data = dict([(k, v[0]) for k, v in self.request.arguments.items()])
# Save the data off and return the object that we will pass back.
obj = db.save(data)
# Write a json response
self.write(json.dumps(obj))
app_settings = {
'debug' : True,
'static_path': os.path.join(os.path.dirname(__file__), "../.."),
}
url_mapping = [
(r"/", MainHandler),
(r"/posts", Posts),
(r"/update", UpdateHandler),
]
if __name__ == "__main__":
application = tornado.web.Application(url_mapping, **app_settings)
application.listen(8000)
tornado.ioloop.IOLoop.instance().start()