-
Notifications
You must be signed in to change notification settings - Fork 553
/
my_rack_app.rb
55 lines (50 loc) · 1.57 KB
/
my_rack_app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'rack'
class MyRackApp
class NonArrayResponse
# The rack response body need not implement #join,
# but it must implement #each. It need not be an Array.
# ActionDispatch::Response, for example, exercises that fact.
# See: http://rack.rubyforge.org/doc/SPEC.html
def each(*args, &blk)
["This is not in an array!"].each(*args, &blk)
end
end
def self.call(env)
case env.values_at('REQUEST_METHOD', 'PATH_INFO')
when ['GET', '/']
[200, {}, ["This is my root!"]]
when ['GET', '/greet']
name = env['QUERY_STRING'][/name=([^&]*)/, 1] || "World"
[200, {}, ["Hello, #{name}"]]
when ['GET', '/non_array_response']
[200, {}, NonArrayResponse.new]
when ['GET', '/locked']
[200, {}, ["Single threaded response."]]
when ['POST', '/greet']
name = env["rack.input"].read(env["CONTENT_LENGTH"])
name = name.force_encoding("UTF-8") if name.respond_to? :force_encoding
name = name[/name=([^&]*)/, 1] || "World"
[200, {}, ["Good to meet you, #{name}!"]]
when ['GET', '/compute']
if env['SERVER_PORT'] == 80 && env["SCRIPT_NAME"] == ""
[200, {}, [""]]
else
[401, {}, [""]]
end
when ['GET', '/error']
env['rack.errors'].puts('Error!')
[500, {}, ['']]
when ['GET', '/env']
[200, {}, [JSON.dump(env)]]
else
[404, {}, ['']]
end
end
end
class MyLockedRackApp
MUTEX = Mutex.new
def self.call(env)
lock = Rack::Lock.new(MyRackApp, MUTEX)
lock.call(env)
end
end