lighttpd.rb 2.8 KB
Newer Older
1
require 'rbconfig'
2
require 'commands/servers/base'
3

4
unless RUBY_PLATFORM !~ /mswin/ && !silence_stderr { `lighttpd -version` }.blank?
5 6 7 8 9 10
  puts "PROBLEM: Lighttpd is not available on your system (or not in your path)"
  exit 1
end

unless defined?(FCGI)
  puts "PROBLEM: Lighttpd requires that the FCGI Ruby bindings are installed on the system"
11 12 13
  exit 1
end

14 15
require 'initializer'
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
user_defined_active_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 26
  opt.on('-c', "--config=#{config_file}", 'Specify a different lighttpd config file.') { |path| config_file = path }
  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 29
  opt.parse!
end
30

31
unless File.exist?(config_file)
32 33 34 35
  if config_file != default_config_file
    puts "=> #{config_file} not found."
    exit 1
  end
36

37
  require 'fileutils'
38

39 40 41
  source = File.expand_path(File.join(File.dirname(__FILE__),
     "..", "..", "..", "configs", "lighttpd.conf"))
  puts "=> #{config_file} not found, copying from #{source}"
42 43

  FileUtils.cp(source, config_file)
44 45
end

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
# open the config/lighttpd.conf file and add the current user defined port setting to it
if command_line_port
  File.open(config_file, 'r+') do |config|
    lines = config.readlines

    lines.each do |line|
      line.gsub!(/^\s*server.port\s*=\s*(\d+)/, "server.port = #{command_line_port}")
    end

    config.rewind
    config.print(lines)
    config.truncate(config.pos)
  end
end

61 62
config = IO.read(config_file)
default_port, default_ip = 3000, '0.0.0.0'
63 64
port = config.scan(/^\s*server.port\s*=\s*(\d+)/).first rescue default_port
ip   = config.scan(/^\s*server.bind\s*=\s*"([^"]+)"/).first rescue default_ip
65
puts "=> Rails application started on http://#{ip || default_ip}:#{port || default_port}"
66

67 68
tail_thread = nil

69
if !detach
70
  puts "=> Call with -d to detach"
71 72
  puts "=> Ctrl-C to shutdown server (see config/lighttpd.conf for options)"
  detach = false
73
  tail_thread = tail(configuration.log_path)
74 75
end

76
trap(:INT) { exit }
77 78

begin
79
  `rake tmp:sockets:clear` # Needed if lighttpd crashes or otherwise leaves FCGI sockets around
80 81
  `lighttpd #{!detach ? "-D " : ""}-f #{config_file}`
ensure
82 83 84
  unless detach
    tail_thread.kill if tail_thread
    puts 'Exiting'
85
  
86
    # Ensure FCGI processes are reaped
87 88 89 90 91 92
    silence_stream(STDOUT) do
      ARGV.replace ['-a', 'kill']
      require 'commands/process/reaper'
    end

    `rake tmp:sockets:clear` # Remove sockets on clean shutdown
93
  end
94
end