forked from abenoseimei/gae-reverse-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgae-reverse-proxy.go
35 lines (31 loc) · 930 Bytes
/
gae-reverse-proxy.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
package main
import (
"log"
"strings"
"net/http"
"net/http/httputil"
"net/url"
"os"
)
func makeHandler(upstream string) func (w http.ResponseWriter, r *http.Request){
url, _ := url.Parse(upstream)
proxy := httputil.NewSingleHostReverseProxy(url)
return func (w http.ResponseWriter, r *http.Request) {
if prior, ok := r.Header["X-Forwarded-For"]; ok {
r.Header.Set("X-Real-IP", strings.Join(prior, ", "))
}
r.Header.Set("X-Proxy-Version", "gae-reverse-proxy/0.0.1")
r.URL.Host = url.Host
r.URL.Scheme = url.Scheme
r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))
proxy.ServeHTTP(w, r)
}
}
func main() {
port := os.Getenv("PORT")
upstream := os.Getenv("UPSTREAM")
http.HandleFunc("/", makeHandler(upstream))
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}