Skip to content

Commit dc54ccd

Browse files
committed
Improve compatibility of rackup handler, w.r.t sinatra.
1 parent 6a4b48f commit dc54ccd

File tree

8 files changed

+150
-6
lines changed

8 files changed

+150
-6
lines changed

examples/sinatra/Gemfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@
44
source "https://rubygems.org"
55

66
gem "sinatra"
7-
gem "falcon"
7+
gem "falcon", path: "../.."
8+
9+
# Rack dependency:
10+
11+
gem "rack", "~> 2.0"
12+
# or:
13+
# gem "rack", "~> 3.0"
14+
# gem "rackup"

examples/sinatra/Gemfile.lock

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
PATH
2+
remote: ../..
3+
specs:
4+
falcon (0.48.3)
5+
async
6+
async-container (~> 0.18)
7+
async-http (~> 0.75)
8+
async-http-cache (~> 0.4)
9+
async-service (~> 0.10)
10+
bundler
11+
localhost (~> 1.1)
12+
openssl (~> 3.0)
13+
process-metrics (~> 0.2)
14+
protocol-http (~> 0.31)
15+
protocol-rack (~> 0.7)
16+
samovar (~> 2.3)
17+
18+
GEM
19+
remote: https://rubygems.org/
20+
specs:
21+
async (2.21.1)
22+
console (~> 1.29)
23+
fiber-annotation
24+
io-event (~> 1.6, >= 1.6.5)
25+
async-container (0.18.3)
26+
async (~> 2.10)
27+
async-http (0.86.0)
28+
async (>= 2.10.2)
29+
async-pool (~> 0.9)
30+
io-endpoint (~> 0.14)
31+
io-stream (~> 0.6)
32+
metrics (~> 0.12)
33+
protocol-http (~> 0.43)
34+
protocol-http1 (>= 0.28.1)
35+
protocol-http2 (~> 0.22)
36+
traces (~> 0.10)
37+
async-http-cache (0.4.4)
38+
async-http (~> 0.56)
39+
async-pool (0.10.2)
40+
async (>= 1.25)
41+
traces
42+
async-service (0.12.0)
43+
async
44+
async-container (~> 0.16)
45+
base64 (0.2.0)
46+
console (1.29.2)
47+
fiber-annotation
48+
fiber-local (~> 1.1)
49+
json
50+
fiber-annotation (0.2.0)
51+
fiber-local (1.1.0)
52+
fiber-storage
53+
fiber-storage (1.0.0)
54+
io-endpoint (0.14.0)
55+
io-event (1.7.5)
56+
io-stream (0.6.1)
57+
json (2.9.1)
58+
localhost (1.3.1)
59+
mapping (1.1.1)
60+
metrics (0.12.1)
61+
mustermann (3.0.3)
62+
ruby2_keywords (~> 0.0.1)
63+
openssl (3.3.0)
64+
process-metrics (0.3.0)
65+
console (~> 1.8)
66+
json (~> 2)
67+
samovar (~> 2.1)
68+
protocol-hpack (1.5.1)
69+
protocol-http (0.47.1)
70+
protocol-http1 (0.28.1)
71+
protocol-http (~> 0.22)
72+
protocol-http2 (0.22.0)
73+
protocol-hpack (~> 1.4)
74+
protocol-http (~> 0.18)
75+
protocol-rack (0.11.1)
76+
protocol-http (~> 0.43)
77+
rack (>= 1.0)
78+
rack (2.2.10)
79+
rack-protection (3.2.0)
80+
base64 (>= 0.1.0)
81+
rack (~> 2.2, >= 2.2.4)
82+
ruby2_keywords (0.0.5)
83+
samovar (2.3.0)
84+
console (~> 1.0)
85+
mapping (~> 1.0)
86+
sinatra (3.2.0)
87+
mustermann (~> 3.0)
88+
rack (~> 2.2, >= 2.2.4)
89+
rack-protection (= 3.2.0)
90+
tilt (~> 2.0)
91+
tilt (2.5.0)
92+
traces (0.14.1)
93+
94+
PLATFORMS
95+
ruby
96+
x86_64-linux
97+
98+
DEPENDENCIES
99+
falcon!
100+
rack (~> 2.0)
101+
sinatra
102+
103+
BUNDLED WITH
104+
2.5.22

examples/sinatra/app.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env ruby
2+
3+
# To run this example, you need to install the `sinatra` gem:
4+
# $ bundle install
5+
# $ bundle exec ./app.rb
6+
7+
require 'sinatra/base'
8+
9+
class Server < Sinatra::Application
10+
set :server, :falcon
11+
12+
# Hello World:
13+
get '/' do
14+
"Hello, World!"
15+
end
16+
end
17+
18+
Server.run!

examples/sinatra/config.ru

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
# Middleware that responds to incoming requests:
77
require "sinatra/base"
8+
89
class MyApp < Sinatra::Base
910
get "/" do
1011
response = Faraday.get "http://sushi.com/nigiri/sake.json"

lib/falcon/rackup/handler.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
# Released under the MIT License.
44
# Copyright, 2024, by Samuel Williams.
55

6-
require "rackup/handler"
7-
86
require_relative "../../falcon"
97

108
require "kernel/sync"

lib/rack/handler/falcon.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88

99
require_relative "../../falcon/rackup/handler"
1010

11+
# Generally speaking, you should not require this file directly, or assume the existance of the `Rack::Handler::Falcon` constant. Instead, use `Rack::Handler.get(:falcon)` to load and access the handler.
12+
1113
module Rack
1214
module Handler
13-
Falcon = ::Falcon::Rackup::Handler
14-
register :falcon, Falcon
15+
# Rack (v2) expects the constant to be in the `Rack::Handler` namespace, so we define a new handler class in the `Rack::Handler` namespace that inherits from `Falcon::Rackup::Handler`.
16+
class Falcon < ::Falcon::Rackup::Handler
17+
end
18+
19+
# Rack (v2) expects a string for the handler constant name. `Falcon.to_s` returns a more human friendly name, so we explicitly pass `Falcon.name` to `register` to ensure Rack can find the handler using the registered name.
20+
register :falcon, Falcon.name
1521
end
1622
end

lib/rackup/handler/falcon.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77

88
require_relative "../../falcon/rackup/handler"
99

10+
# Generally speaking, you should not require this file directly, or assume the existance of the `Rackup::Handler::Falcon` constant. Instead, use `Rackup::Handler.get(:falcon)` to load and access the handler.
11+
1012
module Rackup
1113
module Handler
12-
Falcon = ::Falcon::Rackup::Handler
14+
# Sinatra (and possibly others) try to extract the name using the final part of the class path, so we define a new class in the `Rack::Handler` namespace that inherits from `Falcon::Rackup::Handler`, that follows that convention.
15+
class Falcon < ::Falcon::Rackup::Handler
16+
end
17+
18+
# Rack (v3) expects a class for the handler constant, so we explicitly pass `Falcon` to `register` to ensure Rack can find the handler using the registered name.
1319
register :falcon, Falcon
1420
end
1521
end

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Improve compatibility of rackup handler w.r.t. sinatra.
6+
37
## v0.47.8
48

59
- Fix Falcon Supervisor implementation: due to invalid code, it was unable to start.

0 commit comments

Comments
 (0)