-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_proxy.go
More file actions
50 lines (42 loc) · 1.67 KB
/
Copy pathreverse_proxy.go
File metadata and controls
50 lines (42 loc) · 1.67 KB
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
package proxy
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
)
// SetupReverseProxy is a foundational technique showing how Go writes L7 proxies naturally.
// Standard lib `httputil.ReverseProxy` is robust enough to power many production routers.
func SetupReverseProxy(targetRawURL string, listenAddress string) {
// 1. Parse Upstream/Backend Destination URL
targetUrl, err := url.Parse(targetRawURL)
if err != nil {
log.Fatalf("Invalid Upstream URL: %v", err)
}
// 2. Wrap via HttpUtil ReverseProxy
proxy := httputil.NewSingleHostReverseProxy(targetUrl)
// Optional Customization: Director allows us to modify the Request IN FLIGHT!
// (Essential for appending API Keys, trace IDs, headers like X-Forwarded-For).
originalDirector := proxy.Director
proxy.Director = func(req *http.Request) {
// Retain default Director logic (Host, Scheme logic handling internally)
originalDirector(req)
// Let's add custom Trace headers
req.Header.Set("X-Proxy-Traffic-Trace", "apple-proxy-id: 112345")
// In a real load balancer, we would append the Client's REAL IP to the
// existing X-Forwarded-For slice or initialize if empty.
clientIP := req.RemoteAddr
if xff := req.Header.Get("X-Forwarded-For"); xff != "" {
req.Header.Set("X-Forwarded-For", xff+", "+clientIP)
} else {
req.Header.Set("X-Forwarded-For", clientIP)
}
log.Printf("[Proxy Routing] Rewrote request heading to -> %s%s\n", targetUrl.Host, req.URL.Path)
}
// Start HTTP Server
log.Printf("Starting Custom Reverse Proxy on %s routing to -> %s\n", listenAddress, targetRawURL)
err = http.ListenAndServe(listenAddress, proxy)
if err != nil {
log.Fatalf("Proxy server failed: %v", err)
}
}