Sinatra 的 app 模板,提供一些胶水代码支持类似 Rails 的体验



如果你想灵活的开展工作,又觉得Rails过于庞大(比如Rails6+ 携带一个Node)、文档要读很久,正在犹豫当中。

你恰巧知道Sinatra的存在,15分钟读完的 Sinatra/README 又觉得自己行了,可是Sinatra似乎太简单了,你想要是Sinatra有 MVC和开箱即用的 ORM就好了。


一切基于Sinatra+Rack,用一些胶水代码 把 Rack/Sinatra + 配置 + 文件目录 联系在一起开始工作。容易更改,简单明了。


Sinatra App Template

Lightweight web framework codebase. Just clone and develop on it.

Tech component: Rack+Sinatra+Sequel and default use Postgresql database.

Add rails-like migration command line helpers.

Openbox Features


  • Multi Env Configuration
  • Multi router DSL base on Rack
  • CORS support
  • Hot reload
  • Custom logger
  • ORM base on Sequel'


  • Rails-like migration helpers
  • Test
  • Seed


  • Dockerfile

Find helpful rake tasks

rake or rake -T

Run server & develop

rake server:run

Production Server & deploy

APP_ENV=production bundle exec rake server:run

you can also use docker

docker built -t <what your docker image label> .

Custom server & database

You can use DSL to config Key:Value , then you application just use.

Config::Default.configure do
  set :app_env, ENV.fetch('APP_ENV'){ 'development' }
  set :bind, ENV.fetch('HOST') { '' }
  set :port, ENV.fetch('PORT') { 3000 }
  set :secrets, ENV.fetch('SECRETS') { 'YOU CANNOT GUESS ME' }
  set :max_threads, ENV.fetch('MAX_THREADS') { 5 }

  set :database_url, ENV['DATABASE_URL']

Config::Development.configure do 
  set :database_url, 'ENV['DATABASE_URL']'

Config::Test.configure do 
  set :database_url, ENV['DATABASE_URL']

Config::Production.configure do 
  # set :database_url, ENV['DATABASE_URL']

They have an inheritance relationship

Development < Default
Test < Default
Production < Default

In your code, just use Config directly. core/bootstrap do a work that loaded all necessery mods before your code.

Config.current  # current env configuration




You can also create your own Config for your single Application:

class MyConfig < Config::Base


MyConfig.configure do 
  # set :database_url, ENV['DATABASE_URL']

Mount different Sinatra web application


Lark also is Rack application. We can use Rack middlewares.

require_relative './cores/bootstrap'

# you can load Rack middleware here

# mount applications
require 'controllers/root_controller'
# routers(handy config)
map '/' do
  run RootController 


bases directory are use for Application Base Class.

You can make different Configured Sinatra Application class here, then your application/controller just inherit the Base Class to create Application.

It will share Config, and make less code.

# Sinatra Doc
require 'sinatra/base'
require 'json'

class BaseController < Sinatra::Base
  # Inject config

  # Config & register Sinatra Extensions

  # Rewrite Views dir
  settings.views = File.expand_path(File.join($PROJECT_DIR, 'views'))

  configure :development do
    require 'sinatra/reloader'
    register Sinatra::Reloader

  # mount Sinatra Helpers

  # mount Sinatra middlewares


# Share Configuration

class MyPageServer < BaseController

class MyApiServer < BaseController

ORM & Tools

Provide rails-like rake task help you build app quickly.

rake db:check                   # Checking for current migrations
rake db:connect                 # Connect database
rake db:console                 # Database Console
rake db:create[database_name]   # Create database
rake db:create_migration[name]  # Create a migration
rake db:drop[database_name]     # Drop database
rake db:ls                      # List database tables
rake db:migrate[version]        # Run migrations
rake db:rollback[version]       # Rollback to migration
rake db:version                 # Prints current schema version
rake list                       # List all tasks
rake seed:all                   # Seed: run all seeds
rake seed:run[seed_name]        # Seed: run seed
rake server:run                 # Run server
rake test                       # Run tests

Project Structure

├── Dockerfile # Common Dockerfile
├── Gemfile
├── Gemfile.lock
├── Rakefile # Rake Task Index File.
├── bases # Base configured class. You can make different BaseClasses then reuse them.
│   └── base_controller.rb # You contoller can inherit it or write yourself.
├── # Application index. You can mount controllers and routes here.
├── configs # You can make different configs for applications
│   └── config.rb # Base config
├── controllers 
│   └── root_controller.rb
├── cores # Inject ENVS and autoloads files, make MVC works
│   ├── 01_config.rb # Names can controller mount order
│   └── bootstrap.rb
├── dbs # You can make multi database here
│   ├── default_db.rb # default database connect instance
│   └── migrations # save database migrations
├── docs
│   └── good.feature
├── log # Directory for save logs by default
│   └── development.log
├── loggers # Loggers for application
│   └── default_logger.rb
├── public # Public resources
│   └── favicon.svg
├── seeds # Seeds
├── tasks # Rake helpful tasks 
│   ├── db_task.rb
│   ├── seed_task.rb
│   ├── server_task.rb
│   └── test_task.rb
├── tests # Test cases
│   └── test_demo.rb
└── views # views template
    ├── base.erb
    └── root.erb

Bootstrap & Load orders

For Rake

require_relative './cores/bootstrap'

It will auto load files make sure rake task can work.

In rake we can use Config.current to read configuration.

DB also available.

For Rack/Applications

In the same way

require_relative './cores/bootstrap'
# OR
# Bootstrap.apps

It will autoload all dep mods. Share with a context.

Change load orders

cores/bootstrap.rb defines different load orders, you can change.

In anther way, you can change filename to e.g 00_before_all.rb01_first_load.rb to control mods load order.

社区准则 博客 联系 社区 状态