allow multiline comments in config.ru
[unicorn.git] / lib / unicorn.rb
blob2849ffc0333a66cd6dfa94df5cb8469d1149bd42
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:
30   # This returns a lambda to pass in as the app, this does not "build" the
31   # app (which we defer based on the outcome of "preload_app" in the
32   # Unicorn config).  The returned lambda will be called when it is
33   # time to build the app.
34   def self.builder(ru, op)
35     # allow Configurator to parse cli switches embedded in the ru file
36     op = Unicorn::Configurator::RACKUP.merge!(:file => ru, :optparse => op)
38     # always called after config file parsing, may be called after forking
39     lambda do ||
40       inner_app = case ru
41       when /\.ru$/
42         raw = File.read(ru)
43         raw.sub!(/^__END__\n.*/, '')
44         eval("Rack::Builder.new {(\n#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
45       else
46         require ru
47         Object.const_get(File.basename(ru, '.rb').capitalize)
48       end
50       pp({ :inner_app => inner_app }) if $DEBUG
52       # return value, matches rackup defaults based on env
53       case ENV["RACK_ENV"]
54       when "development"
55         Rack::Builder.new do
56           use Rack::CommonLogger, $stderr
57           use Rack::ShowExceptions
58           use Rack::Lint
59           run inner_app
60         end.to_app
61       when "deployment"
62         Rack::Builder.new do
63           use Rack::CommonLogger, $stderr
64           run inner_app
65         end.to_app
66       else
67         inner_app
68       end
69     end
70   end
72   # returns an array of strings representing TCP listen socket addresses
73   # and Unix domain socket paths.  This is useful for use with
74   # Raindrops::Middleware under Linux: http://raindrops.bogomips.org/
75   def self.listener_names
76     Unicorn::HttpServer::LISTENERS.map do |io|
77       Unicorn::SocketHelper.sock_name(io)
78     end
79   end
81   def self.log_error(logger, message, exc)
82     logger.error "#{message}: #{exc.message} (#{exc.class})"
83     exc.backtrace.each { |line| logger.error(line) }
84   end
85   # :startdoc:
86 end
87 # :enddoc:
88 require 'unicorn/const'
89 require 'unicorn/socket_helper'
90 require 'unicorn/stream_input'
91 require 'unicorn/tee_input'
92 require 'unicorn/http_request'
93 require 'unicorn/configurator'
94 require 'unicorn/tmpio'
95 require 'unicorn/util'
96 require 'unicorn/http_response'
97 require 'unicorn/worker'
98 require 'unicorn/http_server'