Last active
May 2, 2024 13:27
-
-
Save JalfResi/6287706 to your computer and use it in GitHub Desktop.
Simple reverse proxy in Go
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
package main | |
import( | |
"log" | |
"net/url" | |
"net/http" | |
"net/http/httputil" | |
) | |
func main() { | |
remote, err := url.Parse("http://google.com") | |
if err != nil { | |
panic(err) | |
} | |
handler := func(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) { | |
return func(w http.ResponseWriter, r *http.Request) { | |
log.Println(r.URL) | |
r.Host = remote.Host | |
w.Header().Set("X-Ben", "Rad") | |
p.ServeHTTP(w, r) | |
} | |
} | |
proxy := httputil.NewSingleHostReverseProxy(remote) | |
http.HandleFunc("/", handler(proxy)) | |
err = http.ListenAndServe(":8080", nil) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Modified it to use struct and ssl
https://gist.github.com/c0d3kid/862276a6291e4aeaf2638e5f7e93a954