-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig.go
58 lines (54 loc) · 1.81 KB
/
config.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
package http
import (
"errors"
"github.com/elastic/beats/v7/libbeat/outputs/codec"
"net/http"
)
type config struct {
URL string `config:"url"`
Codec codec.Config `config:"codec"`
OnlyFields bool `config:"only_fields"`
MaxRetries int `config:"max_retries"`
Compression bool `config:"compression"`
KeepAlive bool `config:"keep_alive"`
MaxIdleConns int `config:"max_idle_conns"`
IdleConnTimeout int `config:"idle_conn_timeout"`
ResponseHeaderTimeout int `config:"response_header_timeout"`
Username string `config:"username"`
Password string `config:"password"`
AddFields map[string]interface{} `config:"add_fields"`
}
var (
defaultConfig = config{
URL: "http://127.0.0.1:8090/message",
OnlyFields: false,
MaxRetries: -1,
Compression: false,
KeepAlive: true,
MaxIdleConns: 1,
IdleConnTimeout: 0,
ResponseHeaderTimeout: 100,
Username: "",
Password: "",
AddFields: make(map[string]interface{}, 0),
}
)
func (c *config) Validate() error {
_, err := http.NewRequest("POST", c.URL, nil)
if err != nil {
return err
}
if c.MaxIdleConns < 1 {
return errors.New("max_idle_conns can't be <1")
}
if c.IdleConnTimeout < 0 {
return errors.New("idle_conn_timeout can't be <0")
}
if c.ResponseHeaderTimeout < 1 {
return errors.New("response_header_timeout can't be <1")
}
if c.Username != "" && c.Password == "" {
return errors.New("password can't be empty")
}
return nil
}