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

Mark24
·

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

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

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

这是最近做一个简单后端项目的沉淀,可以作为一个简单的起点。

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

分享一下,可能不够成熟,欢迎碰撞,让我可以学习更多~

https://github.com/Mark24Code/sinatra-app-template

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

Apps

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

Tasks

  • Rails-like migration helpers
  • Test
  • Seed

CI&CD

  • 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') { '0.0.0.0' }
  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']
end

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

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

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

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

Config::Development.database_url

Config::Development

Config::Development.database_url

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

class MyConfig < Config::Base

end

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

Mount different Sinatra web application

Edit config.ru

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

require_relative './cores/bootstrap'
Bootstrap.rack 

# you can load Rack middleware here

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

Base

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 http://sinatrarb.com/intro.html
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
  end

  # mount Sinatra Helpers

  # mount Sinatra middlewares

end


# Share Configuration

class MyPageServer < BaseController
end

class MyApiServer < BaseController
end

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
├── README.md
├── 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.
├── config.ru # 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'
Bootstrap.rake

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'
Bootstrap.rack
# 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.

2
1
社区准则 博客 联系 社区 状态
主题