Skip to content

Instantly share code, notes, and snippets.

@a2ikm
Last active August 29, 2015 14:20
Show Gist options
  • Save a2ikm/406f5aade7f934bed922 to your computer and use it in GitHub Desktop.
Save a2ikm/406f5aade7f934bed922 to your computer and use it in GitHub Desktop.
WEBrickを使ったdaemonなWebサーバ
#!/usr/bin/env ruby
#
# -*- mode: ruby -*-
# vi: set ft=ruby :
require "optparse"
require "webrick"
include WEBrick
Process.setproctitle(File.basename(__FILE__)) if Process.respond_to?(:setproctitle)
options = {
:Port => 8000,
:DocumentRoot => Dir.pwd,
:BindAddress => "0.0.0.0",
}
OptionParser.new do |opt|
opt.on "-d", "--[no-]daemon" do |v|
options[:ServerType] = Daemon if v
end
opt.on "-p", "--port PORT", Integer do |v|
options[:Port] = v
end
opt.on "-r", "--root PATH" do |v|
options[:DocumentRoot] = v
end
opt.on "-l", "--log [PATH]" do |v|
v ||= "access.log"
path = File.expand_path(v, Dir.pwd)
f = File.open(path, "a")
f.sync = true
options[:AccessLog] = [
[f, WEBrick::AccessLog::COMBINED_LOG_FORMAT]
]
end
end.parse!(ARGV)
server = HTTPServer.new(options)
%w(INT TERM).each do |sig|
trap(sig) { server.shutdown }
end
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment