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

Dynamic Routing with Circuit - Rubyfuza

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

More Decks by Lance Gleason

Other Decks in Technology

Transcript

  1. Twitter @lgleasain Github lgleasain www.lancegleason.com www.polyglotprogrammincinc.com lgleason@polyglotprogramminginc.com 1 Friday, February

    8, 13
  2. Introductions 2 Friday, February 8, 13

  3. 3 Friday, February 8, 13

  4. 4 Friday, February 8, 13

  5. 5 Friday, February 8, 13

  6. ? 6 Friday, February 8, 13

  7. 7 Friday, February 8, 13

  8. 7 Friday, February 8, 13

  9. 7 Friday, February 8, 13

  10. 7 Friday, February 8, 13

  11. 7 Friday, February 8, 13

  12. 7 Friday, February 8, 13

  13. 8 Friday, February 8, 13

  14. 9 Friday, February 8, 13

  15. 10 Friday, February 8, 13

  16. 11 Friday, February 8, 13

  17. 12 Friday, February 8, 13

  18. Why?? 13 Friday, February 8, 13

  19. Heroku 14 Friday, February 8, 13

  20. www.honeybadger.com www.jackel.com www.bees.com 15 Friday, February 8, 13

  21. www.honeybadger.com => /badgers www.jackel.com => /jaeckels www.bees.com => /bees 16

    Friday, February 8, 13
  22. 17 Friday, February 8, 13

  23. 18 Friday, February 8, 13

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

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

    • Need to be able to specify domains dynamically 18 Friday, February 8, 13
  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
  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
  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
  29. 19 Friday, February 8, 13

  30. The beast at Tanagra 20 Friday, February 8, 13

  31. Rails 21 Friday, February 8, 13

  32. 22 Friday, February 8, 13

  33. • match '/*', to: 'site#proxy' 22 Friday, February 8, 13

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

    Friday, February 8, 13
  35. • match '/*', to: 'site#proxy' • Ugly DB lookup •

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

    Rails only. • Will need to have multiple instances. 22 Friday, February 8, 13
  37. Rack 23 Friday, February 8, 13

  38. 24 Friday, February 8, 13

  39. • Write Rack Middleware 24 Friday, February 8, 13

  40. • Write Rack Middleware • Implement DB Lookup 24 Friday,

    February 8, 13
  41. • Write Rack Middleware • Implement DB Lookup • Re-invent

    the wheel. 24 Friday, February 8, 13
  42. • Write Rack Middleware • Implement DB Lookup • Re-invent

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

  44. Rack Middlware 26 Friday, February 8, 13

  45. class  MyRackMiddleware    def  initialize(appl)        @appl  =

     appl    end    def  call(env)        [404,  {},  “Kiteo,    his  eyes  closed”]    end     end 27 Friday, February 8, 13
  46. Warning...shameless pet project promotion ahead 28 Friday, February 8, 13

  47. Redir https://github.com/springbot/redir 29 Friday, February 8, 13

  48. Passbook-IOS https://github.com/frozon/passbook 30 Friday, February 8, 13

  49. 31 Friday, February 8, 13

  50. Circuit 32 Friday, February 8, 13

  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
  52. 34 Friday, February 8, 13

  53. #  app/behaviors/render_ok.cru require  './my_middleware' run  proc  {|env|  [200,  {'Content-­‐Type'  =>

     'text/plain'},   ['OK']]  } 34 Friday, February 8, 13
  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
  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
  56. 35 Friday, February 8, 13

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

     >  @site.save $  =>  true 35 Friday, February 8, 13
  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
  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
  60. 37 Friday, February 8, 13

  61. Circuit Needs you for 38 Friday, February 8, 13

  62. Circuit Needs you for • Redis support 38 Friday, February

    8, 13
  63. Circuit Needs you for • Redis support • Active Record

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

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

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

    • Riak • Goliath • Feedback 38 Friday, February 8, 13
  67. Twitter @lgleasain Github lgleasain www.lancegleason.com www.polyglotprogrammincinc.com lgleason@polyglotprogramminginc.com 39 Friday, February

    8, 13