Permalink
Browse files

Merge branch 'nuke_router' of github.com:postrank-labs/goliath

  • Loading branch information...
2 parents 951ae62 + 7e841ba commit c53d8a44a6dc850c498fecbee535f4aae3dcc12b @igrigorik igrigorik committed May 11, 2012
View
@@ -4,8 +4,8 @@
require 'goliath'
require 'em-synchrony/em-http'
-
-class TwilioResponse < Goliath::API
+
+class Twilio < Goliath::API
use Goliath::Rack::Params
use Goliath::Rack::JSONP
@@ -18,11 +18,7 @@ def response(env)
http = EM::HttpRequest.new(url).get head: HEADERS
logger.debug "Received #{http.response_header.status} from Twilio"
-
+
[200, {'X-Goliath' => 'Proxy','Content-Type' => 'application/javascript'}, http.response]
end
end
-
-class Twilio < Goliath::API
- get %r{^/(Local|TollFree)}, TwilioResponse
-end
@@ -7,23 +7,12 @@
# Example demonstrating how to use a custom Goliath runner
#
-class Hello < Goliath::API
+class Custom < Goliath::API
def response(env)
[200, {}, "hello!"]
end
end
-class Bonjour < Goliath::API
- def response(env)
- [200, {}, "bonjour!"]
- end
-end
-
-class Custom < Goliath::API
- map "/hello", Hello
- map "/bonjour", Bonjour
-end
-
runner = Goliath::Runner.new(ARGV, nil)
runner.api = Custom.new
runner.app = Goliath::Rack::Builder.build(Custom, runner.api)
@@ -1,129 +0,0 @@
-#!/usr/bin/env ruby
-# encoding: utf-8
-$:<< '../lib' << 'lib'
-
-require 'goliath'
-
-# Example demonstrating how to have an API acting as a router.
-# RackRoutes defines multiple uris and how to map them accordingly.
-# Some of these routes are redirected to other Goliath API.
-#
-# The reason why only the last API is being used by the Goliath Server
-# is because its name matches the filename.
-# All the APIs are available but by default the server will use the one
-# matching the file name.
-
-# Our custom Goliath API
-class HelloWorld < Goliath::API
- def response(env)
- [200, {}, "hello world!"]
- end
-end
-
-class PostHelloWorld < Goliath::API
- def response(env)
- [200, {}, "hello post world!"]
- end
-end
-
-class HeaderCollector < Goliath::API
- def on_headers(env, header)
- @headers ||= {}
- @headers.merge!(header)
- end
-
- def response(env)
- [200, {}, "headers: #{@headers.inspect}"]
- end
-end
-
-class HelloNumber < Goliath::API
- use Goliath::Rack::Params
- def response(env)
- [200, {}, "number #{params[:number]}!"]
- end
-end
-
-class BigNumber < Goliath::API
- use Goliath::Rack::Params
- def response(env)
- [200, {}, "big number #{params[:number]}!"]
- end
-end
-
-class Bonjour < Goliath::API
- def response(env)
- [200, {}, "bonjour!"]
- end
-end
-
-class Hola < Goliath::API
- use Goliath::Rack::Params
- use Goliath::Rack::Validation::RequiredParam, {:key => "foo"}
-
- def response(env)
- [200, {}, "hola! #{params}"]
- end
-end
-
-class Aloha < Goliath::API
- use Goliath::Rack::Validation::RequestMethod, %w(GET)
-
- def response(env)
- [200, {}, "Aloha"]
- end
-end
-
-class Version < Goliath::API
- def response(env)
- [200, {"Content-Type" => "text/html"}, ["Version 0.1"]]
- end
-end
-
-class SayMyName < Goliath::API
- def response(env)
- [200, {}, ["Hello #{@opts[:name]}"]]
- end
-end
-
-
-class RackRoutes < Goliath::API
-
- # map Goliath API to a specific path
- get "/hello_world", HelloWorld
- head "/hello_world", HelloWorld
- post "/hello_world", PostHelloWorld
-
- map "/bonjour", Bonjour
- map "/aloha", Aloha
-
- get '/name1', SayMyName, :api_options => {:name => "Leonard"}
- get '/name2', SayMyName, :api_options => {:name => "Helena"}
-
- # map Goliath API to a specific path and inject validation middleware
- # for this route only in addition to the middleware specified by the
- # API class itself
- map "/headers", HeaderCollector do
- use Goliath::Rack::Validation::RequestMethod, %w(GET)
- end
-
- map "/hola/:name", Hola do
- use Goliath::Rack::Validation::RequestMethod, %w(GET)
- end
-
- # bad route: you cannot run arbitrary procs or classes, please provide
- # an implementation of the Goliath API
- map "/bad_route", Hola do
- run Hola.new
- end
-
- not_found('/') do
- run Proc.new { |env| [404, {"Content-Type" => "text/html"}, ["Try /version /hello_world, /bonjour, or /hola"]] }
- end
-
- # You must use either maps or response, but never both!
- def response(env)
- raise RuntimeException.new("#response is ignored when using maps, so this exception won't raise. See spec/integration/rack_routes_spec.")
- end
-
-end
@@ -1,25 +0,0 @@
-#!/usr/bin/env ruby
-$:<< '../lib' << 'lib'
-
-require 'goliath'
-require 'yajl'
-
-class Base < Goliath::API
- use Goliath::Rack::Params
- use Goliath::Rack::Render, 'json'
-
- def response(env)
- [200, {}, {:p => params}]
- end
-end
-
-class Extend < Base
- def response(env)
- [200, {}, params]
- end
-end
-
-class Router < Goliath::API
- map '/base', Base
- map '/extend', Extend
-end
View
@@ -1,42 +0,0 @@
-#!/usr/bin/env ruby
-$:<< '../lib' << 'lib'
-
-require 'goliath'
-
-class ValidSingleParam < Goliath::API
- use Goliath::Rack::Params
- use Goliath::Rack::Validation::RequiredParam, {:key => 'test'}
-
- # If you are using Golaith version <=0.9.1 you need to use Goliath::Rack::ValidationError
- # to prevent the request from remaining open after an error occurs
- #use Goliath::Rack::ValidationError
-
- def response(env)
- [200, {}, 'OK']
- end
-end
-
-
-class ValidNestedParams < Goliath::API
- use Goliath::Rack::Params
-
- # For this validation to pass you need to have this as parameter (json body here)
- # {
- # 'data' : {
- # 'login' : 'my_login'
- # }
- # }
- #
- use Goliath::Rack::Validation::RequiredParam, :key => %w(data login)
-
- def response(env)
- [200, {}, 'OK']
- end
-end
-
-
-
-class Router < Goliath::API
- map '/valid_param1', ValidSingleParam
- map '/valid_param2', ValidNestedParams
-end
View
@@ -5,9 +5,11 @@
require 'goliath/websocket'
require 'goliath/rack/templates'
-require 'pp'
+class Websocket < Goliath::WebSocket
+ include Goliath::Rack::Templates
+
+ use Goliath::Rack::Favicon, File.expand_path(File.dirname(__FILE__) + '/ws/favicon.ico')
-class WebsocketEndPoint < Goliath::WebSocket
def on_open(env)
env.logger.info("WS OPEN")
env['subscription'] = env.channel.subscribe { |m| env.stream_send(m) }
@@ -26,19 +28,12 @@ def on_close(env)
def on_error(env, error)
env.logger.error error
end
-end
-
-class WSInfo < Goliath::API
- include Goliath::Rack::Templates
def response(env)
- [200, {}, erb(:index, :views => Goliath::Application.root_path('ws'))]
+ if env['REQUEST_PATH'] == '/ws'
+ super(env)
+ else
+ [200, {}, erb(:index, :views => Goliath::Application.root_path('ws'))]
+ end
end
-end
-
-class Websocket < Goliath::API
- use Goliath::Rack::Favicon, File.expand_path(File.dirname(__FILE__) + '/ws/favicon.ico')
-
- get '/', WSInfo
- map '/ws', WebsocketEndPoint
-end
+end
View
@@ -25,7 +25,6 @@ Gem::Specification.new do |s|
s.add_dependency 'rack-respond_to'
s.add_dependency 'async-rack'
s.add_dependency 'multi_json'
- s.add_dependency 'http_router', '~> 0.9.0'
s.add_development_dependency 'rake', '>=0.8.7'
s.add_development_dependency 'rspec', '>2.0'
@@ -48,7 +47,7 @@ Gem::Specification.new do |s|
if RUBY_PLATFORM.include?('darwin')
s.add_development_dependency 'growl', '~> 1.0.3'
- s.add_development_dependency 'rb-fsevent', '0.4.3.1'
+ s.add_development_dependency 'rb-fsevent'
end
ignores = File.readlines(".gitignore").grep(/\S+/).map {|s| s.chomp }
Oops, something went wrong. Retry.

0 comments on commit c53d8a4

Please sign in to comment.