Created
May 12, 2011 23:11
-
-
Save gnrlbzik/969655 to your computer and use it in GitHub Desktop.
WEBRick server script
This file contains 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
# WEBRick server code | |
# - reading file specified per path, servlet then dynamically loads file and erb interprets the code inside of the .rhtml. | |
require 'webrick' | |
include WEBrick | |
require 'erb' | |
WEBrick::HTTPUtils::DefaultMimeTypes['rhtml'] = 'text/html' | |
server = WEBrick::HTTPServer.new :Port => 3000, :DocumentRoot => Dir.pwd | |
class CustomServlet < HTTPServlet::AbstractServlet | |
def do_GET(request, response) | |
response.status = 200 # Success | |
File.open(request.path.to_s.sub(/\//, ''),'r') do |f| | |
@template = ERB.new(f.read) | |
end | |
response.body = @template.result(binding) | |
response['Content-Type'] = "text/html" | |
end | |
# Respond with an HTTP POST just as we do for the HTTP GET. | |
alias :do_POST :do_GET | |
end | |
server.mount('', CustomServlet) | |
yield server if block_given? | |
trap 'INT' do server.shutdown end | |
server.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment