-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
88 lines (76 loc) · 1.68 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package devp2p
import (
"io/ioutil"
"log"
"time"
)
// Config is the p2p server configuration
type Config struct {
Logger *log.Logger
Name string
BindAddress string
BindPort int
MaxPeers int
Bootnodes []string
DialTasks int
DialBusyInterval time.Duration
PeerStore PeerStore
Protocols []*Protocol
}
// DefaultConfig returns a default configuration
func DefaultConfig() *Config {
c := &Config{
Name: "minimal/go1.10.2",
Logger: log.New(ioutil.Discard, "", 0),
BindAddress: "127.0.0.1",
BindPort: 30304,
MaxPeers: 10,
Bootnodes: []string{},
DialTasks: defaultDialTasks,
DialBusyInterval: 1 * time.Minute,
PeerStore: &NoopPeerStore{},
Protocols: []*Protocol{},
}
return c
}
type ConfigOption func(*Config)
func WithName(name string) ConfigOption {
return func(c *Config) {
c.Name = name
}
}
func WithBindAddress(addr string) ConfigOption {
return func(c *Config) {
c.BindAddress = addr
}
}
func WithBindPort(port int) ConfigOption {
return func(c *Config) {
c.BindPort = port
}
}
func WithMaxPeers(maxPeers int) ConfigOption {
return func(c *Config) {
c.MaxPeers = maxPeers
}
}
func WithBootnodes(bootnodes []string) ConfigOption {
return func(c *Config) {
c.Bootnodes = bootnodes
}
}
func WithPeerStore(peerstore PeerStore) ConfigOption {
return func(c *Config) {
c.PeerStore = peerstore
}
}
func WithLogger(logger *log.Logger) ConfigOption {
return func(c *Config) {
c.Logger = logger
}
}
func WithProtocol(p *Protocol) ConfigOption {
return func(c *Config) {
c.Protocols = append(c.Protocols, p)
}
}