-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_proxy.go
48 lines (42 loc) · 996 Bytes
/
handler_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
36
37
38
39
40
41
42
43
44
45
46
47
48
package handlers
import (
"net/http"
"net/http/httputil"
"path"
)
func (s server) proxy(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.Redirect(w, r, s.consolePrefix, http.StatusFound)
return
}
token := s.proxyPathRegex.FindStringSubmatch(r.URL.Path)
if len(token) < 2 {
w.WriteHeader(http.StatusNotFound)
return
}
upstreamId, found, err := s.dao.GetUpstreamIdByToken(token[1])
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
w.WriteHeader(http.StatusNotFound)
return
}
upstream, ok, err := s.upstream.Upstream(upstreamId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
(&httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
r.SetURL(upstream.Url)
r.Out.URL.Path = path.Join(upstream.Url.Path, r.In.URL.Path[37:]) // trim token prefix
r.Out.Header.Del("Cookie")
},
}).ServeHTTP(w, r)
}