lighttpd.rb 2.3 KB
Newer Older
1 2
require 'rbconfig'

3
unless RUBY_PLATFORM !~ /mswin/ && !silence_stderr { `lighttpd -version` }.blank?
4 5 6 7 8 9
  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"
10 11 12
  exit 1
end

13 14
require 'initializer'
configuration = Rails::Initializer.run(:initialize_logger).configuration
15
default_config_file = config_file = "#{RAILS_ROOT}/config/lighttpd.conf"
16

17 18 19 20 21 22
require 'optparse'
ARGV.options do |opt|
  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 }
  opt.parse!
end
23

24
unless File.exist?(config_file)
25 26 27 28
  if config_file != default_config_file
    puts "=> #{config_file} not found."
    exit 1
  end
29 30 31 32 33 34 35
  require 'fileutils'
  source = File.expand_path(File.join(File.dirname(__FILE__),
     "..", "..", "..", "configs", "lighttpd.conf"))
  puts "=> #{config_file} not found, copying from #{source}"
  FileUtils.cp source, config_file
end

36 37
config = IO.read(config_file)
default_port, default_ip = 3000, '0.0.0.0'
38 39
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
40
puts "=> Rails application started on http://#{ip || default_ip}:#{port || default_port}"
41

42 43
tail_thread = nil

44
if ARGV.first == "-d"
45
  puts "=> Configuration in config/lighttpd.conf"
46 47
  detach = true
else
48
  puts "=> Call with -d to detach"
49 50 51
  puts "=> Ctrl-C to shutdown server (see config/lighttpd.conf for options)"
  detach = false

52
  cursor = File.size(configuration.log_path)
53 54
  last_checked = Time.now
  tail_thread = Thread.new do
55
    File.open(configuration.log_path, 'r') do |f|
56 57 58 59 60 61 62 63 64
      loop do
        f.seek cursor
        if f.mtime > last_checked
          last_checked = f.mtime
          contents = f.read
          cursor += contents.length
          print contents
        end
        sleep 1
65 66
      end
    end
67
  end
68 69
end

70
trap(:INT) { exit }
71 72 73 74

begin
  `lighttpd #{!detach ? "-D " : ""}-f #{config_file}`
ensure
75 76 77
  unless detach
    tail_thread.kill if tail_thread
    puts 'Exiting'
78
  
79
    # Ensure FCGI processes are reaped
80 81
    ARGV.replace ['-a', 'kill']
    require 'commands/process/reaper'
82
  end
83
end