Skip to content

Instantly share code, notes, and snippets.

@derrickwilliams
Created November 6, 2017 21:39
Show Gist options
  • Select an option

  • Save derrickwilliams/2a76b700e5de6145b7ec8b0e14b12266 to your computer and use it in GitHub Desktop.

Select an option

Save derrickwilliams/2a76b700e5de6145b7ec8b0e14b12266 to your computer and use it in GitHub Desktop.

Revisions

  1. derrickwilliams created this gist Nov 6, 2017.
    32 changes: 32 additions & 0 deletions simple-go-proxy.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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)
    }