This is a base for a medium sized Sinatra application.
This application uses Bundler to manage your gems and Rack as the webserver interface.
Install them both if you don’t have them:
gem install bundler rack
Sinatra, Rack Flash, and Haml are included by default in the Gemfile and are required for the example app. However, only Sinatra is required while building your own application.
After editing the Gemfile to your liking:
bundle install
Now, you should be good to go.
Initialize controllers in ‘app-dir/application.rb`. You can put routes for each controller in this file or you can delegate routes to their own file (`app-dir/controllers/example_controller.rb`). See the Controllers and Namespaced Routes section below for more details.
Other than controllers, this app follows a typical directory format for a Sinatra app:
modular_sinatra/ ├── config # just a boot file for now ├── controllers # where all the routes live, non-namespaced routes go to ApplicationController ├── lib # models, other classes/modules ├── log # Note: Logger isn't included by default ├── public # assets, images, stylsheets go here └── views # Application layout at layout.haml
To start up a local server:
rackup
To maintain some sanity when dealing with lots of routes on a mid-sized web app, use the controller scheme provided here. Each controller controls the routes in a particular namespace. For example, the provided ‘GameController` serves routes within the `yoursite.com/game/` namespace.
To add a controller:
-
initialize it in application.rb and make it a subclass of the application class
-
add a mapping to the desired namespace
For Example:
app_dir/application.rb class SinatraApp < Sinatra::Base # ... some config stuff class ExampleController < SinatraApp map('/example') end end
-
Add Sinatra routes inside application.rb or to a file in the controllers directory
-
this file should end with ‘_controller.rb’
-
The controller class needs to be nested inside the application class for the route methods to be recognized
-
For Example:
app_dir/controllers/example_controller.rb class SinatraApp class ExampleController get('/say_hi') do # => GET /example/say_hi 'hello world' end end end
Load additional files or directories of files in ‘app-dir/config/boot.rb`.