X-Git-Url: http://git.sourceforge.jp/view?p=shogi-server%2Fshogi-server.git;a=blobdiff_plain;f=webserver;h=a993dd948f578bb6196794b8e268127f29eb84f9;hp=90a3cf79a083119bc7525cfbe56afb8bb7288670;hb=bda0cbd86a4e0e7c18fcfaf3a783c584cd7a86ad;hpb=be989f57216e59d862543f18d3662342fdd9faf9 diff --git a/webserver b/webserver index 90a3cf7..a993dd9 100755 --- a/webserver +++ b/webserver @@ -1,4 +1,6 @@ -## Copyright (C) 2007 Daigo Moriwaki +#!/usr/bin/ruby + +## Copyright (C) 2007-2012 Daigo Moriwaki ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by @@ -19,6 +21,13 @@ require 'timeout' require 'thread' require 'webrick' require 'socket' +require 'fileutils' + +def write_pid_file(file) + open(file, "w") do |fh| + fh.puts "#{$$}" + end +end class ShogiClient CLIENT_NAME = "web" @@ -96,16 +105,18 @@ end def usage $stderr.puts <<-EOF -USAGE: #{$0} [--daemon dir] [--port port] +USAGE: #{$0} [--daemon dir] [--port port] [--pid-file file] --daemon dir Run as a daemon. Log files are put in dir --port port Listening port for HTTP server (default 4080) + --pid-file file Write the process id to the file EOF end def parse_command_line options = Hash::new - parser = GetoptLong.new( ["--daemon", GetoptLong::REQUIRED_ARGUMENT], - ["--port", GetoptLong::REQUIRED_ARGUMENT]) + parser = GetoptLong.new( ["--daemon", GetoptLong::REQUIRED_ARGUMENT], + ["--port", GetoptLong::REQUIRED_ARGUMENT], + ["--pid-file", GetoptLong::REQUIRED_ARGUMENT]) parser.quiet = true begin parser.each_option do |name, arg| @@ -117,15 +128,16 @@ def parse_command_line raise parser.error_message end - dir = options["daemon"] || nil + dir = options["daemon"] + dir = File.expand_path(dir) if dir if dir && ! File.exist?(dir) FileUtils.mkdir(dir) end options["dir"] = dir || File.dirname(__FILE__) - options["port"] ||= 4080 options["port"] = options["port"].to_i - + options["pid-file"] = File.expand_path(options["pid-file"]) if options["pid-file"] + return options end @@ -139,11 +151,21 @@ def main http_log_file = File.join($options["dir"], "shogi-server-httpd.log") http_access_log_file = File.join($options["dir"], "shogi-server-access.log") http_config = {} + http_config[:BindAddress] = "0.0.0.0" http_config[:Port] = $options["port"] http_config[:ServerType] = WEBrick::Daemon if $options["daemon"] http_config[:Logger] = WEBrick::Log.new(http_log_file) http_config[:AccessLog] = [[ WEBrick::Log.new(http_access_log_file), WEBrick::AccessLog::COMMON_LOG_FORMAT ]] + if $options["pid-file"] + http_config[:StartCallback] = Proc.new do + write_pid_file($options["pid-file"]) + end + http_config[:StopCallback] = Proc.new do + FileUtils.rm($options["pid-file"], :force => true) + end + end + server = WEBrick::HTTPServer.new(http_config)