Skip to content

Commit 8fafa17

Browse files
committed
Refactor on /health endpoint
1 parent c037366 commit 8fafa17

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

k8s/k8s.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@ import (
88

99
var (
1010
version = "1.0"
11-
bannedIp = "9"
12-
html = `<!DOCTYPE html><html><body><center>
13-
<img src="https://raw.githubusercontent.com/twogg-git/k8s-intro/master/kubernetes_katacoda.png">
14-
<h1 style="color:green">Playing with Kubernetes</h1>
15-
<h2 style="color:blue">Your server IP:` + getServerIP() + `</h2>
16-
<h3 style="color:blue">Version: twogghub/k8s-workshop:` + version + `</h3>
17-
<h3 style="color:red">Banned IP ending number: ` + bannedIp + `</h3>
18-
</center></body></html>`
11+
bannedIp = "0.0.0.0"
1912
)
2013

2114
func getServerIP() string {
@@ -28,21 +21,38 @@ func getServerIP() string {
2821
}
2922

3023
func playHome(w http.ResponseWriter, r *http.Request) {
24+
html := `<!DOCTYPE html><html><body><center>
25+
<img src="https://raw.githubusercontent.com/twogg-git/k8s-intro/master/kubernetes_katacoda.png">
26+
<h1 style="color:green">Playing with Kubernetes</h1>
27+
<h2 style="color:blue">Your server IP:` + getServerIP() + `</h2>
28+
<h3 style="color:blue">Version: twogghub/k8s-workshop:` + version + `</h3>
29+
<h3 style="color:red">Banned IP: ` + bannedIp + `</h3>
30+
</center></body></html>`
3131
fmt.Fprintf(w, html)
3232
}
3333

34-
func playWithIP(w http.ResponseWriter, r *http.Request) {
34+
func playHealth(w http.ResponseWriter, r *http.Request) {
3535
ip := getServerIP()
36-
if ip[len(ip)-1:] == bannedIp {
36+
if ip == bannedIp {
3737
w.WriteHeader(http.StatusInternalServerError)
3838
} else {
39-
fmt.Fprintf(w, "Play cool, I'm alive!!!")
39+
fmt.Fprintf(w, "I'm alive!.. but also dead for IP: "+bannedIp)
4040
}
4141
}
4242

43+
func playDead(w http.ResponseWriter, r *http.Request) {
44+
bannedIp = r.URL.Query().Get("ip")
45+
fmt.Fprintf(w, "Now playing dead for IP: "+bannedIp)
46+
}
47+
48+
/*func getBannedIP() string {
49+
return bannedIp
50+
}*/
51+
4352
func main() {
4453
http.HandleFunc("/", playHome)
45-
http.HandleFunc("/status", playWithIP)
54+
http.HandleFunc("/health", playHealth)
55+
http.HandleFunc("/kill", playDead)
4656
if err := http.ListenAndServe(":8080", nil); err != nil {
4757
panic(err)
4858
}

0 commit comments

Comments
 (0)