public
Rubygem
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/somebee/merb-core.git
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
...
63
64
65
66
 
67
68
69
...
63
64
65
 
66
67
68
69
0
@@ -63,7 +63,7 @@ spec = Gem::Specification.new do |s|
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,4 +1,5 @@
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
30
...
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
0
@@ -4,27 +4,52 @@
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
0
     # use the app/:type layout that is in use in Merb 0.5
...
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 @@ class Merb::BootLoader::BuildFramework < Merb::BootLoader
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 @@ class Merb::BootLoader::Templates < Merb::BootLoader
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 @@ class Merb::BootLoader::Libraries < Merb::BootLoader
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?
...
67
68
69
70
 
71
72
73
...
67
68
69
 
70
71
72
73
0
@@ -67,7 +67,7 @@ module Merb::Template
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
4
 
5
...
1
2
 
3
4
5
0
@@ -1,3 +1,3 @@
0
 corelib = File.join(File.dirname(__FILE__), "core_ext")
0
 
0
-Dir.glob("#{corelib}/*").each {|fn| require(File.join(fn))}
0
\ No newline at end of file
0
+Dir.glob("#{corelib}/*").each {|fn| require fn}
0
\ No newline at end of file
...
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
...
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
0
@@ -1,89 +1,51 @@
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
-
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
+ 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
- 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
- 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
+
0
     end
0
- end
0
\ No newline at end of file
0
+ end
0
+end
0
\ No newline at end of file
...
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 @@ require 'rack/handler/mongrel'
0
 module Merb
0
   module Rack
0
     class Mongrel < Adapter
0
- class << self
0
- # start server on given host and port.
0
- def start_server(host, port)
0
- server = ::Mongrel::HttpServer.new(host, port)
0
- server.register('/', ::Rack::Handler::Mongrel.new(self))
0
- server.run.join
0
- end
0
+ # start server on given host and port.
0
+ def self.start_server(host, port)
0
+ app = new
0
+ server = ::Mongrel::HttpServer.new(host, port)
0
+ server.register('/', ::Rack::Handler::Mongrel.new(app))
0
+ server.run.join
0
       end
0
     end
0
   end
...
3
4
5
6
7
8
9
10
11
12
13
 
 
 
 
 
 
 
14
15
16
...
3
4
5
 
 
 
 
 
 
 
 
6
7
8
9
10
11
12
13
14
15
0
@@ -3,14 +3,13 @@ require 'thin'
0
 module Merb
0
   module Rack
0
     class Mongrel < Adapter
0
- class << self
0
- # start server on given host and port.
0
- def start_server(host, port)
0
- server = ::Thin::Server.new(host, port, self)
0
- server.silent = true
0
- server.timeout = 3
0
- server.start!
0
- end
0
+ # start a Thin server on given host and port.
0
+ def self.start_server(host, port)
0
+ app = new
0
+ server = ::Thin::Server.new(host, port, app)
0
+ server.silent = true
0
+ server.timeout = 3
0
+ server.start!
0
       end
0
     end
0
   end
...
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
26
27
...
4
5
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
0
@@ -4,24 +4,22 @@ require 'rack/handler/webrick'
0
 module Merb
0
   module Rack
0
     class WEBrick < Adapter
0
- class << self
0
- # start server on given host and port.
0
- def start_server(host, port)
0
- options = {
0
- :Port => port,
0
- :BindAddress => host,
0
- :Logger => Merb.logger,
0
- :AccessLog => [
0
- [Merb.logger, ::WEBrick::AccessLog::COMMON_LOG_FORMAT],
0
- [Merb.logger, ::WEBrick::AccessLog::REFERER_LOG_FORMAT]
0
- ]
0
- }.merge(options)
0
-
0
-
0
- server = ::WEBrick::HTTPServer.new(options)
0
- server.mount("/", ::Rack::Handler::WEBrick, self)
0
- server.start
0
- end
0
+ # start WEBrick server on given host and port.
0
+ def self.start_server(host, port)
0
+ app = new
0
+ options = {
0
+ :Port => port,
0
+ :BindAddress => host,
0
+ :Logger => Merb.logger,
0
+ :AccessLog => [
0
+ [Merb.logger, ::WEBrick::AccessLog::COMMON_LOG_FORMAT],
0
+ [Merb.logger, ::WEBrick::AccessLog::REFERER_LOG_FORMAT]
0
+ ]
0
+ }
0
+
0
+ server = ::WEBrick::HTTPServer.new(options)
0
+ server.mount("/", ::Rack::Handler::WEBrick, app)
0
+ server.start
0
       end
0
     end
0
   end

Comments

    No one has commented yet.