Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dynamic Routing with Circuit - Rubyfuza

Lance Gleason
February 07, 2013

Dynamic Routing with Circuit - Rubyfuza

Rails and Rack both have mechanisms to route requests. They are powerful, and offer some dynamic routing if you want to route within Rails. But what if you need to dynamically route requests to multiple Rack based applications? What if you want to control that routing dynamically with a database? Circuit allows you to do that. In this talk will explore the basics of Rack Middleware and Routing. Then we will show you how Circuit can help you to take Rack routing to the next level dynamically with lots of traffic.

Lance Gleason

February 07, 2013
Tweet

More Decks by Lance Gleason

Other Decks in Technology

Transcript

  1. • Need to be able to quickly add new domains.

    • Need to be able to specify domains dynamically 18 Friday, February 8, 13
  2. • Need to be able to quickly add new domains.

    • Need to be able to specify domains dynamically • Need to be able to have arbitrary nesting 18 Friday, February 8, 13
  3. • Need to be able to quickly add new domains.

    • Need to be able to specify domains dynamically • Need to be able to have arbitrary nesting • Need to use different middlewares 18 Friday, February 8, 13
  4. • Need to be able to quickly add new domains.

    • Need to be able to specify domains dynamically • Need to be able to have arbitrary nesting • Need to use different middlewares • We don’t have budget for devops. 18 Friday, February 8, 13
  5. • match '/*', to: 'site#proxy' • Ugly DB lookup •

    Rails only. 22 Friday, February 8, 13
  6. • match '/*', to: 'site#proxy' • Ugly DB lookup •

    Rails only. • Will need to have multiple instances. 22 Friday, February 8, 13
  7. • Write Rack Middleware • Implement DB Lookup • Re-invent

    the wheel. • Will need multiple instances to use different middleware. 24 Friday, February 8, 13
  8. class  MyRackMiddleware    def  initialize(appl)        @appl  =

     appl    end    def  call(env)        [404,  {},  “Kiteo,    his  eyes  closed”]    end     end 27 Friday, February 8, 13
  9. Circuit • Initially filled a need for a custom CMS

    • Preloads configuration • As fast as * routes • Doesn’t require Rails 33 Friday, February 8, 13
  10. #  app/behaviors/render_ok.cru require  './my_middleware' run  proc  {|env|  [200,  {'Content-­‐Type'  =>

     'text/plain'},   ['OK']]  } #  app/models/site.rb class  Site    include  Circuit::Storage::Sites::MongoidStore::Site    has_one  :root,  :class_name  =>  "Node" end 34 Friday, February 8, 13
  11. #  app/behaviors/render_ok.cru require  './my_middleware' run  proc  {|env|  [200,  {'Content-­‐Type'  =>

     'text/plain'},   ['OK']]  } #  app/models/site.rb class  Site    include  Circuit::Storage::Sites::MongoidStore::Site    has_one  :root,  :class_name  =>  "Node" end #  app/models/node.rb class  Node    include  Circuit::Storage::Nodes::MongoidStore::Node    belongs_to  :site,  :inverse_of  =>  :root end 34 Friday, February 8, 13
  12. $  >  @site  =  Site.new(host:  "example.com",  aliases:   ["www.example.com"]) $

     >  @site.save $  =>  true 35 Friday, February 8, 13
  13. $  >  @site  =  Site.new(host:  "example.com",  aliases:   ["www.example.com"]) $

     >  @site.save $  =>  true $  >  @node  =  Node.new(site:  @site,  slug:  nil,  behavior_klass:   "RenderOk") $  >  @node.save $  =>  true $  >  @node.root?  #  the  nil  slug  and  defined  site  indicates  the   root $  =>  true $  >  @node.behavior $  =>  RenderOk $  >  @node.behavior.class $  =>  Module $  >  @node.behavior.included_modules $  =>  [Circuit::Behavior] 35 Friday, February 8, 13
  14. #  app/behaviors/page.cru use  Circuit::Middleware::Rewriter  do  |request|        

       content_id  =  request.route.last.content.id            ["",  "/contents/#{content_id}"]        end #  config/routes.rb Rails.application.routes.draw  do    resources  :contents end 36 Friday, February 8, 13
  15. Circuit Needs you for • Redis support • Active Record

    • Riak • Goliath 38 Friday, February 8, 13
  16. Circuit Needs you for • Redis support • Active Record

    • Riak • Goliath • Feedback 38 Friday, February 8, 13