forked from cedricporter/url-shorten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·84 lines (65 loc) · 2.46 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# By Hua Liang[Stupid ET]
import urlparse
import tornado.options
from tornado.options import define, options
from tornado.web import HTTPError
import tornado.gen
import tornado.httpserver
import tornado.options
import tornado.web
import tornadoredis
import model
from model.connection import CONNECTION_POOL
from etc import config, const
from base import util
define("port", default=config.PORT, help="run on the given port", type=int)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("templates/short.html")
class ExpandUrlHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self, short_id):
c = tornadoredis.Client(connection_pool=CONNECTION_POOL)
key = util.gen_cache_key(const.CACHE_KEY_PREFIX.SHORT_ID, short_id)
url = yield tornado.gen.Task(c.get, key)
if url:
self.redirect(url, permanent=True)
else:
raise HTTPError(404)
class ShortenUrlHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def post(self):
c = tornadoredis.Client(connection_pool=CONNECTION_POOL)
url = self.get_argument("url")
if not util.validate_url(url):
self.write("not a valid url")
self.finish()
return
key = util.gen_cache_key(const.CACHE_KEY_PREFIX.REVERSE_URL, url)
short_id = redis_short_id = yield tornado.gen.Task(c.get, key)
while not redis_short_id:
short_id = util.gen_short_id()
key = util.gen_cache_key(const.CACHE_KEY_PREFIX.SHORT_ID, short_id)
ret = yield tornado.gen.Task(c.get, key)
if ret:
continue
yield tornado.gen.Task(c.set, key, url)
key = util.gen_cache_key(const.CACHE_KEY_PREFIX.REVERSE_URL, url)
yield tornado.gen.Task(c.set, key, short_id)
break
self.write(urlparse.urljoin(config.SITE_URL, short_id))
self.finish()
application = tornado.web.Application([
(r"/", MainHandler),
(r"/short/?", ShortenUrlHandler),
(r"/([a-zA-Z0-9]{%s})/?" % config.SHORT_ID_LENGTH, ExpandUrlHandler),
], debug=config.DEBUG)
if __name__ == "__main__":
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(application, xheaders=True)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()