Skip to content

Instantly share code, notes, and snippets.

@Codesleuth
Created August 8, 2015 13:18
Show Gist options
  • Save Codesleuth/6c47316bea68ca3722ff to your computer and use it in GitHub Desktop.
Save Codesleuth/6c47316bea68ca3722ff to your computer and use it in GitHub Desktop.
Go HTTP Proxy
package main
import (
"fmt"
"net/http"
"net/http/httputil"
)
func HttpRouteHandler(responseWriter http.ResponseWriter, request *http.Request) {
fmt.Println("# recieved request for ", request.URL)
fmt.Println(" headers:")
for key, val := range request.Header {
fmt.Println("\t", key, ":", val)
}
request.URL.Scheme = "http"
request.URL.Host = request.Host
fmt.Print(" routing request to ", request.URL, "...")
proxy := httputil.NewSingleHostReverseProxy(request.URL)
proxy.ServeHTTP(responseWriter, request)
fmt.Println(" done.")
fmt.Println()
}
func main() {
address := ":1080"
fmt.Print("Starting proxy on ", address, "...")
http.HandleFunc("/", HttpRouteHandler)
http.ListenAndServe(address, nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment