public
Rubygem
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
Click here to lend your support to: thin and make a donation at www.pledgie.com !
* Guess which adapter to use from directory (chdir option)
  or use specified one in 'adapter' option, re #47.
macournoyer (author)
Thu Mar 13 20:58:49 -0700 2008
commit  af63af254fc3fe63c62d2091e897f6b74aae1706
tree    0f8b3389a8f6ef6d09a5214a6e62a15910418247
parent  01b9902d59c795e31931b18a97a7a12cbcdbf305
...
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
0
@@ -1,3 +1,7 @@
0
+== 0.8.0 ??? release
0
+ * Guess which adapter to use from directory (chdir option)
0
+   or use specified one in 'adapter' option, re #47.
0
+
0
 == 0.7.1 Fancy Pants release
0
  * Clean stale PID files when starting as daemon, fixes #53 [Chu Yeow]
0
  * Require EventMachine 0.11.0 for UNIX domain sockets. Until it's released, install from:
...
37
38
39
 
40
41
42
...
37
38
39
40
41
42
43
0
@@ -37,6 +37,7 @@ module Thin
0
 end
0
 
0
 require 'rack'
0
+require 'rack/adapter/loader'
0
 
0
 module Rack
0
   module Handler
...
1
2
3
4
5
6
7
8
9
10
11
 
 
 
 
 
 
 
12
 
 
 
 
13
 
 
 
14
15
16
...
55
56
57
58
59
 
60
61
62
 
63
64
 
65
66
67
...
148
149
150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
152
153
154
...
1
2
3
 
 
 
 
 
 
 
 
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
61
62
63
 
 
64
65
 
 
66
67
 
68
69
70
71
...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
0
@@ -1,16 +1,22 @@
0
 require 'yaml'
0
 
0
 module Thin
0
-  # Build and control one Thin server.
0
-  # Hey Controller pattern is not only for web apps yo!
0
-  module Controllers
0
-    # Raised when a mandatory option is missing to run a command.
0
-    class OptionRequired < RuntimeError
0
-      def initialize(option)
0
-        super("#{option} option required")
0
-      end
0
+  # Error raised that will abort the process and print not backtrace.
0
+  class RunnerError < RuntimeError; end
0
+  
0
+  # Raised when a mandatory option is missing to run a command.
0
+  class OptionRequired < RunnerError
0
+    def initialize(option)
0
+      super("#{option} option required")
0
     end
0
+  end
0
+  
0
+  # Raised when an option is not valid.
0
+  class InvalidOption < RunnerError; end
0
   
0
+  # Build and control one Thin server.
0
+  # Hey Controller pattern is not only for web apps yo!
0
+  module Controllers  
0
     # Controls a Thin server.
0
     # Allow to start, stop, restart and configure a single thin server.
0
     class Controller
0
@@ -55,13 +61,11 @@ module Thin
0
         server.change_privilege @options[:user], @options[:group] if @options[:user] && @options[:group]
0
 
0
         # If a Rack config file is specified we eval it inside a Rack::Builder block to create
0
-        # a Rack adapter from it. DHH was hacker of the year a couple years ago so we default
0
-        # to Rails adapter.
0
+        # a Rack adapter from it. Or else we guess which adapter to use and load it.
0
         if @options[:rackup]
0
-          rackup_code = File.read(@options[:rackup])
0
-          server.app  = eval("Rack::Builder.new {( #{rackup_code}\n )}.to_app", TOPLEVEL_BINDING, @options[:rackup])
0
+          server.app = load_rackup_config
0
         else
0
-          server.app = Rack::Adapter::Rails.new(@options.merge(:root => @options[:chdir]))
0
+          server.app = load_adapter
0
         end
0
 
0
         # If a prefix is required, wrap in Rack URL mapper
0
@@ -148,6 +152,20 @@ module Thin
0
           sleep 1 if File.exist?(file) # HACK Give the thread a little time to open the file
0
           tail_thread
0
         end
0
+
0
+      private
0
+        def load_adapter
0
+          adapter = @options[:adapter] || Rack::Adapter.guess(@options[:chdir])
0
+          log ">> Using #{adapter} adapter"
0
+          Rack::Adapter.for(adapter, @options)
0
+        rescue Rack::AdapterNotFound => e
0
+          raise InvalidOption, e.message
0
+        end
0
+        
0
+        def load_rackup_config
0
+          rackup_code = File.read(@options[:rackup])
0
+          eval("Rack::Builder.new {( #{rackup_code}\n )}.to_app", TOPLEVEL_BINDING, @options[:rackup])
0
+        end
0
     end
0
   end
0
 end
0
\ No newline at end of file
...
1
2
3
4
 
5
6
7
...
62
63
64
65
66
 
 
67
68
69
70
71
72
 
 
 
 
 
 
73
74
75
...
153
154
155
156
 
 
 
 
 
157
158
159
...
1
2
3
 
4
5
6
7
...
62
63
64
 
 
65
66
67
68
69
 
70
71
72
73
74
75
76
77
78
79
80
...
158
159
160
 
161
162
163
164
165
166
167
168
0
@@ -1,7 +1,7 @@
0
 require 'optparse'
0
 require 'yaml'
0
 
0
-module Thin
0
+module Thin  
0
   # CLI runner.
0
   # Parse options and send command to the correct Controller.
0
   class Runner
0
@@ -62,14 +62,19 @@ module Thin
0
         opts.on("-p", "--port PORT", "use PORT (default: #{@options[:port]})")          { |port| @options[:port] = port.to_i }
0
         opts.on("-S", "--socket FILE", "bind to unix domain socket")                    { |file| @options[:socket] = file }
0
         opts.on("-y", "--swiftiply [KEY]", "Run using swiftiply")                       { |key| @options[:swiftiply] = key }
0
-        opts.on("-e", "--environment ENV", "Rails environment " +                       
0
-                                           "(default: #{@options[:environment]})")      { |env| @options[:environment] = env }
0
+        opts.on("-A", "--adapter NAME", "Rack adapter to use " +                       
0
+                                        "(default: auto-detected)")                     { |name| @options[:adapter] = name }
0
         opts.on("-c", "--chdir DIR", "Change to dir before starting")                   { |dir| @options[:chdir] = File.expand_path(dir) }
0
         opts.on("-r", "--rackup FILE", "Load a Rack config file instead of " +
0
                                        "Rails adapter")                                 { |file| @options[:rackup] = file }
0
-        opts.on(      "--prefix PATH", "Mount the app under PATH (start with /)")       { |path| @options[:prefix] = path }
0
         opts.on(      "--stats PATH", "Mount the Stats adapter under PATH")             { |path| @options[:stats] = path }
0
         
0
+        opts.separator ""
0
+        opts.separator "Rails options:"
0
+        opts.on("-e", "--environment ENV", "Rails environment " +                       
0
+                                           "(default: #{@options[:environment]})")      { |env| @options[:environment] = env }
0
+        opts.on(      "--prefix PATH", "Mount the app under PATH (start with /)")       { |path| @options[:prefix] = path }
0
+        
0
         unless Thin.win? # Daemonizing not supported on Windows
0
           opts.separator ""
0
           opts.separator "Daemon options:"
0
@@ -153,7 +158,11 @@ module Thin
0
       end
0
       
0
       if controller.respond_to?(@command)
0
-        controller.send(@command, *@arguments)
0
+        begin
0
+          controller.send(@command, *@arguments)
0
+        rescue RunnerError => e
0
+          abort e.message
0
+        end
0
       else
0
         abort "Invalid options for command: #{@command}"
0
       end
...
5
6
7
8
9
 
 
10
11
12
13
 
14
15
16
...
5
6
7
 
 
8
9
10
11
12
 
13
14
15
16
0
@@ -5,12 +5,12 @@ module Thin
0
   
0
   module VERSION #:nodoc:
0
     MAJOR    = 0
0
-    MINOR    = 7
0
-    TINY     = 1
0
+    MINOR    = 8
0
+    TINY     = 0
0
     
0
     STRING   = [MAJOR, MINOR, TINY].join('.')
0
     
0
-    CODENAME = 'Fancy Pants'
0
+    CODENAME = 'No Name Yet'
0
     
0
     RACK     = [0, 3].freeze # Latest Rack version that was tested
0
   end
...
10
11
12
13
 
 
14
15
16
...
10
11
12
 
13
14
15
16
17
0
@@ -10,7 +10,8 @@ describe Controller, 'start' do
0
                                  :log                  => 'thin.log',
0
                                  :timeout              => 60,
0
                                  :max_conns            => 2000,
0
-                                 :max_persistent_conns => 1000)
0
+                                 :max_persistent_conns => 1000,
0
+                                 :adapter              => 'rails')
0
     
0
     @server = OpenStruct.new
0
     @adapter = OpenStruct.new

Comments