| | 2 | |
|---|
| | 3 | Little bitty lightweight ruby app server. FOr when you really need performance |
|---|
| | 4 | for simple dynamic pages. |
|---|
| | 5 | |
|---|
| | 6 | **FEATURES** 1. Mongrel handler built in that parses incoming requests |
|---|
| | 7 | including multipart uploads and post as well as ?query=strings. Puts the |
|---|
| | 8 | params into @params and the cookies into @cookies when it instantiates your |
|---|
| | 9 | controller class. |
|---|
| | 10 | |
|---|
| | 11 | 2. Dead simple routing mechanism. You can define regex routes or rely on the |
|---|
| | 12 | handler to map urls to classes. By default it maps /example/hello/world to |
|---|
| | 13 | Example.new.hello('world') and returns the Erb template that your simple |
|---|
| | 14 | controller classes return. But you can also use the RouteMatcher for more |
|---|
| | 15 | complex mappings. |
|---|
| | 16 | |
|---|
| | 17 | ROUTES = Merb::RouteMatcher.new do |r| |
|---|
| | 18 | r.add /foo\/bar/, :class => 'Qux', :method => 'spiffy' |
|---|
| | 19 | r.add /test\/hello/, :class => 'Neebler', :method => 'wibble' |
|---|
| | 20 | end |
|---|
| | 21 | |
|---|
| | 22 | These routes are checked in order that you add them and the first one found is |
|---|
| | 23 | used. If no routes match it falls back to the defaults mapping. It passes |
|---|
| | 24 | extra url parts to your method as args. So with the routes defined above this |
|---|
| | 25 | is the route map. |
|---|
| | 26 | |
|---|
| | 27 | /foo/bar/baz/12 --> Qux.new.spiffy('baz', '12') |
|---|
| | 28 | |
|---|
| | 29 | 3. Simple controller classes with built in render method and template handling |
|---|
| | 30 | with instance vars available in the views automatically. |
|---|
| | 31 | |
|---|
| | 32 | class Test < Merb::Controller |
|---|
| | 33 | template_dir :test |
|---|
| | 34 | |
|---|
| | 35 | def hello(*names) |
|---|
| | 36 | # @params and @cookies are avaialable here. string |
|---|
| | 37 | # keys and not symbols just yet. |
|---|
| | 38 | # Assign the parameter to an instance variable |
|---|
| | 39 | @name = names.join(', ') |
|---|
| | 40 | render |
|---|
| | 41 | end |
|---|
| | 42 | end |
|---|
| | 43 | |
|---|
| | 44 | <html> |
|---|
| | 45 | <head> |
|---|
| | 46 | <title>Hello, <%= @name %></title> |
|---|
| | 47 | </head> |
|---|
| | 48 | <body> |
|---|
| | 49 | <h1><%= @params.inspect %></h1> |
|---|
| | 50 | <h1><%= @cookies.inspect %></h1> |
|---|
| | 51 | <% 5.times do %> |
|---|
| | 52 | <h1>Hello, <%= @name %>!</h1> |
|---|
| | 53 | <% end %> |
|---|
| | 54 | </body> |
|---|
| | 55 | </html> |
|---|
| | 56 | |
|---|
| | 57 | 4. the server. right now you add your routes and the port number and such in |
|---|
| | 58 | the server.rb file. will be split out into a config file in next release. So |
|---|
| | 59 | by default it runs on port 4000 |
|---|
| | 60 | |
|---|
| | 61 | $ ruby server.rb |
|---|