Froze rails gems
[depot.git] / vendor / rails / railties / lib / commands / servers / lighttpd.rb
1 require 'rbconfig'
2 require 'commands/servers/base'
3
4 unless RUBY_PLATFORM !~ /mswin/ && !silence_stderr { `lighttpd -version` }.blank?
5 puts "PROBLEM: Lighttpd is not available on your system (or not in your path)"
6 exit 1
7 end
8
9 unless defined?(FCGI)
10 puts "PROBLEM: Lighttpd requires that the FCGI Ruby bindings are installed on the system"
11 exit 1
12 end
13
14 require 'initializer'
15 configuration = Rails::Initializer.run(:initialize_logger).configuration
16 default_config_file = config_file = Pathname.new("#{RAILS_ROOT}/config/lighttpd.conf").cleanpath
17
18 require 'optparse'
19
20 detach = false
21 command_line_port = nil
22
23 ARGV.options do |opt|
24 opt.on("-p", "--port=port", "Changes the server.port number in the config/lighttpd.conf") { |port| command_line_port = port }
25 opt.on('-c', "--config=#{config_file}", 'Specify a different lighttpd config file.') { |path| config_file = path }
26 opt.on('-h', '--help', 'Show this message.') { puts opt; exit 0 }
27 opt.on('-d', '-d', 'Call with -d to detach') { detach = true; puts "=> Configuration in config/lighttpd.conf" }
28 opt.parse!
29 end
30
31 unless File.exist?(config_file)
32 if config_file != default_config_file
33 puts "=> #{config_file} not found."
34 exit 1
35 end
36
37 require 'fileutils'
38
39 source = File.expand_path(File.join(File.dirname(__FILE__),
40 "..", "..", "..", "configs", "lighttpd.conf"))
41 puts "=> #{config_file} not found, copying from #{source}"
42
43 FileUtils.cp(source, config_file)
44 end
45
46 # open the config/lighttpd.conf file and add the current user defined port setting to it
47 if command_line_port
48 File.open(config_file, 'r+') do |config|
49 lines = config.readlines
50
51 lines.each do |line|
52 line.gsub!(/^\s*server.port\s*=\s*(\d+)/, "server.port = #{command_line_port}")
53 end
54
55 config.rewind
56 config.print(lines)
57 config.truncate(config.pos)
58 end
59 end
60
61 config = IO.read(config_file)
62 default_port, default_ip = 3000, '0.0.0.0'
63 port = config.scan(/^\s*server.port\s*=\s*(\d+)/).first rescue default_port
64 ip = config.scan(/^\s*server.bind\s*=\s*"([^"]+)"/).first rescue default_ip
65 puts "=> Rails #{Rails.version} application starting on http://#{ip || default_ip}:#{port || default_port}"
66
67 tail_thread = nil
68
69 if !detach
70 puts "=> Call with -d to detach"
71 puts "=> Ctrl-C to shutdown server (see config/lighttpd.conf for options)"
72 detach = false
73 tail_thread = tail(configuration.log_path)
74 end
75
76 trap(:INT) { exit }
77
78 begin
79 `rake tmp:sockets:clear` # Needed if lighttpd crashes or otherwise leaves FCGI sockets around
80 `lighttpd #{!detach ? "-D " : ""}-f #{config_file}`
81 ensure
82 unless detach
83 tail_thread.kill if tail_thread
84 puts 'Exiting'
85
86 # Ensure FCGI processes are reaped
87 silence_stream(STDOUT) do
88 ARGV.replace ['-a', 'kill']
89 require 'commands/process/reaper'
90 end
91
92 `rake tmp:sockets:clear` # Remove sockets on clean shutdown
93 end
94 end