-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (102 loc) · 2.63 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/namsral/flag"
)
const defaultTick = 60 * time.Second
type config struct {
contentType string
server string
statusCode int
tick time.Duration
url string
userAgent string
}
func (c *config) init(args []string) error {
flags := flag.NewFlagSet(args[0], flag.ExitOnError)
flags.String(flag.DefaultConfigFlagname, "", "Path to config file")
var (
statusCode = flags.Int("status", 200, "Response HTTP status code")
tick = flags.Duration("tick", defaultTick, "Ticking interval")
server = flags.String("server", "", "Server HTTP header value")
contentType = flags.String("content_type", "", "Content-Type HTTP header value")
userAgent = flags.String("user_agent", "", "User-Agent HTTP header value")
url = flags.String("url", "", "Request URL")
)
if err := flags.Parse(args[1:]); err != nil {
return err
}
c.statusCode = *statusCode
c.tick = *tick
c.server = *server
c.contentType = *contentType
c.userAgent = *userAgent
c.url = *url
return nil
}
func cancel() {
log.Println("I am dying, please wait...")
time.Sleep(2 * time.Second)
log.Println("I am dead.")
}
func run(ctx context.Context, c *config, out io.Writer) error {
c.init(os.Args)
log.SetOutput(out)
log.Println("Starting...", c.tick, os.Getpid())
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
for {
select {
case s := <-signalChan:
switch s {
case syscall.SIGINT, syscall.SIGTERM:
log.Printf("Got SIGINT/SIGTERM, exiting.")
cancel()
os.Exit(1)
case syscall.SIGHUP:
log.Printf("Got SIGHUP, reloading.")
c.init(os.Args)
}
case <-ctx.Done():
return nil
case <-time.Tick(c.tick):
resp, err := http.Get(c.url)
if err != nil {
return err
}
log.Print(os.Getpid(), ": ")
if resp.StatusCode != c.statusCode {
log.Printf("Status code mismatch, got: %d\n", resp.StatusCode)
}
if s := resp.Header.Get("server"); s != c.server {
log.Printf("Server header mismatch, got: %s\n", s)
}
if ct := resp.Header.Get("content-type"); ct != c.contentType {
log.Printf("Content-Type header mismatch, got: %s\n", ct)
}
if ua := resp.Header.Get("user-agent"); ua != c.userAgent {
log.Printf("User-Agent header mismatch, got: %s\n", ua)
}
}
}
}
func main() {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
c := &config{}
defer func() {
cancel()
}()
if err := run(ctx, c, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}