# we must require rubygems because LocalService account don't have RUBYOPT= defined require 'rubygems' require "win32/service" SERVICE_NAME = "MongrelHttp" SERVICE_DISPLAYNAME = "Mongrel Basic HTTP Service" SERVICE_ROOT = File.expand_path(File.dirname(__FILE__)) SERVICE_SCRIPT = File.join(SERVICE_ROOT, __FILE__) if ARGV[0] == "install" require 'rbconfig' argv = [] argv << '"' + Config::CONFIG['bindir'] + '/ruby.exe"' argv << '"' + SERVICE_SCRIPT + '"' svc = Win32::Service.new svc.create_service{ |s| s.service_name = SERVICE_NAME s.display_name = SERVICE_DISPLAYNAME s.binary_path_name = argv.join(' ') s.dependencies = [] } svc.close puts "#{SERVICE_DISPLAYNAME} service installed" elsif ARGV[0] == "start" Win32::Service.start(SERVICE_NAME) started = false while started == false s = Win32::Service.status(SERVICE_NAME) started = true if s.current_state == "running" break if started == true puts "One moment, " + s.current_state sleep 1 end puts "#{SERVICE_DISPLAYNAME} service started" elsif ARGV[0] == "stop" Win32::Service.stop(SERVICE_NAME) stopped = false while stopped == false s = Win32::Service.status(SERVICE_NAME) stopped = true if s.current_state == "stopped" break if stopped == true puts "One moment, " + s.current_state sleep 1 end puts "#{SERVICE_DISPLAYNAME} service stopped" elsif ARGV[0] == "remove" begin Win32::Service.stop(SERVICE_NAME) rescue end begin Win32::Service.delete(SERVICE_NAME) rescue end puts "#{SERVICE_DISPLAYNAME} service deleted" else # This was added to avoid users running the service script form plain command line # (LocalService accound don't have HOMEDRIVE defined.) if ENV["HOMEDRIVE"]!=nil puts "No option provided. You must provide an option. Exiting..." exit end require 'mongrel' require 'yaml' require 'zlib' class SimpleHandler < Mongrel::HttpHandler def process(request, response) response.start do |head,out| head["Content-Type"] = "text/html" results = "
Your request:#{request.params.to_yaml}View the files."
if request.params["HTTP_ACCEPT_ENCODING"] == "gzip,deflate"
head["Content-Encoding"] = "deflate"
# send it back deflated
out << Zlib::Deflate.deflate(results)
else
# no gzip supported, send it back normal
out << results
end
end
end
end
# I commented this until we found a way to log events without problems (maybe file locking mechanism?)
#STDERR.reopen(File.join(File.dirname(__FILE__), 'service.log'), "w")
#STDERR.sync = true
def log(message)
#STDERR.puts Time.now.strftime("%Y-%m-%d %H:%M:%S") + ' - ' + message
end
class HttpDaemon < Win32::Daemon
def service_init
log "** #{SERVICE_DISPLAYNAME} starting..."
@http_server = Mongrel::HttpServer.new("0.0.0.0", 3000)
@simple = SimpleHandler.new
@http_server.register("/", @simple)
@files = Mongrel::DirHandler.new(SERVICE_ROOT)
@http_server.register("/files", @files)
log "** Done initialization."
end
def service_stop
log "** Stop signal received."
end
def service_main
@http_server.run
while state == RUNNING
sleep 1
end
@http_server.stop
end
end
svc = HttpDaemon.new
svc.mainloop
end