Class: SC::Rack::Service
Overview
Configures a standard set of Rack adaptors for serving SproutCore apps. Based on your config settings, this will configure appropriate middlewear apps for each incoming project. Requests are then cascaded through the projects to find one that can service the request.
Class Method Summary collapse
-
.start(opts = {}) ⇒ Object
Convenience method to start the rack service as a server.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(*projects) ⇒ Service
constructor
A new instance of Service.
-
#middleware_for(project) ⇒ Object
Construct new middleware for the named project.
Constructor Details
#initialize(*projects) ⇒ Service
Returns a new instance of Service.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/sproutcore/rack/service.rb', line 96 def initialize(*projects) opts = projects.pop @projects = projects.flatten # Get apps for each project & cascade if needed @apps = @projects.map { |project| middleware_for(project) } @app = @apps.size == 1 ? @apps.first : ::Rack::Cascade.new(@apps) # Now put it behind some useful general optimizers... @app = ::Rack::Recursive.new(@app) @app = ::Rack::ConditionalGet.new(@app) #@app = ::Rack::Deflater.new(@app) # preprocess IPs so we can restrict properly ips = opts[:allow_from_ips] || '127.0.0.1' SC.logger << "Allowing access only from IPs: #{ips}. Use --allow-from-ips='*.*.*.*' to allow all\n" ips = ips.split(',') @app = SC::Rack::RestrictIP.new(@app, ips) end |
Class Method Details
.start(opts = {}) ⇒ Object
Convenience method to start the rack service as a server. You must pass at least a :project => foo, or :projects => [foo, bar] option to this method, but you may also pass a number of other config optios including:
Options
:host:: the hostname to listen on (default 0.0.0.0)
:port:: the portname to listen on (default 4020)
:daemonize:: if true, service will be run as daemon
:pid:: if daemonizing, this is the path of the file to write pid to
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 |
# File 'lib/sproutcore/rack/service.rb', line 31 def self.start(opts = {}) # load rack begin require 'rack' rescue LoadError => e gem 'rack' require 'rack' end # Guess. if ENV.include?("PHP_FCGI_CHILDREN") server = ::Rack::Handler::FastCGI # We already speak FastCGI .delete :File .delete :Port elsif ENV.include?("REQUEST_METHOD") server = ::Rack::Handler::CGI else begin server = ::Rack::Handler::Thin rescue LoadError => e server = ::Rack::Handler::Mongrel end end opts[:Filesystem] ||= opts[:filesystem] || true # enabled by default self.filesystem = opts[:Filesystem] projects = opts.delete(:projects) || [opts.delete(:project)].compact app = self.new(*(projects+[opts])) opts[:Host] ||= opts[:host] # allow either case. opts[:Port] ||= opts[:port] || '4020' # If daemonize is set, do it... if opts[:daemonize] SC.logger.info "Daemonizing..." pid = opts[:pid] if RUBY_VERSION < "1.9" return if fork Process.setsid return if fork Dir.chdir "/" File.umask 0000 STDIN.reopen "/dev/null" STDOUT.reopen "/dev/null", "a" STDERR.reopen "/dev/null", "a" else Process.daemon end if pid File.open(pid, 'w'){ |f| f.write("#{Process.pid}") } at_exit { File.delete(pid) if File.exist?(pid) } end end SC.logger << "Starting server at http://#{opts[:Host] || '0.0.0.0'}:#{opts[:Port]} in #{SC.build_mode} mode\n" SC.logger << "To quit sc-server, press Control-C\n" server.run app, opts end |
Instance Method Details
#call(env) ⇒ Object
117 |
# File 'lib/sproutcore/rack/service.rb', line 117 def call(env); @app.call(env); end |
#middleware_for(project) ⇒ Object
Construct new middleware for the named project
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 |
# File 'lib/sproutcore/rack/service.rb', line 120 def middleware_for(project) apps = [] # setup some conditional items... config = project.config # serve files out of the public directory if serve_public is # configures && the public directory exists if config[:serve_public] pubdir = File.join(project.project_root, 'public') apps << ::Rack::File.new(pubdir) if File.directory?(pubdir) end if self.filesystem apps << SC::Rack::Filesystem.new(project) end #if config.serve_test_runner apps << SC::Rack::Dev.new(project) #end # Add builder for the project itself apps << SC::Rack::Builder.new(project) if project.buildfile.proxies.size > 0 apps << SC::Rack::Proxy.new(project) end # Wrap'em in a cascade if needed. This will return the first # app that does not return nil app = apps.size == 1 ? apps.first : ::Rack::Cascade.new(apps) # Add show exceptions handler if enabled app = ::Rack::ShowExceptions.new(app) if config[:serve_exceptions] return app # done! end |