forked from evandbrown/gceme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (137 loc) · 3.46 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"google.golang.org/cloud/compute/metadata"
)
type Instance struct {
Id string
Name string
Hostname string
Zone string
Project string
InternalIP string
ExternalIP string
LBRequest string
ClientIP string
Error string
}
const version string = "4.0.0"
func main() {
showversion := flag.Bool("version", false, "display version")
frontend := flag.Bool("frontend", false, "run in frontend mode")
port := flag.Int("port", 8080, "port to bind")
backend := flag.String("backend-service", "http://127.0.0.1:8081", "hostname of backend server")
flag.Parse()
if *showversion {
fmt.Printf("Version %s\n", version)
return
}
http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s\n", version)
})
if *frontend {
frontendMode(*port, *backend)
} else {
backendMode(*port)
}
}
func backendMode(port int) {
log.Println("Operating in backend mode...")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
i := newInstance()
raw, _ := httputil.DumpRequest(r, true)
i.LBRequest = string(raw)
resp, _ := json.Marshal(i)
fmt.Fprintf(w, "%s", resp)
})
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
}
func frontendMode(port int, backendURL string) {
log.Println("Operating in frontend mode...")
tpl := template.Must(template.New("out").Parse(html))
transport := http.Transport{DisableKeepAlives: false}
client := &http.Client{Transport: &transport}
req, _ := http.NewRequest(
"GET",
backendURL,
nil,
)
req.Close = false
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
i := &Instance{}
resp, err := client.Do(req)
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "Error: %s\n", err.Error())
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error: %s\n", err.Error())
return
}
err = json.Unmarshal([]byte(body), i)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error: %s\n", err.Error())
return
}
tpl.Execute(w, i)
})
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
resp, err := client.Do(req)
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "Backend could not be connected to: %s", err.Error())
return
}
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
w.WriteHeader(http.StatusOK)
})
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
}
type assigner struct {
err error
}
func (a *assigner) assign(getVal func() (string, error)) string {
if a.err != nil {
return ""
}
s, err := getVal()
if err != nil {
a.err = err
}
return s
}
func newInstance() *Instance {
var i = new(Instance)
if !metadata.OnGCE() {
i.Error = "Not running on GCE"
return i
}
a := &assigner{}
i.Id = a.assign(metadata.InstanceID)
i.Zone = a.assign(metadata.Zone)
i.Name = a.assign(metadata.InstanceName)
i.Hostname = a.assign(metadata.Hostname)
i.Project = a.assign(metadata.ProjectID)
i.InternalIP = a.assign(metadata.InternalIP)
i.ExternalIP = a.assign(metadata.ExternalIP)
if a.err != nil {
i.Error = a.err.Error()
}
return i
}