Skip to content

Instantly share code, notes, and snippets.

@wulab
Last active August 29, 2015 14:06
Show Gist options
  • Save wulab/0bac5050b856012855b5 to your computer and use it in GitHub Desktop.
Save wulab/0bac5050b856012855b5 to your computer and use it in GitHub Desktop.
Running Servlets with WEBrick
<pre>
Request to <%= servlet_request.request_uri %>
Query params <%= servlet_request.query.inspect %>
</pre>
require 'webrick'
include WEBrick
server = HTTPServer.new(Port: 8000)
# Add a mime type for *.rhtml files
HTTPUtils::DefaultMimeTypes.store('rhtml', 'text/html')
# Required for CGI on Windows; unnecessary on Unix/Linux
server.config.store(:CGIInterpreter, HTTPServlet::CGIHandler::Ruby)
# Mount servlets
server.mount('/', HTTPServlet::FileHandler, '.')
# Trap signals so as to shutdown cleanly
['TERM', 'INT'].each do |signal|
trap(signal) { server.shutdown }
end
# Start the server and block on input
server.start
#!/bin/sh
ruby -rwebrick -e 'WEBrick::Config::HTTP[:MimeTypes]["rhtml"] = "text/html"' -run -e httpd -- -p 8000 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment