-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (71 loc) · 1.78 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
80
81
82
83
84
85
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
)
var initialHealthStatus string
var messageOrginal string
var message string
var toggleTime int
var errorReadingToggleInterval error
func ticking() {
if toggleTime == -1 {
message = messageOrginal + ": toggleTime = -1"
return
}
ticker := time.NewTicker(1 * time.Second)
// defer ticker.Stop()
countdown := toggleTime
go func() {
for range ticker.C {
// fmt.Println("tick")
countdown--
if countdown == 0 {
message = messageOrginal + ": done"
} else {
message = messageOrginal + ": " + strconv.Itoa(countdown)
}
if countdown == 0 {
if initialHealthStatus == "bad" {
initialHealthStatus = "good"
} else if initialHealthStatus == "good" {
initialHealthStatus = "bad"
}
ticker.Stop()
break
}
}
}()
}
func healthStatusHandler(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "Health Status: %s", initialHealthStatus)
fmt.Fprint(w, initialHealthStatus)
}
func messageHandler(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "Health Status: %s", initialHealthStatus)
fmt.Fprint(w, message)
}
func main() {
initialHealthStatus = os.Getenv("INITIAL_HEALTH_STATUS")
if initialHealthStatus == "" {
initialHealthStatus = "not set"
}
toggleIntervalStr := os.Getenv("TOGGLE_INTERVAL")
toggleTime, errorReadingToggleInterval = strconv.Atoi(toggleIntervalStr)
if errorReadingToggleInterval != nil {
toggleTime = -1
}
messageOrginal = os.Getenv("MESSAGE")
ticking()
http.HandleFunc("/health", healthStatusHandler)
http.HandleFunc("/", messageHandler)
port := "8080"
fmt.Printf("Starting server on port %s...\n", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}