-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
66 lines (55 loc) · 1.46 KB
/
log.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
// +build ignore
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
poller "github.com/SunSince90/website-poller"
)
// This is a simple example that just logs the result of each request.
// No parameteres are defined, so, it wil poll every 30 seconds with no
// user agents.
func main() {
id := "poll-user"
page := &poller.Page{
ID: &id,
URL: "https://api.github.com/users/sunsince90",
Headers: map[string][]string{
"Accept": {"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"},
"Accept-Encoding": {"gzip, deflate, br"},
"Accept-Language": {"en-US,it-IT;q=0.8,it;q=0.5,en;q=0.3"},
"Cache-Control": {"no-cache"},
},
}
p, err := poller.New(page)
if err != nil {
fmt.Println("error occurred:", err)
os.Exit(1)
}
p.SetHandlerFunc(handleResponse)
ctx, canc := context.WithCancel(context.Background())
exitChan := make(chan struct{})
go func() {
p.Start(ctx, true)
close(exitChan)
}()
// -- Graceful shutdown
signalChan := make(chan os.Signal, 1)
signal.Notify(
signalChan,
syscall.SIGHUP, // kill -SIGHUP XXXX
syscall.SIGINT, // kill -SIGINT XXXX or Ctrl+c
syscall.SIGQUIT, // kill -SIGQUIT XXXX
)
<-signalChan
fmt.Println("exit requested")
canc()
<-exitChan // Wait for the poller goroutine to return
fmt.Println("goodbye!")
}
func handleResponse(id string, resp *http.Response, err error) {
fmt.Println("request with id", id, "returned status", resp.Status)
}