public
Fork of wycats/merb-core
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/auser/merb-core.git
Search Repo:
Updated rack adapters, added Merb::Config and made a merb binary that 
works
ezmobius (author)
Sun Jan 13 17:50:05 -0800 2008
commit  7dffeb416ffd4f55763551387cabf6c7aff441c4
tree    94306e7ee23b53d4a690f6c06a9823173b151c61
parent  86eef63c291eb02a236ee9f966aee55c0b0c21b0
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0
@@ -1 +1,21 @@
0
+Copyright (c) 2008 Ezra Zygmuntowicz
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+"Software"), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sublicense, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
...
63
64
65
66
 
67
68
69
...
63
64
65
 
66
67
68
69
0
@@ -63,7 +63,7 @@
0
   s.add_dependency "ruby2ruby"
0
   s.add_dependency "json_pure"
0
   s.add_dependency "assistance"
0
-
0
+ s.add_dependency "rspec"
0
   # Requirements
0
   s.requirements << "install the json gem to get faster json parsing"
0
   s.required_ruby_version = ">= 1.8.4"
...
1
2
3
4
 
 
...
1
2
3
 
4
5
0
@@ -1,5 +1,6 @@
0
 #!/usr/bin/env ruby
0
 require 'rubygems'
0
 require 'merb'
0
-Merb::Rack::Adapter.new
0
+
0
+Merb.start
...
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
...
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
0
@@ -4,26 +4,51 @@
0
 require 'rubygems'
0
 require 'set'
0
 require 'fileutils'
0
+require "assistance"
0
 
0
 $LOAD_PATH.push File.dirname(__FILE__) unless
0
   $LOAD_PATH.include?(File.dirname(__FILE__)) ||
0
   $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
0
 
0
+require 'merb_core/autoload'
0
+require 'merb_core/core_ext'
0
 require 'merb_core/gem_ext/erubis'
0
 require 'merb_core/logger'
0
 require 'merb_core/version'
0
-require 'merb_core/core_ext'
0
 
0
-gem "assistance"
0
-require "assistance"
0
 
0
 module Merb
0
   class << self
0
     
0
+ def start(argv=ARGV)
0
+ Merb::Config.parse_args(argv)
0
+ BootLoader.run
0
+ case Merb::Config[:adapter]
0
+ when nil
0
+ # Guess.
0
+ if ENV.include?("PHP_FCGI_CHILDREN")
0
+ adapter = Merb::Rack::FastCGI
0
+ else
0
+ begin
0
+ adapter = Merb::Rack::Mongrel
0
+ rescue LoadError => e
0
+ adapter = Merb::Rack::WEBrick
0
+ end
0
+ end
0
+ when "mongrel"
0
+ adapter = Merb::Rack::Mongrel
0
+ when "webrick"
0
+ adapter = Merb::Rack::WEBrick
0
+ when "fastcgi"
0
+ adapter = Merb::Rack::FastCGI
0
+ else
0
+ adapter = Merb::Rack.const_get(server.capitalize)
0
+ end
0
+ adapter.start_server(Merb::Config[:host], Merb::Config[:port])
0
+ end
0
+
0
     attr_accessor :environment, :load_paths
0
     Merb.load_paths = Hash.new
0
-
0
- require 'merb_core/autoload'
0
     
0
     # This is the core mechanism for setting up your application layout
0
     # merb-core won't set a default application layout, but merb-more will
...
1
2
3
 
4
5
6
7
8
9
 
10
11
12
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
@@ -1,12 +1,14 @@
0
 module Merb
0
   autoload :AbstractController, "merb_core/controller/abstract_controller"
0
   autoload :BootLoader, "merb_core/boot/bootloader"
0
+ autoload :Config, "merb_core/config"
0
   autoload :Const, "merb_core/constants"
0
   autoload :Controller, "merb_core/controller/merb_controller"
0
   autoload :ControllerExceptions, "merb_core/controller/exceptions"
0
   autoload :Dispatcher, "merb_core/dispatch/dispatcher"
0
   autoload :ErubisCaptureMixin, "merb_core/controller/mixins/erubis_capture"
0
   autoload :Plugins, "merb_core/plugins"
0
+ autoload :Rack, "merb_core/rack"
0
   autoload :RenderMixin, "merb_core/controller/mixins/render"
0
   autoload :Request, "merb_core/dispatch/request"
0
   autoload :Responder, "merb_core/controller/mixins/responder"
...
1
2
3
4
5
6
7
8
9
...
52
53
54
55
56
 
 
57
58
59
...
133
134
135
136
 
137
138
139
...
157
158
159
 
160
161
162
...
1
2
 
 
 
 
3
4
5
...
48
49
50
 
 
51
52
53
54
55
...
129
130
131
 
132
133
134
135
...
153
154
155
156
157
158
159
0
@@ -1,9 +1,5 @@
0
 module Merb
0
   
0
- def self.start
0
- BootLoader.run
0
- end
0
-
0
   class BootLoader
0
     
0
     cattr_accessor :subclasses
0
@@ -52,8 +48,8 @@
0
     %[view model controller helper mailer part].each do |component|
0
       Merb.push_path(component.to_sym, Merb.root_path("app/#{component}s"))
0
     end
0
- Merb.push_path(:app_controller, Merb.root_path("app/controllers", "application_controller.rb"))
0
- Merb.push_path(:config, Merb.root_path("config", "router.rb"))
0
+ Merb.push_path(:app_controller, Merb.root_path("app/controllers/application.rb"))
0
+ Merb.push_path(:config, Merb.root_path("config/router.rb"))
0
     Merb.push_path(:lib, Merb.root_path("lib"))
0
   end
0
 end
0
@@ -133,7 +129,7 @@
0
 end
0
 
0
 class Merb::BootLoader::Libraries < Merb::BootLoader
0
- @@libraries = {:disable_json_gem => %[json/ext json/pure]}
0
+ @@libraries = {:disable_json_gem => %w[json/ext json/pure]}
0
 
0
   # Add other libraries to load in early in the boot process
0
   #
0
@@ -157,6 +153,7 @@
0
   end
0
   
0
   def require_first_working(first, *rest)
0
+ p first, rest
0
     require first
0
   rescue LoadError
0
     raise LoadError if rest.empty?
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
0
@@ -1 +1,294 @@
0
+require 'optparse'
0
+module Merb
0
+ class Config
0
+ class << self
0
+
0
+ def defaults
0
+ @defaults ||= {
0
+ :host => "0.0.0.0",
0
+ :port => "4000",
0
+ :adapter => 'mongrel',
0
+ :reloader => true,
0
+ :cache_templates => false,
0
+ :merb_root => Dir.pwd,
0
+ :use_mutex => true,
0
+ :session_id_cookie_only => true,
0
+ :query_string_whitelist => [],
0
+ :mongrel_x_sendfile => true
0
+ }
0
+ end
0
+
0
+ def [](key)
0
+ (@configuration||={})[key]
0
+ end
0
+
0
+ def []=(key,val)
0
+ @configuration[key] = val
0
+ end
0
+ def delete(key)
0
+ @configuration.delete key
0
+ end
0
+
0
+ def fetch(key, default)
0
+ @configuration.fetch key, default
0
+ end
0
+
0
+ def to_yaml
0
+ @configuration.to_yaml
0
+ end
0
+
0
+ def setup(global_merb_yml = nil)
0
+ @configuration ||= {}
0
+ if FileTest.exist? "#{defaults[:merb_root]}/framework"
0
+ $LOAD_PATH.unshift( "#{defaults[:merb_root]}/framework" )
0
+ end
0
+ global_merb_yml ||= "#{defaults[:merb_root]}/config/merb.yml"
0
+ apply_configuration_from_file defaults, global_merb_yml
0
+ end
0
+
0
+ def apply_configuration_from_file(configuration, file)
0
+ if File.exists?(file)
0
+ configuration.merge(Erubis.load_yaml_file(file))
0
+ else
0
+ configuration
0
+ end
0
+ end
0
+
0
+ def parse_args(argv = ARGV)
0
+ @configuration ||= {}
0
+ # Our primary configuration hash for the length of this method
0
+ options = {}
0
+
0
+ # Environment variables always win
0
+ options[:environment] = ENV['MERB_ENV'] if ENV['MERB_ENV']
0
+
0
+ # Build a parser for the command line arguments
0
+ opts = OptionParser.new do |opts|
0
+ opts.version = Merb::VERSION
0
+
0
+ opts.banner = "Usage: merb [fdcepghmisluMG] [argument]"
0
+ opts.define_head "Merb Mongrel+ Erb. Lightweight replacement for ActionPack."
0
+ opts.separator '*'*80
0
+ opts.separator 'If no flags are given, Merb starts in the foreground on port 4000.'
0
+ opts.separator '*'*80
0
+
0
+ opts.on("-u", "--user USER", "This flag is for having merb run as a user other than the one currently logged in. Note: if you set this you must also provide a --group option for it to take effect.") do |config|
0
+ options[:user] = config
0
+ end
0
+
0
+ opts.on("-G", "--group GROUP", "This flag is for having merb run as a group other than the one currently logged in. Note: if you set this you must also provide a --user option for it to take effect.") do |config|
0
+ options[:group] = config
0
+ end
0
+
0
+ opts.on("-f", "--config-file FILENAME", "This flag is for adding extra config files for things like the upload progress module.") do |config|
0
+ options[:config] = config
0
+ end
0
+
0
+ opts.on("-d", "--daemonize", "This will run a single merb in the background.") do |config|
0
+ options[:daemonize] = true
0
+ end
0
+
0
+ opts.on("-c", "--cluster-nodes NUM_MERBS", "Number of merb daemons to run.") do |nodes|
0
+ options[:cluster] = nodes
0
+ end
0
+
0
+ opts.on("-p", "--port PORTNUM", "Port to run merb on, defaults to 4000.") do |port|
0
+ options[:port] = port
0
+ end
0
+
0
+ opts.on("-h", "--host HOSTNAME", "Host to bind to (default is all IP's).") do |host|
0
+ options[:host] = host
0
+ end
0
+
0
+ opts.on("-m", "--merb-root Merb.root", "The path to the Merb.root for the app you want to run (default is current working dir).") do |root|
0
+ options[:merb_root] = File.expand_path(root)
0
+ end
0
+
0
+ opts.on("-a", "--adapter mongrel", "The rack adapter to use to run merb[mongrel, emongrel, thin, fastcgi, webrick]") do |adapter|
0
+ options[:adapter] = adapter
0
+ end
0
+
0
+
0
+ opts.on("-i", "--irb-console", "This flag will start merb in irb console mode. All your models and other classes will be available for you in an irb session.") do |console|
0
+ ::Merb::BootLoader.initialize_merb
0
+ _merb = Class.new do
0
+ class << self
0
+ include Merb::GeneralControllerMixin
0
+ def params() {} end
0
+ end
0
+ def self.show_routes(all_opts = false)
0
+ seen = []
0
+ unless Merb::Router.named_routes.empty?
0
+ puts "Named Routes"
0
+ Merb::Router.named_routes.each do |name,route|
0
+ puts " #{name}: #{route}"
0
+ seen << route
0
+ end
0
+ end
0
+ puts "Anonymous Routes"
0
+ (Merb::Router.routes - seen).each do |route|
0
+ puts " #{route}"
0
+ end
0
+ nil
0
+ end
0
+ end
0
+
0
+ Object.send(:define_method, :merb) {
0
+ _merb
0
+ }
0
+ ARGV.clear # Avoid passing args to IRB
0
+ require 'irb'
0
+ require 'irb/completion'
0
+ def exit
0
+ exit!
0
+ end
0
+ if File.exists? ".irbrc"
0
+ ENV['IRBRC'] = ".irbrc"
0
+ end
0
+ IRB.start
0
+ exit!
0
+ end
0
+
0
+ opts.on("-s", "--start-drb PORTNUM", "This is the port number to run the drb daemon on for sessions and upload progress monitoring.") do |drb_port|
0
+ puts "Starting merb drb server on port: #{Merb::Config[:drb_server_port]}"
0
+ Merb::Server.start(drb_port, :drbserver_start)
0
+ exit if Merb::Config[:only_drb]
0
+ end
0
+
0
+ opts.on("-l", "--log-level LEVEL", "Log levels can be set to any of these options: DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN") do |loglevel|
0
+ options[:log_level] = loglevel
0
+ end
0
+
0
+ opts.on("-e", "--environment STRING", "Run merb in the correct mode(development, production, testing)") do |env|
0
+ options[:environment] ||= env
0
+ end
0
+
0
+ opts.on("-r", "--script-runner ['RUBY CODE'| FULL_SCRIPT_PATH]",
0
+ "Command-line option to run scripts and/or code in the merb app.") do |code_or_file|
0
+ ::Merb::BootLoader.initialize_merb
0
+ if File.exists?(code_or_file)
0
+ eval(File.read(code_or_file))
0
+ else
0
+ eval(code_or_file)
0
+ end
0
+ exit!
0
+ end
0
+
0
+ opts.on("-P","--generate-plugin PATH", "Generate a fresh merb plugin at PATH.") do |path|
0
+ require 'merb/generators/merb_plugin'
0
+ ::Merb::PluginGenerator.run path || Dir.pwd
0
+ exit
0
+ end
0
+
0
+ opts.on("-K", "--graceful PORT or all", "Gracefully kill one merb proceses by port number. Use merb -K all to gracefully kill all merbs.") do |ports|
0
+ @configuration = defaults.merge(options)
0
+ Merb::Server.kill(ports, 1)
0
+ end
0
+
0
+ opts.on("-k", "--kill PORT or all", "Kill one merb proceses by port number. Use merb -k all to kill all merbs.") do |ports|
0
+ @configuration = defaults.merge(options)
0
+ Merb::Server.kill(ports, 9)
0
+ end
0
+
0
+ opts.on("-M", "--merb-config FILENAME", "This flag is for explicitly declaring the merb app's config file.") do |config|
0
+ options[:merb_config] = config
0
+ end
0
+
0
+ opts.on("-w", "--webrick", "Run merb using Webrick Rack Adapter instead of mongrel.") do |webport|
0
+ puts "Starting merb webrick server on port: #{Merb::Config[:port]}"
0
+ trap('TERM') { exit }
0
+ Merb::Server.webrick_start(Merb::Config[:port])
0
+ end
0
+
0
+ opts.on("-F", "--fastcgi", "Run merb using FastCGI Rack Adapter instead of mongrel.") do
0
+ trap('TERM') { exit }
0
+ Merb::Server.fastcgi_start
0
+ end
0
+
0
+ opts.on("-X", "--mutex on/off", "This flag is for turning the mutex lock on and off.") do |mutex|
0
+ if mutex == 'off'
0
+ options[:use_mutex] = false
0
+ else
0
+ options[:use_mutex] = true
0
+ end
0
+ end
0
+
0
+ opts.on("-D", "--debugger", "Run merb using rDebug.") do
0
+ begin
0
+ require 'ruby-debug'
0
+ Debugger.start
0
+ Debugger.settings[:autoeval] = true if Debugger.respond_to?(:settings)
0
+ puts "Debugger enabled"
0
+ rescue LoadError
0
+ puts "You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug'"
0
+ exit
0
+ end
0
+ end
0
+
0
+ opts.on("-?", "-H", "--help", "Show this help message") do
0
+ puts opts
0
+ exit
0
+ end
0
+ end
0
+
0
+ # If we run merb with no arguments and we are not inside a merb root
0
+ # show the help message
0
+ if !defined?(Merb.framework_root) && (argv.size == 0) && !File.exists?("#{options[:merb_root] || Merb::Config.defaults[:merb_root]}/config/merb_init.rb")
0
+ puts "You are not in the root of a merb application...\n"
0
+ puts opts
0
+ exit
0
+ end
0
+
0
+ # Parse what we have on the command line
0
+ opts.parse!(argv)
0
+
0
+ # merb <argument> is same as merb -g <argument>
0
+ if argv.size == 1
0
+ require 'merb/generators/merb_app/merb_app'
0
+ ::Merb::AppGenerator.run File.expand_path(argv.last)
0
+ exit!
0
+ end
0
+
0
+ # Load up the configuration from file, but keep the command line
0
+ # options that may have been chosen. Also, pass-through if we have
0
+ # a new merb_config path.
0
+ options = Merb::Config.setup(options[:merb_config]).merge(options)
0
+
0
+ # Finally, if all else fails... set the environment to 'development'
0
+ options[:environment] ||= 'development'
0
+
0
+ environment_merb_yml = "#{options[:merb_root]}/config/environments/#{options[:environment]}.yml"
0
+
0
+ @configuration = Merb::Config.apply_configuration_from_file options, environment_merb_yml
0
+
0
+ case Merb::Config[:environment].to_s
0
+ when 'production'
0
+ Merb::Config[:reloader] = Merb::Config.fetch(:reloader, false)
0
+ Merb::Config[:exception_details] = Merb::Config.fetch(:exception_details, false)
0
+ Merb::Config[:cache_templates] = true
0
+ else
0
+ Merb::Config[:reloader] = Merb::Config.fetch(:reloader, true)
0
+ Merb::Config[:exception_details] = Merb::Config.fetch(:exception_details, true)
0
+ end
0
+
0
+ Merb::Config[:reloader_time] ||= 0.5 if Merb::Config[:reloader] == true
0
+
0
+
0
+ if Merb::Config[:reloader]
0
+ Thread.abort_on_exception = true
0
+ Thread.new do
0
+ loop do
0
+ sleep( Merb::Config[:reloader_time] )
0
+ ::Merb::BootLoader.reload if ::Merb::BootLoader.app_loaded?
0
+ end
0
+ Thread.exit
0
+ end
0
+ end
0
+ @configuration
0
+ end
0
+
0
+ end # class << self
0
+ end # Config
0
+
0
+end
...
67
68
69
70
 
