require 'launch' require 'webrick' ## # Launch::WEBrickHTTPServer adds launchd support to WEBrick. # # To use, replace WEBrick::HTTPServer with Launch::WEBrickHTTPServer. # # By default Launch::WEBrickHTTPServer expects to find the socket list under # 'WEBrickSockets' but this may be overridden through the # +:LaunchdSockets+ option. # # The server will automatically shut down when a TERM signal is sent. If you # wish to perform other shutdown actions override TERM but be sure to shut # down webrick. # # An example WEBrick server using Launch::WEBrickHTTPServer would be: # # require 'launch/webrick' # # Launch::WEBrickHTTPServer.new(:DocumentRoot => ARGV.shift).start # # Here is an example plist for this server which listens on port 8000: # # # # # # Label # net.segment7.launch.webrick # ProgramArguments # # /path/to/ruby # /path/to/webrick # /Users/your_user/Sites # # ServiceIPC # # Sockets # # WEBrickSockets # # SockServiceName # 8000 # # # # class Launch::WEBrickHTTPServer < WEBrick::HTTPServer include Launch ## # Initializes an HTTP server with +options+ and set the server's listeners # using Launch. A TERM handler to shut down the server is automatically # set. # # +:LaunchdSockets+ may be set to change the socket key from the default of # 'WEBrickSockets' def initialize options = {} options[:DoNotListen] = true sockets_key = options.delete(:LaunchdSockets) || 'WEBrickSockets' super launch_checkin servers = launch_sockets sockets_key, TCPServer listeners.replace servers trap 'TERM' do shutdown end end end