-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle.go
More file actions
42 lines (32 loc) · 885 Bytes
/
Copy pathtoggle.go
File metadata and controls
42 lines (32 loc) · 885 Bytes
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
package main
import (
"log"
"os"
"net/http"
"strconv"
)
var healthy bool = true
var port string = "8080"
func main() {
if len(os.Args) > 1 { port = os.Args[1] }
http.HandleFunc("/", rootHandler)
http.HandleFunc("/toggle", toggleHandler)
log.Printf("Bound to port " + port)
http.ListenAndServe(":" + port, nil)
}
func rootHandler(w http.ResponseWriter, r *http.Request){
if healthy {
w.WriteHeader(http.StatusOK)
w.Write([]byte("200 - OK"))
log.Printf("200 - OK")
} else {
w.WriteHeader(http.StatusGone)
w.Write([]byte("410 - Gone"))
log.Printf("410 - Gone")
}
}
func toggleHandler(w http.ResponseWriter, r *http.Request){
healthy = !healthy
w.Write([]byte("Setting health to " + strconv.FormatBool(healthy)))
log.Printf("Setting health to %v", healthy)
}