GithubHelp home page GithubHelp logo

akhmel / modular-sinatra Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jfeaver/modular-sinatra

0.0 2.0 0.0 117 KB

A base for a modular Sinatra app which namespaces routes through various controllers

Ruby 71.73% CSS 2.59% HTML 25.68%

modular-sinatra's Introduction

Modular Sinatra

This is a base for a medium sized Sinatra application.

Setup

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.

Usage

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

Controllers and Namespaced Routes

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

Advanced

Load additional files or directories of files in ‘app-dir/config/boot.rb`.

modular-sinatra's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.