-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
91 lines (76 loc) · 1.75 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"embed"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/urfave/cli/v2"
)
//go:generate yarn
//go:generate yarn build
//go:embed dist/*
var embedFs embed.FS
type FileSystem struct {
efs http.FileSystem
}
func NewFS() *FileSystem {
return &FileSystem{http.FS(embedFs)}
}
func (fs FileSystem) Open(name string) (http.File, error) {
f, err := fs.efs.Open(filepath.Join("dist", name))
if os.IsNotExist(err) {
return fs.efs.Open("dist/index.html")
}
return f, err
}
func main() {
app := &cli.App{
Name: "seenvoy",
Usage: "see the configs of the Envoy",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "addr",
Value: ":8080",
Usage: "specify the seenvoy listen address",
},
&cli.StringFlag{
Name: "target",
Usage: "specify the admin URL of the envoy",
Aliases: []string{"t"},
Required: true,
},
},
Action: func(c *cli.Context) error {
reverseProxy, err := buildReverseProxy(c.String("target"))
if err != nil {
return err
}
http.NewServeMux()
http.Handle("/", http.FileServer(NewFS()))
http.Handle("/api/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = r.URL.Path[len("/api/"):]
reverseProxy.ServeHTTP(w, r)
}))
log.Println("seenvoy listening on", c.String("addr"))
return http.ListenAndServe(c.String("addr"), nil)
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func buildReverseProxy(s string) (*httputil.ReverseProxy, error) {
if !strings.Contains(s, "://") {
s = "http://" + s
}
target, err := url.Parse(s)
if err != nil {
return nil, err
}
log.Println("reverse proxy target:", target)
return httputil.NewSingleHostReverseProxy(target), nil
}