mirror of
https://github.com/lloeki/minimal-rack/
synced 2025-12-06 05:04:40 +01:00
Compare commits
3 commits
5e6537c0e9
...
de989153e4
| Author | SHA1 | Date | |
|---|---|---|---|
| de989153e4 | |||
| 56e614e978 | |||
| 54a72b31ce |
4 changed files with 188 additions and 3 deletions
19
LICENSE
Normal file
19
LICENSE
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
Copyright (c) 2020 Loic Nageleisen
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
23
README.md
Normal file
23
README.md
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
# minimal-rack
|
||||||
|
|
||||||
|
Minimal Rack-based apps for historical and toying purposes
|
||||||
|
|
||||||
|
This covers Rack, Rails, Sinatra, Grape, and possibly more Rack-based stuff in the future.
|
||||||
|
|
||||||
|
# Idea
|
||||||
|
|
||||||
|
Sometimes I want to toy around with specific versions of a framework on specific versions of Ruby or this or that web server, explore how versions behave, and Rack being Rack, what happens when you nest things and what you can and cannot nest.
|
||||||
|
|
||||||
|
Instead of hacking code for each version, manually changing Gemfiles back and forth, possibly using bad versions, or generating a thousand Rails apps, these profide a simple, compact, dynamic way to just get what I want and hack on the actual thing I want to learn about.
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
|
||||||
|
* `compatibility` is a stupid YAML file with various version constraints.
|
||||||
|
* Native execution: `ruby {thing}.rb` serves `{thing}`. There is version resolution so `ruby rails.rb 7.0` gives you a Rails 7.0.something app, `ruby rails.rb 7.0.0` gives you exactly 7.0.0. There is server resolution too , so `ruby rails.rb 7.0 puma` serves with Puma.
|
||||||
|
* Docker execution: `rake:serve[rails]` gives you some rails, `:serve[rails:7.0]` gives you a Rails 7.0.something app like above, `rake:serve[rails,2.5]` gives you some rails version compatible with and running under ruby 2.5, give it `2.5-alpine` instead or add `musl` and it'll obey, and guess what you can shove `puma` or `thin` or whatever inside those brackets too.
|
||||||
|
* No bundler commands. There is bundler, but it's inline to dynamically describe deps, so, magic.
|
||||||
|
|
||||||
|
# Caveats
|
||||||
|
|
||||||
|
- There's a small issue with version selection vs how a thing version is specified in compatibility: if you say 'rails 7' it'll pick 7.1 but run with another compatibility match from a lesser version.
|
||||||
|
- There's no automatic web server picking (yet?), so it defaults to Thin but sometimes Thin doesn't work (e.g Rack 3)
|
||||||
|
|
@ -55,6 +55,10 @@ rails:
|
||||||
- version: '~> 7.0.0'
|
- version: '~> 7.0.0'
|
||||||
# 3.3 has a bug: https://bugs.ruby-lang.org/issues/20085
|
# 3.3 has a bug: https://bugs.ruby-lang.org/issues/20085
|
||||||
ruby: ['>= 2.7', '< 3.3']
|
ruby: ['>= 2.7', '< 3.3']
|
||||||
|
gem:
|
||||||
|
sinatra: '>= 0.a'
|
||||||
|
grape: '>= 0.a'
|
||||||
|
pry: '> 0'
|
||||||
- version: '~> 7.1.0'
|
- version: '~> 7.1.0'
|
||||||
# 3.3 has a bug: https://bugs.ruby-lang.org/issues/20085
|
# 3.3 has a bug: https://bugs.ruby-lang.org/issues/20085
|
||||||
ruby: ['>= 2.7', '< 3.3']
|
ruby: ['>= 2.7', '< 3.3']
|
||||||
|
|
|
||||||
145
rails.rb
145
rails.rb
|
|
@ -28,17 +28,146 @@ gemfile(true) do
|
||||||
|
|
||||||
gem 'rails', "~> #{version}.0"
|
gem 'rails', "~> #{version}.0"
|
||||||
gem server
|
gem server
|
||||||
|
gem 'ddtrace', path: '../../Datadog/dd-trace-rb', require: 'ddtrace/auto_instrument'
|
||||||
|
|
||||||
match.fetch('gem', []).each do |name, requirement|
|
match.fetch('gem', []).each do |name, requirement|
|
||||||
gem name, requirement
|
gem name, requirement
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
require 'grape'
|
||||||
|
|
||||||
|
class GrapeAPI < Grape::API
|
||||||
|
version 'v0', using: :header, vendor: 'hello'
|
||||||
|
format :json
|
||||||
|
prefix :hello
|
||||||
|
|
||||||
|
get :world do
|
||||||
|
{ hello: 'grape' }
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/:id' do
|
||||||
|
{ hello: params[:id] }
|
||||||
|
end
|
||||||
|
|
||||||
|
class Deeper < Grape::API
|
||||||
|
get :world do
|
||||||
|
{ hello: 'deeper' }
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/:id' do
|
||||||
|
{ hello: params[:id] }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Deep < Grape::API
|
||||||
|
get :world do
|
||||||
|
{ hello: 'deep' }
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/:id' do
|
||||||
|
{ hello: params[:id] }
|
||||||
|
end
|
||||||
|
|
||||||
|
mount Deeper => '/deeper'
|
||||||
|
end
|
||||||
|
|
||||||
|
mount Deep => '/deep'
|
||||||
|
end
|
||||||
|
|
||||||
|
require 'sinatra/base'
|
||||||
|
require 'json'
|
||||||
|
|
||||||
|
class SinatraApp < Sinatra::Base
|
||||||
|
get '/hello/world' do
|
||||||
|
status 200
|
||||||
|
content_type :json
|
||||||
|
body JSON.dump({ hello: :sinatra })
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/hello/:id' do
|
||||||
|
status 200
|
||||||
|
content_type :json
|
||||||
|
body JSON.dump({ hello: params[:id] })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
RackApp = Rack::Builder.new do
|
||||||
|
map "/hello/world" do
|
||||||
|
run -> (env) { [200, { 'content-type' => 'application/json' }, [JSON.dump({ hello: :rack })]] }
|
||||||
|
end
|
||||||
|
|
||||||
|
map "/grape" do
|
||||||
|
run GrapeAPI.new
|
||||||
|
end
|
||||||
|
|
||||||
|
map "/sinatra" do
|
||||||
|
run SinatraApp.new
|
||||||
|
end
|
||||||
|
|
||||||
|
map "/rack" do
|
||||||
|
run -> (env) { RackApp.call(env) }
|
||||||
|
end
|
||||||
|
|
||||||
|
map "/engine/simple" do
|
||||||
|
run -> (env) { SimpleEngine::Engine.call(env) }
|
||||||
|
end
|
||||||
|
|
||||||
|
map "/engine/endpoint" do
|
||||||
|
run -> (env) { EndpointEngine::Engine.call(env) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
require "action_controller/railtie"
|
require "action_controller/railtie"
|
||||||
|
|
||||||
|
module FindRootHack
|
||||||
|
def find_root(from)
|
||||||
|
Pathname.new File.realpath from
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module SimpleEngine
|
||||||
|
class Engine < ::Rails::Engine
|
||||||
|
extend FindRootHack
|
||||||
|
|
||||||
|
routes.append do
|
||||||
|
get "/hello/world" => "simple_engine/hello#world"
|
||||||
|
get "/hello/:id" => "simple_engine/hello#world"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class HelloController < ActionController::API
|
||||||
|
def world
|
||||||
|
render json: {hello: :engine}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module EndpointEngine
|
||||||
|
class Engine < ::Rails::Engine
|
||||||
|
extend FindRootHack
|
||||||
|
|
||||||
|
endpoint RackApp
|
||||||
|
end
|
||||||
|
|
||||||
|
class HelloController < ActionController::API
|
||||||
|
def world
|
||||||
|
render json: {hello: params[:id] || :engine}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module SimpleApp
|
||||||
class App < Rails::Application
|
class App < Rails::Application
|
||||||
routes.append do
|
routes.append do
|
||||||
get "/hello/world" => "hello#world"
|
get "/hello/world" => "hello#world"
|
||||||
|
get "/hello/:id" => "hello#world"
|
||||||
|
|
||||||
|
mount SimpleEngine::Engine => "/engine/simple"
|
||||||
|
mount EndpointEngine::Engine => "/engine/endpoint"
|
||||||
|
mount RackApp => "/rack"
|
||||||
|
mount SinatraApp => "/sinatra"
|
||||||
|
mount GrapeAPI => "/grape"
|
||||||
end
|
end
|
||||||
|
|
||||||
config.consider_all_requests_local = true # display errors
|
config.consider_all_requests_local = true # display errors
|
||||||
|
|
@ -50,6 +179,7 @@ class App < Rails::Application
|
||||||
config.secret_key_base = 'a4e6df27-2f39-41e4-83d2-3bc4d087c910'
|
config.secret_key_base = 'a4e6df27-2f39-41e4-83d2-3bc4d087c910'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if Gem::Requirement.new('< 5.0').satisfied_by?(Gem.loaded_specs['rails'].version)
|
if Gem::Requirement.new('< 5.0').satisfied_by?(Gem.loaded_specs['rails'].version)
|
||||||
action_controller_api_class = ActionController::Base
|
action_controller_api_class = ActionController::Base
|
||||||
|
|
@ -59,10 +189,19 @@ end
|
||||||
|
|
||||||
class HelloController < action_controller_api_class
|
class HelloController < action_controller_api_class
|
||||||
def world
|
def world
|
||||||
render json: {hello: :world}
|
render json: {hello: params[:id] || :rails}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
App.initialize!
|
Datadog.configure do |c|
|
||||||
|
c.diagnostics.debug = ['true', '1'].include?(ENV['DD_DIAGNOSTICS'])
|
||||||
|
c.tracing.enabled = true
|
||||||
|
c.tracing.instrument :rack
|
||||||
|
c.tracing.instrument :rails
|
||||||
|
c.tracing.instrument :sinatra
|
||||||
|
c.tracing.instrument :grape
|
||||||
|
end
|
||||||
|
|
||||||
Rack::Server.new(app: App, Host: '0.0.0.0', Port: 3000).start
|
SimpleApp::App.initialize!
|
||||||
|
|
||||||
|
Rack::Server.new(app: SimpleApp::App, Host: '0.0.0.0', Port: 3000).start
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue