Created
March 21, 2017 01:50
-
-
Save anonymous/665124f6d73fa2fdf4b3df84bdb86055 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'redic' | |
require 'watts' | |
require 'hoshi' | |
class ShortLink < Watts::App | |
class Hello < Watts::Resource | |
def redis | |
@redis ||= Redic.new 'redis://localhost:6379' | |
end | |
get { | |
count = redis.call('get', 'url:counter').to_i | |
b = Hoshi::View(:html5) { | |
doctype | |
html { | |
body { | |
h1 'Shorten Link' | |
form(method: 'post') { | |
input(type: 'text', name: 'url', placeholder: 'type or paste your url here') | |
input(type: 'submit') | |
} | |
p "#{count} link(s) shortened so far." | |
} | |
} | |
} | |
[200, {'Content-type' => 'text/html'}, [b]] | |
} | |
post { | |
p = request.params | |
u = p['url'] | |
n = redis.call('incr', 'url:counter').to_i | |
s = n.to_s(36) | |
redis.call('set', "url:#{s}", u) | |
b = Hoshi::View(:html5) { | |
doctype | |
html { | |
body { | |
h1 'Your link has been shortened' | |
a(href: "http://localhost:8080/#{s}") { safe "http://localhost:8080/#{s}" } | |
} | |
} | |
} | |
"shortlink.ru" 67L, 1392C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment