-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (87 loc) · 2.04 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
)
func main() {
name := os.Getenv("APP_NAME")
port := os.Getenv("APP_PORT")
bkeHost := os.Getenv("BKE_HOST")
bkePort := os.Getenv("BKE_PORT")
bkePath := os.Getenv("BKE_PATH")
// ハンドラー関数を定義する
proxy := func(w http.ResponseWriter, _ *http.Request) {
fmt.Println(fmt.Sprintf("app_name: %s", name))
cli, err := newHttpClient(fmt.Sprintf("http://%s:%s/%s", bkeHost, bkePort, bkePath), "GET")
if err != nil {
fmt.Printf("http client error: %v\n", err)
w.WriteHeader(500)
return
}
defer cli.Close()
cli.SendRequest()
apiRes, err := cli.RespToString()
if err != nil {
fmt.Printf("http client error: %v\n", err)
w.WriteHeader(500)
return
}
io.WriteString(w, fmt.Sprintf("route: %s -> %s", name, apiRes))
}
backend := func(w http.ResponseWriter, _ *http.Request) {
fmt.Println(fmt.Sprintf("app_name: %s", name))
io.WriteString(w, name)
}
// パスとハンドラー関数を結びつける
http.HandleFunc("/", proxy)
http.HandleFunc("/backend", backend)
// Web サーバーを起動する
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
type HttpClient struct {
req *http.Request
client *http.Client
method string
url string
resp *http.Response
}
func newHttpClient(url string, method string) (*HttpClient, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return &HttpClient{}, err
}
client := new(http.Client)
return &HttpClient{
req: req,
client: client,
method: method,
url: url,
}, nil
}
func (h *HttpClient) setHeader(header map[string]string) {
for k, v := range header {
h.req.Header.Set(k, v)
}
}
func (h *HttpClient) SendRequest() error {
r, err := h.client.Do(h.req)
if err != nil {
return err
}
h.resp = r
return nil
}
func (h *HttpClient) Close() {
h.resp.Body.Close()
}
func (h *HttpClient) RespToString() (string, error) {
b, err := ioutil.ReadAll(h.resp.Body)
if err != nil {
return "", err
}
return string(b), nil
}