Created
June 26, 2011 04:14
-
-
Save darkhelmet/1047235 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 '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