-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
69 lines (60 loc) · 1.21 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
59
60
61
62
63
64
65
66
67
68
69
package sanji
import (
"net"
"net/http"
"time"
"github.com/bitleak/lmstfy/client"
)
const (
DefaultNamespace = "default"
DefaultPort = 7777
DefaultPollInterval = 5
maxReadTimeout = 600
)
type Config struct {
client *client.LmstfyClient
Namespace string
Token string
Host string
Port int
PollInterval int
}
func Configure(c *Config) *Config {
c.assignDefaultIfBlank()
c.validate()
c.initClient()
return c
}
func (c *Config) initClient() {
cli := &http.Client{
Transport: &http.Transport{
MaxIdleConns: 128,
MaxIdleConnsPerHost: 32,
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: time.Minute,
}).DialContext,
},
Timeout: maxReadTimeout * time.Second,
}
c.client = client.NewLmstfyWithClient(cli, c.Host, c.Port, c.Namespace, c.Token)
}
func (c *Config) validate() {
if c.Token == "" {
panic("missing LmstfyClient token config!")
}
if c.Host == "" {
panic("missing LmstfyClient host config!")
}
}
func (c *Config) assignDefaultIfBlank() {
if c.Namespace == "" {
c.Namespace = "default"
}
if c.Port == 0 {
c.Port = DefaultPort
}
if c.PollInterval == 0 {
c.PollInterval = DefaultPollInterval
}
}