Просмотр исходного кода

Provide a shortcut for proxying minikube

Lu Stadler лет назад: 7
Родитель
Сommit
e73b27d8fe
2 измененных файлов с 29 добавлено и 0 удалено
  1. 1 0
      go/fake-http/README.md
  2. 28 0
      go/fake-http/fake-http.go

+ 1 - 0
go/fake-http/README.md

6
`minikube start`.
6
`minikube start`.
7
7
8
```
8
```
9
# shortcut for the cmdline below: fake-http -proxy-minikube
9
$ fake-http -proxy-url=https://$(minikube ip):8443 -proxy-client-cert ~/.minikube/client.crt -proxy-client-key ~/.minikube/client.key
10
$ fake-http -proxy-url=https://$(minikube ip):8443 -proxy-client-cert ~/.minikube/client.crt -proxy-client-key ~/.minikube/client.key
10
2018/06/09 13:21:19 Listening on http://localhost:8080
11
2018/06/09 13:21:19 Listening on http://localhost:8080
11
2018/06/09 13:21:19 See http://localhost:8080/_help
12
2018/06/09 13:21:19 See http://localhost:8080/_help

+ 28 - 0
go/fake-http/fake-http.go

13
	"net/http"
13
	"net/http"
14
	"net/http/httputil"
14
	"net/http/httputil"
15
	"net/url"
15
	"net/url"
16
	"os"
17
	"os/exec"
18
	"path"
16
	"strings"
19
	"strings"
17
20
18
	"gopkg.in/yaml.v2"
21
	"gopkg.in/yaml.v2"
23
	proxyURL        string
26
	proxyURL        string
24
	proxyClientCert string
27
	proxyClientCert string
25
	proxyClientKey  string
28
	proxyClientKey  string
29
30
	proxyMinikube bool
26
}
31
}
27
32
28
func init() {
33
func init() {
30
	flag.StringVar(&flags.proxyURL, "proxy-url", "", "Proxy requests to this URL")
35
	flag.StringVar(&flags.proxyURL, "proxy-url", "", "Proxy requests to this URL")
31
	flag.StringVar(&flags.proxyClientCert, "proxy-client-cert", "", "Client certificate to use when connecting to proxy")
36
	flag.StringVar(&flags.proxyClientCert, "proxy-client-cert", "", "Client certificate to use when connecting to proxy")
32
	flag.StringVar(&flags.proxyClientKey, "proxy-client-key", "", "Client key to use when connecting to proxy")
37
	flag.StringVar(&flags.proxyClientKey, "proxy-client-key", "", "Client key to use when connecting to proxy")
38
39
	flag.BoolVar(&flags.proxyMinikube, "proxy-minikube", false, "Shortcut for -proxy-url=https://$(minikube ip):8443 -proxy-client-cert ~/.minikube/client.crt -proxy-client-key ~/.minikube/client.key")
33
}
40
}
34
41
35
var responses = []Response{}
42
var responses = []Response{}
37
func main() {
44
func main() {
38
	flag.Parse()
45
	flag.Parse()
39
46
47
	if flags.proxyMinikube {
48
		err := proxyMinikube()
49
		if err != nil {
50
			log.Fatalf("Error: Setting up Minikube proxy: %s", err)
51
		}
52
	}
53
40
	if flag.NArg() == 1 {
54
	if flag.NArg() == 1 {
41
		rs, err := loadResponses(flag.Arg(0))
55
		rs, err := loadResponses(flag.Arg(0))
42
		if err != nil {
56
		if err != nil {
165
	log.Fatal(http.ListenAndServe(flags.addr, nil))
179
	log.Fatal(http.ListenAndServe(flags.addr, nil))
166
}
180
}
167
181
182
func proxyMinikube() error {
183
	out, err := exec.Command("minikube", "ip").Output()
184
	if err != nil {
185
		return err
186
	}
187
	flags.proxyURL = fmt.Sprintf("https://%s:8443", strings.TrimSpace(string(out)))
188
189
	homeDir := os.Getenv("HOME")
190
	flags.proxyClientCert = path.Join(homeDir, ".minikube/client.crt")
191
	flags.proxyClientKey = path.Join(homeDir, ".minikube/client.key")
192
193
	return nil
194
}
195
168
func matchResponse(req *http.Request, responses []Response) *Response {
196
func matchResponse(req *http.Request, responses []Response) *Response {
169
	for _, resp := range responses {
197
	for _, resp := range responses {
170
		if req.Method == resp.Method && req.URL.Path == resp.Path {
198
		if req.Method == resp.Method && req.URL.Path == resp.Path {