-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
67 lines (56 loc) · 1.31 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
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
const (
OK = "OK"
BAD = "BAD"
TIMEOUT = "TIMEOUT"
)
type State struct {
status string
}
func NewState() *State {
return &State{status: OK}
}
func (s *State) Health(rw http.ResponseWriter, r *http.Request) {
log.Printf("Received /health request: source=%v status=%v", r.RemoteAddr, s.status)
switch s.status {
case OK:
io.WriteString(rw, "I'm healthy")
case BAD:
http.Error(rw, "Internal Error", 500)
case TIMEOUT:
time.Sleep(30 * time.Second)
default:
io.WriteString(rw, "UNKNOWN")
}
}
func (s *State) Sabotage(rw http.ResponseWriter, r *http.Request) {
s.status = BAD
io.WriteString(rw, "Sabotage ON")
}
func (s *State) Recover(rw http.ResponseWriter, r *http.Request) {
s.status = OK
io.WriteString(rw, "Recovered.")
}
func (s *State) Timeout(rw http.ResponseWriter, r *http.Request) {
s.status = TIMEOUT
io.WriteString(rw, "Configured to timeout.")
}
func main() {
httpState := NewState()
mux := http.NewServeMux()
mux.HandleFunc("/health", httpState.Health)
mux.HandleFunc("/sabotage", httpState.Sabotage)
mux.HandleFunc("/recover", httpState.Recover)
mux.HandleFunc("/timeout", httpState.Timeout)
log.Print("Starting http server")
err := http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("PORT")), mux)
log.Fatal(err)
}