aboutsummaryrefslogtreecommitdiff
path: root/cmd/main/http.go
blob: ce314955b28be67f08092075b6586d622ffd2f6c (plain) (blame)
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
package main

import (
	"bytes"
	"fmt"

	"riedstra.dev/go/checkup"
)

type httpCheck struct {
	URL  string
	Code int
}

func httpStatusWorker(jobs <-chan httpCheck, msg chan<- *string) {
	for hc := range jobs {
		err := checkup.HttpStatusOK(hc.URL, hc.Code)
		if err != nil {
			s := fmt.Sprintf("Checking: %s, %v\n",
				hc.URL, err)
			msg <- &s
			continue
		}
		msg <- nil
	}
}

func checkStatus(conf *Config) {
	buf := &bytes.Buffer{}

	hc := make(chan httpCheck)
	msgs := make(chan *string)

	for i := 1; i <= conf.Workers; i++ {
		go httpStatusWorker(hc, msgs)
	}

	go func() {
		for url, code := range conf.StatusChecks {
			if code == nil {
				code = &conf.ExpectedStatusCode
			}
			hc <- httpCheck{url, *code}
		}
		close(hc)
	}()

	for i := 0; i < len(conf.StatusChecks); i++ {
		m := <-msgs
		if m != nil {
			buf.Write([]byte(*m))
		}
	}

	notify(conf, buf.Bytes())
}