["Hello Rack"]] end end run HelloWorld.new require 'rubygems' require 'goliath' class Proxy < Goliath::API def response(env) [200, {}, "Hello Goliath"] end end
post_init super no_environment_strings end def process_http_request redis = EM::Protocols::Redis.connect path = @http_request_uri.to_s query = @http_query_string.to_s response = EM::DelegatedHttpResponse.new(self) case path when '/' if @http_query_string params = URI.decode_www_form(query) url = params.assoc('url').last.chomp redis.keys '*' do |result| key = result.size.base62_encode redis.set key, url do |result| response.status = 200 response.content_type 'text/html' response.content = "http://localhost:9000/#{key}" response.send_response end end else response.status = 200 response.content_type 'text/html' response.content = 'Hi' response.send_response end else redis.get path[1..-1] do |result| response.status = 302 response.headers["Location"] = result response.send_response end end end end EM.run{ EM.start_server '0.0.0.0', 9000, Shortener } EM Example github.com/andrew/em-shortener
< Goliath::API use Goliath::Rack::Params def response(env) url = REDIS.get params[:key] if url [302, {:location => url}, nil] else [404, {}, 'Not Found'] end end end class Create < Goliath::API use Goliath::Rack::Params def response(env) if params['url'] key = REDIS.keys.size.base62_encode REDIS.set(key, params['url']) [200, {}, "http://#{env['HTTP_HOST']}/#{key}"] else [200, {}, "Hi!"] end end end class Shortener < Goliath::API map "/" do run Create.new end map "/:key" do run Redirect.new end end Goliath Example github.com/andrew/goliath-shortener