-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp.py
executable file
·91 lines (69 loc) · 2.29 KB
/
webapp.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import config
import tornado.httpserver
import tornado.ioloop
import tornado.web
import threading
from redis import Redis
from urllib import unquote
from time import sleep
class Home(tornado.web.RequestHandler):
def get(self):
""" redirect to the /posts """
self.redirect("/posts", 301)
class Posts(tornado.web.RequestHandler):
def get(self):
""" render posts list page (10 by page) """
page = self.get_argument("page", 1)
offset = (page - 1) * 10
self.render("posts.html")
raise NotImplementedYet()
def post(self):
""" save new post """
title = self.get_argument("title")
body = self.get_argument("body")
tags = self.get_argument("tags")
raise NotImplementedYet()
class OnePost(tornado.web.RequestHandler):
def get(self, id):
""" render one post """
raise NotImplementedYet()
class Tags(tornado.web.RequestHandler):
def get(self, id):
""" render tag cloud """
raise NotImplementedYet()
class OneTag(tornado.web.RequestHandler):
def get(self, id):
""" render posts list by tag """
raise NotImplementedYet()
class Search(tornado.web.RequestHandler):
def get(self, expression):
""" render search results """
unquoted = unquote(expression)
raise NotImplementedYet()
class PostComments(tornado.web.RequestHandler):
def post(self, id):
""" save post comment """
body = self.get_argument("body")
parent_id = self.get_argument("parent_id", None)
self.redirect("/posts/%d"%id)
raise NotImplementedYet()
class NotImplementedYet(Exception):
pass
application = tornado.web.Application([
(r"/", Home),
(r"/posts", Posts),
(r"/posts/(\d+)", OnePost),
(r"/posts/(\d+)/comments", PostComments),
(r"/tags", Tags),
(r"/tags/(\d+)", OneTag),
(r"/search/(.+)", Search),
], debug=config.debug, template_path=os.path.join(os.path.dirname(__file__), 'templates'))
if __name__ == "__main__":
R = Redis(config.db_host)
R.select(config.db_idx)
http_server = tornado.httpserver.HTTPServer(application)
http_server.bind(config.port, config.host)
http_server.start(0)
tornado.ioloop.IOLoop.instance().start()