Guest User

Untitled

a guest
Feb 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #!/usr/local/bin/ruby
  2. require 'rubygems'
  3. require 'mongrel'
  4.  
  5. # Add the request handler directory to the load path.
  6. # Files in the 'app/controllers' dir will be mapped against the first segment
  7. # of a URL
  8. $LOAD_PATH.unshift( File.join( File.dirname( __FILE__ ) , 'app/controllers' ) )
  9.  
  10. PORT = 4000
  11.  
  12. # If true, controller source files are 'load'ed rather than 'require'd
  13. # so you can tweak code and reload a page.
  14. ALLOW_RELOADING = true
  15.  
  16. class String
  17. def import
  18. ALLOW_RELOADING ? load( self + '.rb' ) : require( self )
  19. end
  20.  
  21. def controller_class_name
  22. self.capitalize
  23. end
  24. end
  25.  
  26. class MerberHandler < Mongrel::HttpHandler
  27.  
  28. def instantiate_controller(controller_name)
  29. controller_name.import
  30. begin
  31. return Object.const_get( controller_name.controller_class_name ).new
  32. rescue Exception
  33. warn "Error getting instance of '#{controller_name.controller_class_name}': #{$!}"
  34. raise $!
  35. end
  36. end
  37.  
  38. # Grab the request URL and break it up to get the parts that map to the
  39. # code request. There's a simple assumption that the first part defines a
  40. # class holding the desired code.
  41. def handle(request)
  42. path = request.params["PATH_INFO"]
  43. puts request.inspect
  44. puts '='*50
  45. # Might want to consider returning a default object if we have a bare URL.
  46. return [nil, nil, nil ] if path =~ /^\/$/
  47. c, m, args = path.to_s.gsub( /^\//, '' ).split( '/' , 3)
  48. args = args.to_s.strip.empty? ? nil : args.split( '/' )
  49. # STDERR.puts( "handler_details returning #{h}, #{m}, #{args.inspect} ")
  50. # Return an array with our object instance, the method name, and any args.
  51. [ instantiate_controller(c), m, args ]
  52. end
  53.  
  54. def process(request, response)
  55. response.start(200) do |head,out|
  56. head["Content-Type"] = "text/html"
  57. begin
  58. # Looks at the URL and breaks it up into
  59. # chunks that map to a class, a method call,
  60. # and arguments.
  61. # Basically,
  62. # /foo/bar/baz/qux
  63. # ends up becoming
  64. # Foo.new.bar( 'baz', 'qux' )
  65. controller, method, args = handle(request)
  66.  
  67. if controller
  68. # No allowance for default methods.
  69. # Worth considering, maybe default to 'index' or 'to_s'
  70. out << ( args ? controller.send( method, *args ) :
  71. controller.send( method ) )
  72. else
  73. out << "<html><body>Error: no merb controller found for this url.</body></html>"
  74. end
  75. rescue Exception
  76. out << "<html>Error! #{$!}</html>"
  77. end
  78. end
  79. end
  80.  
  81. end
  82.  
  83. h = Mongrel::HttpServer.new("0.0.0.0", PORT)
  84. h.register("/", MerberHandler.new)
  85. h.register("/", Mongrel::DirHandler.new("assets"))
  86. h.register("/favicon.ico", Mongrel::Error404Handler.new(""))
  87. h.run.join
  88.  
  89. # Trap any console interupts
  90. #trap( signal ){ s.shutdown }
Add Comment
Please, Sign In to add comment