71
72
73
...
67
68
69
 
70
71
72
73
0
@@ -67,7 +67,7 @@
0
     # ]}}
0
     #---
0
     # @public
0
- def register_extensions(engine, extensions) enforce!(engine => Class, extensions => Array)
0
+ def register_extensions(engine, extensions)
0
       raise ArgumentError, "The class you are registering does not have a compile_template method" unless
0
         engine.respond_to?(:compile_template)
0
       extensions.each{|ext| EXTENSIONS[ext] = engine }
...
1
2
3
 
...
1
2
 
3
0
@@ -1,4 +1,4 @@
0
 corelib = File.join(File.dirname(__FILE__), "core_ext")
0
 
0
-Dir.glob("#{corelib}/*").each {|fn| require(File.join(fn))}
0
+Dir.glob("#{corelib}/*").each {|fn| require fn}
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1 +1,39 @@
0
+require 'rack'
0
+module Merb
0
+ module Rack
0
+ autoload :Adapter, "merb_core/rack/adapter"
0
+ autoload :Mongrel, "merb_core/rack/adapter/mongrel"
0
+ autoload :Thin, "merb_core/rack/adapter/thin"
0
+ autoload :WEBrick, "merb_core/rack/adapter/webrick"
0
+ autoload :FastCGI, "merb_core/rack/adapter/fcgi"
0
+ class RequestWrapper
0
+ def initialize(env)
0
+ @env = env
0
+ end
0
+
0
+ def params
0
+ @env
0
+ end
0
+
0
+ def body
0
+ @env['rack.input']
0
+ end
0
+ end
0
+
0
+ class << self
0
+
0
+ def start(host, ports)
0
+ ports.each do |port|
0
+ start_server(host, port)
0
+ trap("INT"){ Merb.stop }
0
+ end
0
+ end
0
+
0
+ def stop
0
+ end
0
+
0
+ end # class << self
0
+
0
+ end # Rack
0
+end # Merb
...
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
 
...
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
0
@@ -1,90 +1,52 @@
0
 # for OSX compatibility
0
 Socket.do_not_reverse_lookup = true
0
+
0
 module Merb
0
   module Rack
0
- class RequestWrapper
0
- def initialize(env)
0
- @env = env
0
+
0
+ class Adapter
0
+ def initialize(options={})
0
+ @static_server = ::Rack::File.new(::File.join(Merb.root, "public"))
0
       end
0
       
0
- def params
0
- @env
0
- end
0
       
0
- def body
0
- @env['rack.input']
0
- end
0
- end
0
-
0
- class << self
0
+ def call(env)
0
+ path = env['PATH_INFO'].chomp('/')
0
+ cached_path = (path.empty? ? 'index' : path) + '.html'
0
         
0
- def start(host, ports)
0
- ports.each do |port|
0
- start_server(host, port)
0
- trap("INT"){ Merb.stop }
0
- end
0
+ if file_exist?(path) # Serve the file if it's there
0
+ serve_staic(env)
0
+ elsif file_exist?(cached_path) # Serve the page cache if it's there
0
+ env['PATH_INFO'] = cached_path
0
+ serve_staic(env)
0
+ else # No static file, let Merb handle it
0
+ serve_dynamic(env)
0
+ end
0
       end
0
       
0
- def stop
0
+ # TODO refactor this in File#can_serve?(path) ??
0
+ def file_exist?(path)
0
+ full_path = ::File.join(@static_server.root, ::Rack::Utils.unescape(path))
0
+ ::File.file?(full_path) && ::File.readable?(full_path)
0
       end
0
       
0
- end # class << self
0
-
0
- end # Rack
0
-end # Merb
0
+ def serve_staic(env)
0
+ @static_server.call(env)
0
+ end
0
+
0
+ def serve_dynamic(env)
0
+ request = RequestWrapper.new(env)
0
+ response = StringIO.new
0
+ begin
0
+ controller, action = ::Merb::Dispatcher.handle(request, response)
0
+ rescue Object => e
0
+ return [500, {"Content-Type"=>"text/html"}, "Internal Server Error"]
0
+ end
0
+ [controller.status, controller.headers, controller.body]
0
+ end
0
 
0
-
0
-
0
-
0
-class Adapter
0
- def initialize(options={})
0
- @root = options[:root] || Dir.pwd
0
- @env = options[:environment] || 'development'
0
-
0
- load_application
0
-
0
- @static_server = Rack::File.new(::File.join(Merb.root, "public"))
0
- end
0
-
0
- def load_application
0
- ENV['RAILS_ENV'] = @env
0
-
0
- require "#{@root}/config/environment"
0
- require 'dispatcher'
0
- end
0
-
0
- # TODO refactor this in File#can_serve?(path) ??
0
- def file_exist?(path)
0
- full_path = ::File.join(@static_server.root, Utils.unescape(path))
0
- ::File.file?(full_path) && ::File.readable?(full_path)
0
- end
0
-
0
- def serve_staic(env)
0
- @static_server.call(env)
0
- end
0
-
0
- def serve_dynamic(env)
0
- request = RequestWrapper.new(env)
0
- response = StringIO.new
0
- begin
0
- controller, action = ::Merb::Dispatcher.handle(request, response)
0
- rescue Object => e
0
- return [500, {"Content-Type"=>"text/html"}, "Internal Server Error"]
0
+
0
     end
0
- [controller.status, controller.headers, controller.body]
0
- end
0
-
0
- def call(env)
0
- path = env['PATH_INFO'].chomp('/')
0
- cached_path = (path.empty? ? 'index' : path) + 'html'
0
-
0
- if file_exist?(path) # Serve the file if it's there
0
- serve_staic(env)
0
- elsif file_exist?(cached_path) # Serve the page cache if it's there
0
- env['PATH_INFO'] = cached_path
0
- serve_staic(env)
0
- else # No static file, let Merb handle it
0
- serve_dynamic(env)
0
- end
0
- end
0
+ end
0
+end
...
1
2
3
4
5
6
7
 
 
 
8
9
10
...
1
2
3
 
 
 
 
4
5
6
7
8
9
0
@@ -1,10 +1,9 @@
0
 module Merb
0
   module Rack
0
     class FastCGI < Adapter
0
- class << self
0
- def start_server(host=nil, port=nil)
0
- Rack::Handler::FastCGI.run(self)
0
- end
0
+ def self.start_server(host=nil, port=nil)
0
+ app = new
0
+ Rack::Handler::FastCGI.run(app)
0
       end
0
     end
0
   end
...
4
5
6
7
8
9
10
11
12
13
 
 
 
 
 
 
14
15
16
...
4
5
6
 
 
 
 
 
 
 
7
8
9
10
11
12
13
14
15
0
@@ -4,13 +4,12 @@
0
 module Merb
0
   module Rack
0
     class Mongrel < Adapter
0
- class << self
0<