Created
June 13, 2022 14:00
-
-
Save higuma/c0229664a7405fca884f1dd64d2fd620 to your computer and use it in GitHub Desktop.
HTTP dev server with ruby webrick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'webrick' | |
# Add additional MIME types here | |
WEBrick::Config::HTTP[:MimeTypes].update({ | |
'wasm' => 'application/wasm' | |
}) | |
# Defaults | |
ROOT_DEFAULT = './' | |
PORT_DEFAULT = 8000 | |
options = { | |
root: ROOT_DEFAULT, | |
port: PORT_DEFAULT | |
} | |
# Process ENV | |
options[:root] = ENV['ROOT'] if ENV['ROOT'] | |
options[:port] = ENV['PORT'].to_i if ENV['PORT'] | |
# Prepare ARGV parser | |
parser = OptionParser.new | |
parser.version = '0.1.0' | |
parser.on('-r', '--root DIR', String, "root directory (default = #{ROOT_DEFAULT})") do |v| | |
options[:root] = v | |
end | |
parser.on('-p', '--port NUM', Integer, "port number (default = #{PORT_DEFAULT})") do |v| | |
if v < 0 || v > 65535 | |
raise "Invalid port number: #{v} (must be within the range of 0 to 65535)" | |
end | |
options[:port] = v | |
end | |
# Run | |
begin | |
parser.parse! ARGV | |
server = WEBrick::HTTPServer.new({ | |
DocumentRoot: options[:root], | |
Port: options[:port] | |
}) | |
server.start | |
rescue | |
STDERR.puts $! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment