Skip to content

Instantly share code, notes, and snippets.

@fanweixiao
Forked from darkhelmet/shrt.rb
Created March 10, 2012 05:22
Show Gist options
  • Save fanweixiao/2010335 to your computer and use it in GitHub Desktop.
Save fanweixiao/2010335 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'uri'
RIAK = ['localhost', 8098]
run lambda { |env|
req = Rack::Request.new(env)
if req.post? && req.path == '/'
res = Net::HTTP.new(*RIAK).start do |http|
http.post('/riak/shrt', req.params['url'], { 'Content-Type' => 'text/plain' })
end
loc = [req.base_url, 'r', res['location'].split('/').last].join('/')
[res.code.to_i, { 'Content-Type' => 'text/plain', 'Location' => loc }, [res.message]]
elsif req.get? && req.path.start_with?('/r/')
key = req.path.split('/').last
res = Net::HTTP.new(*RIAK).start do |http|
http.get("/riak/shrt/#{key}")
end
[302, { 'Content-Type' => 'text/plain', 'Location' => res.body }, ['Redirecting...']]
else
[404, {'Content-Type' => 'text/plain'}, ['wat']]
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment