Skip to content

Instantly share code, notes, and snippets.

@yous
Forked from chrismdp/http
Last active December 15, 2017 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yous/897f88a1707b5f5859ea39a8b1d09330 to your computer and use it in GitHub Desktop.
Save yous/897f88a1707b5f5859ea39a8b1d09330 to your computer and use it in GitHub Desktop.
Simple http server in ruby, that ensures there's no browser caching. For serving static files in development.
#!/usr/bin/env ruby
require 'webrick'
require 'optparse'
class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler
def prevent_caching(res)
res['ETag'] = nil
res['Last-Modified'] = Time.now + 100**4
res['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
res['Pragma'] = 'no-cache'
res['Expires'] = Time.now - 100**4
end
def do_GET(req, res)
super
prevent_caching(res)
end
end
options = { port: 8000 }
OptionParser.new do |opts|
opts.banner = 'Usage: server [options]'
opts.on('-p', '--port PORT', 'Specify the port') { |v| options[:port] = v }
end.parse!
server = WEBrick::HTTPServer.new(Port: options[:port])
server.mount('/', NonCachingFileHandler, Dir.pwd)
trap('INT') { server.stop }
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment