Last active
December 21, 2015 07:59
-
-
Save tompave/6275405 to your computer and use it in GitHub Desktop.
A simple ruby HTTP server that listens on 127.0.0.1:80 and redirects all requests to a specific URL.
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
#! /usr/bin/env ruby | |
require 'webrick' | |
require 'uri' | |
TARGET_URL = URI.parse "http://tommaso.pavese.me/back_to_work/" | |
redirect_callback = Proc.new do |request, response| | |
response.set_redirect WEBrick::HTTPStatus::TemporaryRedirect, TARGET_URL | |
end | |
# signal handling | |
trap("INT") { @server.shutdown } | |
trap("TERM") { @server.shutdown } | |
begin | |
@server = WEBrick::HTTPServer.new :Port => 80, :RequestCallback => redirect_callback | |
Process.daemon # comment this line if managed by launchd | |
@server.start | |
rescue Errno::EACCES | |
puts "Can't bind to port 80 without sudo" | |
rescue StandardError | |
# do nothing, then exit | |
ensure | |
@server.shutdown if @server | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment