-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
61 lines (48 loc) · 938 Bytes
/
utils.go
File metadata and controls
61 lines (48 loc) · 938 Bytes
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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
func initPing(server string, token string) {
ticker := time.NewTicker(tick)
netClient := &http.Client{
Timeout: timeout,
}
defer ticker.Stop()
for range ticker.C {
err := everyTick(server, token, netClient)
if err != nil {
log.Println(err)
return
}
}
}
func everyTick(server string, token string, netClient *http.Client) error {
req, err := http.NewRequest("GET", fmt.Sprintf(urlHub, server), nil)
if err != nil {
log.Println(err)
return nil
}
req.Header.Set(tokenKey, token)
resp, err := netClient.Do(req)
if err != nil {
log.Println(err)
return nil
}
bodyByte, e := ioutil.ReadAll(resp.Body)
if e != nil {
resp.Body.Close()
return e
}
body := string(bodyByte)
if body == done {
log.Println("REGISTER!")
} else {
log.Printf("wrong answer from server, body: %s", body)
}
resp.Body.Close()
return nil
}