-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
53 lines (44 loc) · 1.16 KB
/
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
49
50
51
52
53
package httputil
import (
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
func ReverseProxy(rawURL string) (http.HandlerFunc, error) {
remote, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
handler := func(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
r.Host = remote.Host
p.ServeHTTP(w, r)
}
}
proxy := httputil.NewSingleHostReverseProxy(remote)
return handler(proxy), nil
}
// DevProxy is for serving static files in development mode.
// mainly used for UI development with a separate server.
func DevProxy(mux *http.ServeMux, service http.Handler, isDev bool, proxyAddr string, exceptions []string) error {
if !isDev {
mux.Handle("/", service)
return nil
}
// staticFile := httputil.ServeFile(os.DirFS("./app/web-build"))
staticFile, err := ReverseProxy(proxyAddr)
if err != nil {
return err
}
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, e := range exceptions {
if strings.HasPrefix(r.URL.Path, e) {
service.ServeHTTP(w, r)
return
}
}
staticFile.ServeHTTP(w, r)
}))
return nil
}