documentation cleanup/reduction
[unicorn.git] / lib / unicorn.rb
blob8a5fdcc5dd6ae5da3e18f581ec529c44d70b24d7
1 # -*- encoding: binary -*-
2 require 'fcntl'
3 require 'etc'
4 require 'stringio'
5 require 'rack'
6 require 'kgio'
8 # :stopdoc:
9 # Unicorn module containing all of the classes (include C extensions) for
10 # running a Unicorn web server.  It contains a minimalist HTTP server with just
11 # enough functionality to service web application requests fast as possible.
12 # :startdoc:
14 # \Unicorn exposes very little of an user-visible API and most of its
15 # internals are subject to change.  \Unicorn is designed to host Rack
16 # applications, so applications should be written against the Rack SPEC
17 # and not \Unicorn internals.
18 module Unicorn
20   # Raised inside TeeInput when a client closes the socket inside the
21   # application dispatch.  This is always raised with an empty backtrace
22   # since there is nothing in the application stack that is responsible
23   # for client shutdowns/disconnects.  This exception is visible to Rack
24   # applications unless PrereadInput middleware is loaded.
25   class ClientShutdown < EOFError
26   end
28   # :stopdoc:
29   def self.run(app, options = {})
30     Unicorn::HttpServer.new(app, options).start.join
31   end
33   # This returns a lambda to pass in as the app, this does not "build" the
34   # app (which we defer based on the outcome of "preload_app" in the
35   # Unicorn config).  The returned lambda will be called when it is
36   # time to build the app.
37   def self.builder(ru, opts)
38     # allow Configurator to parse cli switches embedded in the ru file
39     Unicorn::Configurator::RACKUP.update(:file => ru, :optparse => opts)
41     # always called after config file parsing, may be called after forking
42     lambda do ||
43       inner_app = case ru
44       when /\.ru$/
45         raw = File.read(ru)
46         raw.sub!(/^__END__\n.*/, '')
47         eval("Rack::Builder.new {(#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
48       else
49         require ru
50         Object.const_get(File.basename(ru, '.rb').capitalize)
51       end
53       pp({ :inner_app => inner_app }) if $DEBUG
55       # return value, matches rackup defaults based on env
56       case ENV["RACK_ENV"]
57       when "development"
58         Rack::Builder.new do
59           use Rack::CommonLogger, $stderr
60           use Rack::ShowExceptions
61           use Rack::Lint
62           run inner_app
63         end.to_app
64       when "deployment"
65         Rack::Builder.new do
66           use Rack::CommonLogger, $stderr
67           run inner_app
68         end.to_app
69       else
70         inner_app
71       end
72     end
73   end
75   # returns an array of strings representing TCP listen socket addresses
76   # and Unix domain socket paths.  This is useful for use with
77   # Raindrops::Middleware under Linux: http://raindrops.bogomips.org/
78   def self.listener_names
79     Unicorn::HttpServer::LISTENERS.map do |io|
80       Unicorn::SocketHelper.sock_name(io)
81     end
82   end
83   # :startdoc:
84 end
85 # :enddoc:
86 require 'unicorn/const'
87 require 'unicorn/socket_helper'
88 require 'unicorn/stream_input'
89 require 'unicorn/tee_input'
90 require 'unicorn/http_request'
91 require 'unicorn/configurator'
92 require 'unicorn/tmpio'
93 require 'unicorn/util'
94 require 'unicorn/http_response'
95 require 'unicorn/worker'
96 require 'unicorn/http_server'