Dynamic Routing with Circuit - Rubyfuza

89e0c5e7bfe1c613b1b9287d89963e73?s=47 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.

89e0c5e7bfe1c613b1b9287d89963e73?s=128

Lance Gleason

February 07, 2013
Tweet

Transcript

  1. 24.
  2. 25.

    • Need to be able to quickly add new domains.

    • Need to be able to specify domains dynamically 18 Friday, February 8, 13
  3. 26.

    • 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
  4. 27.

    • 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
  5. 28.

    • 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
  6. 35.

    • match '/*', to: 'site#proxy' • Ugly DB lookup •

    Rails only. 22 Friday, February 8, 13
  7. 36.

    • match '/*', to: 'site#proxy' • Ugly DB lookup •

    Rails only. • Will need to have multiple instances. 22 Friday, February 8, 13
  8. 41.
  9. 42.

    • Write Rack Middleware • Implement DB Lookup • Re-invent

    the wheel. • Will need multiple instances to use different middleware. 24 Friday, February 8, 13
  10. 45.

    class  MyRackMiddleware    def  initialize(appl)        @appl  =

     appl    end    def  call(env)        [404,  {},  “Kiteo,    his  eyes  closed”]    end     end 27 Friday, February 8, 13
  11. 51.

    Circuit • Initially filled a need for a custom CMS

    • Preloads configuration • As fast as * routes • Doesn’t require Rails 33 Friday, February 8, 13
  12. 54.

    #  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
  13. 55.

    #  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
  14. 57.

    $  >  @site  =  Site.new(host:  "example.com",  aliases:   ["www.example.com"]) $

     >  @site.save $  =>  true 35 Friday, February 8, 13
  15. 58.

    $  >  @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
  16. 59.

    #  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
  17. 64.
  18. 65.

    Circuit Needs you for • Redis support • Active Record

    • Riak • Goliath 38 Friday, February 8, 13
  19. 66.

    Circuit Needs you for • Redis support • Active Record

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