forked from postrank-labs/goliath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_server.rb
More file actions
executable file
·57 lines (47 loc) · 1.32 KB
/
Copy pathcustom_server.rb
File metadata and controls
executable file
·57 lines (47 loc) · 1.32 KB
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
56
#!/usr/bin/env ruby
$:<< '../lib' << 'lib'
require 'goliath/api'
require 'goliath/runner'
# Example demonstrating how to use a custom rack builder with the
# Goliath server and mixing Goliath APIs with normal Rack end points.
#
# Note, that the same routing behavior is supported by Goliath, loost at
# the rack_routes.rb example to see how to define custom routes.
# Our custom Goliath API
class HelloWorld < Goliath::API
def response(env)
[200, {}, "hello world!"]
end
end
# Another Goliath API
class Bonjour < Goliath::API
def response(env)
[200, {}, "bonjour!"]
end
end
# Rack builder acting as a router
router = Rack::Builder.new do
# Rack end point
map '/version' do
use ::Rack::ContentLength
run Proc.new {|env| [200, {"Content-Type" => "text/html"}, ["Version 0.1"]] }
end
# map the /hello_world uri to our Goliath API
map "/hello_world" do
use ::Rack::ContentLength
run HelloWorld.new
end
# map the /bonjour uri to our other Goliath API
map "/bonjour" do
use ::Rack::ContentLength
run Bonjour.new
end
# catch the root route and return a 404
map "/" do
use ::Rack::ContentLength
run Proc.new {|env| [404, {"Content-Type" => "text/html"}, ["Try /version /hello_world or /bonjour"]] }
end
end
runner = Goliath::Runner.new(ARGV, nil)
runner.app = router
runner.run