-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_server_node.go
More file actions
executable file
·46 lines (38 loc) · 946 Bytes
/
cache_server_node.go
File metadata and controls
executable file
·46 lines (38 loc) · 946 Bytes
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
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
func boom() {
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
fmt.Println(req.Host)
// create the reverse proxy
url, err := url.Parse("http://" + req.Host)
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(url)
// Update the headers to allow for SSL redirection
req.URL.Host = url.Host
req.URL.Scheme = url.Scheme
req.Header.Set("X-Forwarded-Host", req.Header.Get("Host"))
req.Host = url.Host
// Note that ServeHttp is non blocking and uses a go routine under the hood
proxy.ServeHTTP(res, req)
})
http.ListenAndServe(":8080", nil)
}