forked from ContainerSolutions/k8shserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk8shserver.go
53 lines (48 loc) · 1.22 KB
/
k8shserver.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
package main
import (
"bytes"
"encoding/json"
"strings"
"fmt"
"log"
"net/http"
"os"
)
const zoneUrl string = "http://metadata.google.internal/computeMetadata/v1/instance/zone"
func getZone() string {
client := &http.Client{}
req, err := http.NewRequest("GET", zoneUrl, nil)
req.Header.Add("Metadata-Flavor", "Google")
response, err := client.Do(req)
if err != nil {
panic("Unable to retrieve zone information: " + err.Error())
} else {
defer response.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(response.Body)
zoneSplit := strings.Split(buf.String(), "/")
return zoneSplit[len(zoneSplit)-1]
}
}
func sayHostname(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
hostname, _ := os.Hostname()
dat := map[string]interface{}{
"hostname": hostname,
"zone": getZone(),
}
mapB, _ := json.Marshal(dat)
w.Header().Set("Content-Type","application/json")
fmt.Fprintf(w, string(mapB)) // send data to client side
}
func main() {
http.HandleFunc("/", sayHostname) // set router
err := http.ListenAndServe(":9090", nil) // set listen port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}