Skip to content

Instantly share code, notes, and snippets.

@hkbu-kennycheng
Forked from JalfResi/revprox.go
Created October 25, 2024 15:41
Show Gist options
  • Select an option

  • Save hkbu-kennycheng/e48a8a545d25e24f1b5de8bc8281b924 to your computer and use it in GitHub Desktop.

Select an option

Save hkbu-kennycheng/e48a8a545d25e24f1b5de8bc8281b924 to your computer and use it in GitHub Desktop.

Revisions

  1. @JalfResi JalfResi revised this gist Jul 16, 2021. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions revprox.go
    Original file line number Diff line number Diff line change
    @@ -13,19 +13,19 @@ func main() {
    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)
    }
    }

    func handler(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)
    }
    }
  2. @JalfResi JalfResi revised this gist Jun 23, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions revprox.go
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,7 @@ func main() {
    func handler(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)
    }
  3. @JalfResi JalfResi created this gist Aug 20, 2013.
    30 changes: 30 additions & 0 deletions revprox.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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)
    }

    proxy := httputil.NewSingleHostReverseProxy(remote)
    http.HandleFunc("/", handler(proxy))
    err = http.ListenAndServe(":8080", nil)
    if err != nil {
    panic(err)
    }
    }

    func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
    return func(w http.ResponseWriter, r *http.Request) {
    log.Println(r.URL)
    w.Header().Set("X-Ben", "Rad")
    p.ServeHTTP(w, r)
    }
    }