Class: SC::Rack::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/sproutcore/rack/service.rb

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

Instance Method Summary collapse

Constructor Details

#initialize(*projects) ⇒ Service

Returns a new instance of Service.



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sproutcore/rack/service.rb', line 96

def initialize(*projects)
  @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)
  
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
    options.delete :File
    options.delete :Port
  elsif ENV.include?("REQUEST_METHOD")
    server = ::Rack::Handler::CGI
  else
    begin
      server = ::Rack::Handler::Mongrel
    rescue LoadError => e
      server = ::Rack::Handler::WEBrick
    end
  end
  
  opts[:Filesystem] ||= opts[:filesystem] || false # allow either case
  self.filesystem = opts[:Filesystem]
  
  projects = opts.delete(:projects) || [opts.delete(:project)].compact
  app = self.new(*projects)
  
  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



110
# File 'lib/sproutcore/rack/service.rb', line 110

def call(env); @app.call(env); end

#middleware_for(project) ⇒ Object

Construct new middleware for the named project



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
# File 'lib/sproutcore/rack/service.rb', line 113

def middleware_for(project)
  apps = []

  # setup some conditional items...
  config = project.config
  #if config.serve_test_runner || config.serve_docs
    apps << SC::Rack::Dev.new(project) 
  #end
  if project.buildfile.proxies.size > 0
    apps << SC::Rack::Proxy.new(project) 
  end

  # Add builder for the project itself
  apps  << SC::Rack::Builder.new(project)
  
  # 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
  
  # 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