Permalink
Browse files

change shorted url to method post

  • Loading branch information...
cedricporter committed May 12, 2014
1 parent 7df0366 commit e1d21743638336d2f652efd0f1d6ba716ff12184
Showing with 43 additions and 34 deletions.
  1. +2 −0 base/__init__.py
  2. +30 −0 base/util.py
  3. +1 −0 etc/config.py.sample
  4. +10 −34 main.py
View
@@ -0,0 +1,2 @@
+
+
View
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# By Hua Liang[Stupid ET]
+
+import re
+import string
+import random
+from etc import config
+
+
+regex = re.compile(
+ r'^(?:http|ftp)s?://' # http:// or https://
+ r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
+ r'localhost|' #localhost...
+ r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
+ r'(?::\d+)?' # optional port
+ r'(?:/?|[/?]\S+)$', re.IGNORECASE)
+
+
+def validate_url(url):
+ return bool(regex.match(url))
+
+
+def gen_short_id(length=config.SHORT_ID_LENGTH):
+ return "".join(random.sample(string.digits + string.ascii_letters, length))
+
+
+def gen_cache_key(*args):
+ return ":".join(str(item) for item in args)
+
View
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
PORT = 8865
+DEBUG = False
MAIN_PAGE_REDIRECT = "https://github.com/cedricporter/url-shorten"
SITE_URL = "http://163.gs/"
View
44 main.py
@@ -1,10 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-#
+# By Hua Liang[Stupid ET]
-import re
-import string
-import random
import urlparse
from tornado.web import HTTPError
@@ -15,27 +12,7 @@
import tornadoredis
from etc import config, const
-
-
-regex = re.compile(
- r'^(?:http|ftp)s?://' # http:// or https://
- r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
- r'localhost|' #localhost...
- r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
- r'(?::\d+)?' # optional port
- r'(?:/?|[/?]\S+)$', re.IGNORECASE)
-
-
-def validate_url(url):
- return bool(regex.match(url))
-
-
-def gen_short_id(length=config.SHORT_ID_LENGTH):
- return "".join(random.sample(string.digits + string.ascii_letters, length))
-
-
-def gen_cache_key(*args):
- return ":".join(str(item) for item in args)
+from base import util
c = tornadoredis.Client()
@@ -51,39 +28,38 @@ class ExpandUrlHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self, short_id):
- key = gen_cache_key(const.CACHE_KEY_PREFIX.SHORT_ID, short_id)
+ 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)
else:
raise HTTPError(404)
- self.finish()
class ShortenUrlHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
- def get(self):
+ def post(self):
url = self.get_argument("url")
- if not validate_url(url):
+ if not util.validate_url(url):
self.write("not a valid url")
self.finish()
return
while True:
- key = gen_cache_key(const.CACHE_KEY_PREFIX.REVERSE_URL, url)
+ key = util.gen_cache_key(const.CACHE_KEY_PREFIX.REVERSE_URL, url)
short_id = yield tornado.gen.Task(c.get, key)
if short_id:
break
- short_id = gen_short_id()
- key = gen_cache_key(const.CACHE_KEY_PREFIX.SHORT_ID, 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 = gen_cache_key(const.CACHE_KEY_PREFIX.REVERSE_URL, url)
+ key = util.gen_cache_key(const.CACHE_KEY_PREFIX.REVERSE_URL, url)
yield tornado.gen.Task(c.set, key, short_id)
break
@@ -95,7 +71,7 @@ def get(self):
(r"/", MainHandler),
(r"/short/?", ShortenUrlHandler),
(r"/([a-zA-Z0-9]{%s})/?" % config.SHORT_ID_LENGTH, ExpandUrlHandler),
-])
+], debug=config.DEBUG)
if __name__ == "__main__":
application.listen(config.PORT)

0 comments on commit e1d2174

Please sign in to comment.