3
# Copyright 2009 Facebook
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
21
import tornado.database
22
import tornado.httpserver
24
import tornado.options
28
from tornado.options import define, options
30
define("port", default=8888, help="run on the given port", type=int)
31
define("mysql_host", default="127.0.0.1:3306", help="blog database host")
32
define("mysql_database", default="blog", help="blog database name")
33
define("mysql_user", default="blog", help="blog database user")
34
define("mysql_password", default="blog", help="blog database password")
37
class Application(tornado.web.Application):
41
(r"/archive", ArchiveHandler),
42
(r"/feed", FeedHandler),
43
(r"/entry/([^/]+)", EntryHandler),
44
(r"/compose", ComposeHandler),
45
(r"/auth/login", AuthLoginHandler),
46
(r"/auth/logout", AuthLogoutHandler),
49
blog_title=u"Tornado Blog",
50
template_path=os.path.join(os.path.dirname(__file__), "templates"),
51
static_path=os.path.join(os.path.dirname(__file__), "static"),
52
ui_modules={"Entry": EntryModule},
54
cookie_secret="11oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
55
login_url="/auth/login",
57
tornado.web.Application.__init__(self, handlers, **settings)
59
# Have one global connection to the blog DB across all handlers
60
self.db = tornado.database.Connection(
61
host=options.mysql_host, database=options.mysql_database,
62
user=options.mysql_user, password=options.mysql_password)
65
class BaseHandler(tornado.web.RequestHandler):
68
return self.application.db
70
def get_current_user(self):
71
user_id = self.get_secure_cookie("user")
72
if not user_id: return None
73
return self.db.get("SELECT * FROM authors WHERE id = %s", int(user_id))
76
class HomeHandler(BaseHandler):
78
entries = self.db.query("SELECT * FROM entries ORDER BY published "
81
self.redirect("/compose")
83
self.render("home.html", entries=entries)
86
class EntryHandler(BaseHandler):
88
entry = self.db.get("SELECT * FROM entries WHERE slug = %s", slug)
89
if not entry: raise tornado.web.HTTPError(404)
90
self.render("entry.html", entry=entry)
93
class ArchiveHandler(BaseHandler):
95
entries = self.db.query("SELECT * FROM entries ORDER BY published "
97
self.render("archive.html", entries=entries)
100
class FeedHandler(BaseHandler):
102
entries = self.db.query("SELECT * FROM entries ORDER BY published "
104
self.set_header("Content-Type", "application/atom+xml")
105
self.render("feed.xml", entries=entries)
108
class ComposeHandler(BaseHandler):
109
@tornado.web.authenticated
111
id = self.get_argument("id", None)
114
entry = self.db.get("SELECT * FROM entries WHERE id = %s", int(id))
115
self.render("compose.html", entry=entry)
117
@tornado.web.authenticated
119
id = self.get_argument("id", None)
120
title = self.get_argument("title")
121
text = self.get_argument("markdown")
122
html = markdown.markdown(text)
124
entry = self.db.get("SELECT * FROM entries WHERE id = %s", int(id))
125
if not entry: raise tornado.web.HTTPError(404)
128
"UPDATE entries SET title = %s, markdown = %s, html = %s "
129
"WHERE id = %s", title, text, html, int(id))
131
slug = unicodedata.normalize("NFKD", title).encode(
133
slug = re.sub(r"[^\w]+", " ", slug)
134
slug = "-".join(slug.lower().strip().split())
135
if not slug: slug = "entry"
137
e = self.db.get("SELECT * FROM entries WHERE slug = %s", slug)
141
"INSERT INTO entries (author_id,title,slug,markdown,html,"
142
"published) VALUES (%s,%s,%s,%s,%s,UTC_TIMESTAMP())",
143
self.current_user.id, title, slug, text, html)
144
self.redirect("/entry/" + slug)
147
class AuthLoginHandler(BaseHandler, tornado.auth.GoogleMixin):
148
@tornado.web.asynchronous
150
if self.get_argument("openid.mode", None):
151
self.get_authenticated_user(self.async_callback(self._on_auth))
153
self.authenticate_redirect()
155
def _on_auth(self, user):
157
raise tornado.web.HTTPError(500, "Google auth failed")
158
author = self.db.get("SELECT * FROM authors WHERE email = %s",
161
# Auto-create first author
162
any_author = self.db.get("SELECT * FROM authors LIMIT 1")
164
author_id = self.db.execute(
165
"INSERT INTO authors (email,name) VALUES (%s,%s)",
166
user["email"], user["name"])
171
author_id = author["id"]
172
self.set_secure_cookie("user", str(author_id))
173
self.redirect(self.get_argument("next", "/"))
176
class AuthLogoutHandler(BaseHandler):
178
self.clear_cookie("user")
179
self.redirect(self.get_argument("next", "/"))
182
class EntryModule(tornado.web.UIModule):
183
def render(self, entry):
184
return self.render_string("modules/entry.html", entry=entry)
188
tornado.options.parse_command_line()
189
http_server = tornado.httpserver.HTTPServer(Application())
190
http_server.listen(options.port)
191
tornado.ioloop.IOLoop.instance().start()
194
if __name__ == "__main__":