-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (58 loc) · 1.2 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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
const (
serviceURLEnvVar = "SERVICE_URL"
debugEnvVar = "DEBUG"
)
func main() {
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
fmt.Printf("received signal %v\n", sig)
done <- true
}()
url := os.Getenv(serviceURLEnvVar)
if url == "" {
fmt.Printf("empty %s, exiting\n", serviceURLEnvVar)
os.Exit(1)
}
debug := os.Getenv(debugEnvVar)
for {
select {
case <-done:
fmt.Println("received done signal, terminating process")
return
case <-time.After(3 * time.Second):
res, err := http.Get(url)
if err != nil {
fmt.Printf("error calling %s: %v\n", url, err)
continue
}
if res.StatusCode != http.StatusOK {
fmt.Printf("got non 200 response: %v\n", res.Status)
continue
} else {
fmt.Printf("got succesful response: %v\n", res.Status)
}
bs, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Printf("could not read response body: %v\n", err)
continue
}
res.Body.Close()
if debug != "" {
fmt.Printf("got response: %s\n", string(bs))
}
}
}
}