Created
January 18, 2018 14:51
-
-
Save derrickwilliams/a14003c7c54be45b828a85128f8dfcf1 to your computer and use it in GitHub Desktop.
golang proxy example
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" | |
| "log" | |
| "net/http" | |
| "net/http/httputil" | |
| "net/url" | |
| "github.com/darkhelmet/env" | |
| ) | |
| var ( | |
| port = env.IntDefault("PORT", 5000) | |
| ) | |
| func main() { | |
| proxyUrl, err := url.Parse(env.String("PROXY_URL")) | |
| if err != nil { | |
| log.Fatalf("failed parsing url: %s", err) | |
| } | |
| proxy := &httputil.ReverseProxy{ | |
| Director: func(req *http.Request) { | |
| req.URL.Scheme = proxyUrl.Scheme | |
| req.URL.Host = proxyUrl.Host | |
| req.URL.Path = req.URL.Path | |
| req.Host = proxyUrl.Host | |
| }, | |
| } | |
| log.Printf("listening on port %d", port) | |
| http.ListenAndServe(fmt.Sprintf(":%d", port), proxy) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
copied from
darkhelmet/proxy