public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
Search Repo:
Server can now yield a Rack::Builder to allow building an app in one call:


 Server.start '0.0.0.0', 3000 do
   use Rack::CommonLogger
   use Rack::ShowExceptions
   map "/lobster" do
     use Rack::Lint
     run Rack::Lobster.new
   end
 end
macournoyer (author)
Tue Jan 22 12:01:52 -0800 2008
commit  57eceafee8354b99f06a6bcb29bceb02e863fc9e
tree    be66688d136c641e02d8eef91127906b28d18e7c
parent  90d02898c69e4ff2b673368c9d85f936be121507
...
1
 
 
 
 
 
 
 
 
 
 
 
2
3
4
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
@@ -1,4 +1,15 @@
0
 == 0.5.5 Pony release
0
+ * Server can now yield a Rack::Builder to allow building an app in one call:
0
+
0
+ Server.start '0.0.0.0', 3000 do
0
+ use Rack::CommonLogger
0
+ use Rack::ShowExceptions
0
+ map "/lobster" do
0
+ use Rack::Lint
0
+ run Rack::Lobster.new
0
+ end
0
+ end
0
+
0
  * Add a very basic stats page through Stats adapter, load w/ --stats and browse to /stats.
0
  * Add --trace (-V) option to trace request/response and get backtrace w/out all Ruby debug stuff.
0
  * Add --config (-C) option to load options from a config file in thin script [Matt Todd].
...
36
37
38
39
 
40
41
42
...
36
37
38
 
39
40
41
42
0
@@ -36,7 +36,7 @@
0
     
0
     def headers=(key_value_pairs)
0
       key_value_pairs.each do |k, vs|
0
- vs.each { |v| @headers[k] = v.chomp }
0
+ vs.each_line { |v| @headers[k] = v.chomp }
0
       end
0
     end
0
     
...
23
24
25
26
 
 
 
 
 
 
 
 
 
 
 
 
 
27
28
29
30
 
 
 
 
 
 
31
32
33
...
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
0
@@ -23,11 +23,29 @@
0
     
0
     # Creates a new server binded to <tt>host:port</tt>
0
     # that will pass request to +app+.
0
- def initialize(host, port, app=nil)
0
+ # If a block is passed, a <tt>Rack::Builder</tt> instance
0
+ # will be passed to build the +app+.
0
+ #
0
+ # Server.new '0.0.0.0', 3000 do
0
+ # use Rack::CommonLogger
0
+ # use Rack::ShowExceptions
0
+ # map "/lobster" do
0
+ # use Rack::Lint
0
+ # run Rack::Lobster.new
0
+ # end
0
+ # end.start!
0
+ #
0
+ def initialize(host, port, app=nil, &block)
0
       @host = host
0
       @port = port.to_i
0
       @app = app
0
       @timeout = 60 # sec
0
+
0
+ @app = Rack::Builder.new(&block).to_app if block
0
+ end
0
+
0
+ def self.start(host, port, &block)
0
+ new(host, port, &block).start!
0
     end
0
     
0
     # Starts the handlers.
...
78
79
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
0
@@ -78,4 +78,41 @@
0
       Net::HTTP.post_form(URI.parse('http://0.0.0.0:3333' + url), params).body
0
     end
0
 end
0
+
0
+describe Server, 'app configuration' do
0
+ it "should build app from constructor" do
0
+ server = Server.new('0.0.0.0', 3000, :works)
0
+
0
+ server.app.should == :works
0
+ end
0
+
0
+ it "should build app from builder block" do
0
+ server = Server.new '0.0.0.0', 3000 do
0
+ run(proc { |env| :works })
0
+ end
0
+
0
+ server.app.call({}).should == :works
0
+ end
0
+
0
+ it "should use middlewares in builder block" do
0
+ server = Server.new '0.0.0.0', 3000 do
0
+ use Rack::ShowExceptions
0
+ run(proc { |env| :works })
0
+ end
0
+
0
+ server.app.class.should == Rack::ShowExceptions
0
+ server.app.call({}).should == :works
0
+ end
0
+
0
+ it "should work with Rack url mapper" do
0
+ server = Server.new '0.0.0.0', 3000 do
0
+ map '/test' do
0
+ run(proc { |env| :works })
0
+ end
0
+ end
0
+
0
+ server.app.call({})[0].should == 404
0
+ server.app.call({'PATH_INFO' => '/test'}).should == :works
0
+ end
0
+end

Comments

    No one has commented yet.