-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (66 loc) · 1.86 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
package main
import (
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
)
const version string = "1.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) {
fmt.Fprintf(w, "%s", "Hello World...")
})
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) {
tpl.Execute(w, "x")
})
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))
}