Permalink
Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
297 lines (205 sloc) 4.68 KB

Controllers

Run a single controller app:

class App < E
  # ...
end

App.run

# or
app = App.mount do
  # some setup
end
app.run

# or create a new app and mount controller
app = E.new
app.mount App do
  # some setup
end
app.run

Controllers can be also mounted by using Regexps:

app = E.new
app.mount /SomeController/
# etc.
app.run

[ contents ↑ ]

Slices

Slices are used to bundle, setup and run a set of controllers.

A Espresso Slice is nothing more than a Ruby Module.

That's it, to create a slice simply wrap your controllers into a module:

require 'e'
require 'e-ext' # needed for Forum.run and Forum.mount to work

module Forum
  class Users < E
    # ...
  end
  class Posts < E
    # ...
  end
end

Forum.run  # running Forum Slice directly

# creating a new app from Forum Slice
app = Forum.mount do 
  # some setup
end
app.run

# or create a new app and mount the slice
app = E.new
app.mount Forum do
  # some setup
end
app.run

[ contents ↑ ]

Automount

By using automount you do not need to mount controllers manually.

To make newly created app to automount all found controllers, pass true as first argument:

app = E.new(true)

If you want to automount only controllers contained into some namespace, use that namespace as first argument:

module Frontend
  class Pages < E
    # ...
  end

  class News < E
    # ...
  end
end

module Admin
  class Pages < E
    # ...
  end

  class News < E
    # ...
  end
end
app = E.new(Frontend)

this will automount only controllers under Frontend module, leaving you to mount Admin controllers manually.

Namespace can also be provided as a regular expression:

module Frontend
  class Pages < E
    # ...
  end

  class News < E
    # ...
  end
end

module Admin
  class Pages < E
    # ...
  end

  class News < E
    # ...
  end
end
app = E.new(/Pages/)

this will automount Frontend::Pages and Admin::Pages

If you need some controller(s) to not be automounted, use reject_automount! method inside controller class:

class Pages < E
  # ...
end

class News < E
  reject_automount!
  # ...
end

app = E.new(true)

this will automount Pages controller but not News

Mount Root

To mount a controller/slice into a specific root, pass it as a String:

module Forum
  class Users < E
    # ...
  end
  class Posts < E
    # ...
  end
end

app = Forum.mount('/forum')
app.run

# or
app = E.new
app.mount(Forum, '/forum')
app.run

[ contents ↑ ]

Arbitrary Applications

Espresso allow to mount any Rack-compatible application same way as usual controllers/slices mounted.

Example: mount SomeSinatraApp into "/"

E.new do
  mount SomeSinatraApp
end

Example: mount SomeSinatraApp into "/blog"

E.new do
  mount SomeSinatraApp, "/blog"
end

By default, mounted applications will respond to any request method.

To make it respond only to some request method(s), use :on option:

Example: make SomeSinatraApp to respond only to specific requests

E.new do
  mount SomeSinatraApp, on: :get
  # or
  mount SomeSinatraApp, on: [:get, :post]
  # or
  mount SomeSinatraApp, request_method: :get
  # or
  mount SomeSinatraApp, request_methods: [:get, :post]
end

Worth to note that mounted applications will honor host politics:

Example: SomeSinatraApp will respond only to requests originating on default host and site.com

E.new do
  map host: 'site.com'
  mount SomeSinatraApp
end

[ contents ↑ ]

Run

By default Espresso will run WEBrick server on 5252 port.

To run another server/port, use :server/:port options.

If given server requires some options, pass them next to :server option.

Example: Use Thin server on its default port

app.run :server => :Thin

Example: Use EventedMongrel server with custom options

app.run :server => :EventedMongrel, :port => 9090, :num_processors => 100

[ contents ↑ ]

config.ru

Running a single controller:

require 'your-app-file(s)'

run MyController

Running a Slice:

require 'your-app-file(s)'

run MySlice

Running an app instance:

require 'your-app-file(s)'

app = MyController.mount

# or create a new Espresso application using `E.new`
app = E.new :automount  # will auto-discover all available controllers

run app

[ contents ↑ ]