Created
November 28, 2012 21:43
-
-
Save MBO/4164816 to your computer and use it in GitHub Desktop.
Cramp example with Journey instead of HttpRouter
This file contains hidden or 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 "bundler" | |
Bundler.setup(:default) | |
require "cramp" | |
require "http_router" | |
require "thin" | |
class HomeController < Cramp::Action | |
def before_start | |
if params[:password] != "foo" | |
halt 401, {}, "Bad Password" | |
else | |
continue | |
end | |
end | |
def start | |
EM.add_timer(1) { render "Hello World"; finish } | |
end | |
end | |
routes = HttpRouter.new do | |
add("/(:password)").to(HomeController) | |
end | |
Rack::Handler::Thin.run routes, :Port => 3000 |
This file contains hidden or 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 "bundler" | |
Bundler.setup(:default) | |
require "cramp" | |
require "journey" | |
require "thin" | |
class HomeController < Cramp::Action | |
def before_start | |
if params[:password] != "foo" | |
halt 401, {}, "Bad Password" | |
else | |
continue | |
end | |
end | |
def start | |
EM.add_timer(1) { render "Hello World"; finish } | |
end | |
end | |
class Router | |
extend Forwardable | |
def_delegator :@router, :call | |
def initialize | |
@routes = Journey::Routes.new | |
@router = Journey::Router.new( | |
@routes, | |
parameters_key: "router.params" | |
) | |
yield(self) if block_given? | |
end | |
def route(path, options) | |
app = options.delete(:to) | |
conditions = options.delete(:conditions) { {} } | |
defaults = options.delete(:defaults) { {} } | |
name = options.delete(:name) { nil } | |
pattern = Journey::Path::Pattern.new( | |
Journey::Router::Strexp.new( | |
Journey::Router::Utils.normalize_path(path), | |
{}, # requirements | |
%w( / ? ), # separators | |
true # anchored | |
) | |
) | |
@routes.add_route(app, pattern, conditions, defaults, name) | |
end | |
end | |
router = Router.new do |r| | |
r.route "/(:password)", to: HomeController | |
end | |
Rack::Handler::Thin.run router, :Port => 3000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment