-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (53 loc) · 1.34 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"crypto/tls"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
)
type Proxy struct {
target *url.URL
proxy *httputil.ReverseProxy
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL)
r.Host = p.target.Host
w.Header().Set("X-Proxied-By", "go-reverse-proxy")
p.proxy.ServeHTTP(w, r)
}
func getEnv(key string, fallback string) string {
value := os.Getenv(key)
if value == "" {
value = fallback
}
return value
}
func logBanner(upstream string, port string) {
log.Printf("Starting reverse proxy on port %s\n", port)
log.Printf("Proxying requests to %s\n", upstream)
}
func main() {
// Replace 'target' with the URL of the server you want to proxy to
upstream := getEnv("UPSTREAM", "https://httpbin.org")
target, err := url.Parse(upstream)
if err != nil {
panic(err)
}
// Create a new ReverseProxy instance
proxy := httputil.NewSingleHostReverseProxy(target)
// Configure the reverse proxy to use HTTPS
proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// Create a new Proxy instance
p := &Proxy{target: target, proxy: proxy}
// Start the HTTP server and register the Proxy instance as the handler
port := getEnv("PORT", "8080")
logBanner(upstream, port)
err = http.ListenAndServe(":"+port, p)
if err != nil {
panic(err)
}
}