-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.go
More file actions
77 lines (70 loc) · 1.61 KB
/
Copy pathagent.go
File metadata and controls
77 lines (70 loc) · 1.61 KB
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
package skadigo
import (
"fmt"
"net/http"
"net/url"
"time"
)
// Logger can be logrus, zap, etc...
type Logger interface {
Errorf(string, ...interface{})
}
// Options for agent
type Options struct {
// in debug mode, all request will be printed and not really sent.
// and the worker will receive no job.
Debug bool
// you can custom http client timeout milliseconds, default value is 3000ms(3s)
Timeout int
// can be many kind of logger, look up the def
Logger Logger
}
// Agent or client
type Agent struct {
base string
httpc *http.Client
interval time.Duration
log Logger
}
// New skadi agent instance, you can just use it for sending messages,
// or StartWorker to handle your command later.
// token: your agent token
// server: skadi agent api server, https://github.com/hack-fan/skadi
func New(token, server string, opts *Options) *Agent { // nolint
// check options
base, err := url.Parse(server)
if err != nil || base.Host == "" {
panic("invalid skadi server address")
}
var timeout = 3 * time.Second
var interval = time.Minute
var log Logger
var debug bool
if opts != nil {
if opts.Timeout > 0 {
timeout = time.Duration(opts.Timeout) * time.Millisecond
}
if opts.Logger != nil {
log = opts.Logger
}
if opts.Debug {
debug = true
}
}
if log == nil {
log = defaultLogger{}
}
return &Agent{
base: server,
httpc: &http.Client{
Transport: customRoundTripper(token, debug),
Timeout: timeout,
},
interval: interval,
log: log,
}
}
type defaultLogger struct{}
func (defaultLogger) Errorf(format string, args ...interface{}) {
fmt.Printf(format, args...)
}