Created
August 8, 2015 13:18
-
-
Save Codesleuth/6c47316bea68ca3722ff to your computer and use it in GitHub Desktop.
Go HTTP Proxy
This file contains hidden or 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
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