-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvivo.go
65 lines (57 loc) · 1.54 KB
/
vivo.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
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"io"
"log"
"net/http"
"path"
)
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
func notFound(w http.ResponseWriter) {
w.WriteHeader(404)
io.WriteString(w, "404 page not found")
}
func vivoForward(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
notFound(w)
return
}
client := &http.Client{}
//http: Request.RequestURI can't be set in client requests.
//http://golang.org/src/pkg/net/http/client.go
req.RequestURI = ""
req.Host = "127.0.0.1"
req.URL.Host = "127.0.0.1:2025"
req.URL.Scheme = "http"
req.URL.Path = "/" + path.Base(req.URL.Path)
resp, err := client.Do(req)
if err != nil {
http.Error(w, "Server Error", http.StatusInternalServerError)
log.Print("ServeHTTP:", err)
}
defer resp.Body.Close()
copyHeader(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}
func VivoListen(frontendStaticDir string, basePath string) {
basePath += "/"
fs := http.StripPrefix(basePath, http.FileServer(http.Dir(frontendStaticDir)))
http.Handle(basePath, fs)
if basePath != "/" {
// redirect "/" to base path
http.Handle("/", http.RedirectHandler(basePath, http.StatusSeeOther))
}
http.HandleFunc(basePath+"logs", vivoForward)
http.HandleFunc(basePath+"metrics", vivoForward)
http.HandleFunc(basePath+"traces", vivoForward)
log.Printf("Vivo proxy listening on base path %s", basePath)
if err := http.ListenAndServe("0.0.0.0:3000", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}