Skip to content

Instantly share code, notes, and snippets.

@thinkAmi
Created Aug 19, 2014
Embed
What would you like to do?
Windows7 + WEBrick + apcupsdで、UPSの状態をブラウザから確認する
# 起動
# C:\apcupsd\cgi>ruby server.rb
# アクセス
# http://localhost:10080/multimon.cgi
require 'webrick'
DOCUMENT_ROOT = 'C:/apcupsd/cgi'
server = WEBrick::HTTPServer.new({
DocumentRoot: DOCUMENT_ROOT,
BindAddress: '0.0.0.0',
Port: 10080
})
# DocumentRootに含まれる`.cgi`ファイルが、CGIで使うファイル
cgi_files = Dir.entries(DOCUMENT_ROOT).select{ |f| File.extname(f) == '.cgi' }
cgi_files.each {|cgi_file|
server.mount(cgi_file, WEBrick::HTTPServlet::CGIHandler, File.join(DOCUMENT_ROOT, cgi_file))
}
# p Signal.list で、以下が定義されていることが分かる
# {"EXIT"=>0, "INT"=>2, "ILL"=>4, "ABRT"=>22, "FPE"=>8, "KILL"=>9, "SEGV"=>11, "TERM"=>15}
# とりあえずは、SIGINTとSIGTERMにのみ反応
%i[INT TERM].each {|signal|
Signal.trap(signal){ server.shutdown }
}
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment