forked from golang-starters/golang-health-check
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
45 lines (38 loc) · 846 Bytes
/
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
package main
import (
"fmt"
"log"
"net/http"
)
var isOnline = true
func main() {
http.HandleFunc("/api/greeting", greeting)
http.HandleFunc("/api/stop", stop)
http.HandleFunc("/api/health", health)
http.Handle("/", http.FileServer(assetFS()))
fmt.Println("Web server running on port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func greeting(w http.ResponseWriter, r *http.Request) {
if isOnline {
message := "World"
if m := r.FormValue("name"); m != "" {
message = m
}
fmt.Fprintf(w, "Hello %s!", message)
return
}
w.WriteHeader(503)
w.Write([]byte("Not Online"))
}
func stop(w http.ResponseWriter, r *http.Request) {
isOnline = false
w.Write([]byte("Stopping HTTP Server"))
}
func health(w http.ResponseWriter, r *http.Request) {
if isOnline {
w.WriteHeader(200)
return
}
w.WriteHeader(500)
}