/
client.go
54 lines (43 loc) · 1.12 KB
/
client.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
package client
import (
"time"
"github.com/BurntSushi/toml"
errors "golang.org/x/xerrors"
)
type Duration struct {
time.Duration
}
func (d *Duration) UnmarshalText(text []byte) (err error) {
d.Duration, err = time.ParseDuration(string(text))
return
}
const (
TypeWebsocket = "websocket"
TypeQuic = "quic"
)
type Config struct {
Type string `toml:"type"` // support websocket and quic
Host string `toml:"host"`
Path string `toml:"path"`
DebugCA string `toml:"debug_ca"`
ListenAddr string `toml:"listen_addr"`
Timeout Duration `toml:"timeout"`
Secret string `toml:"secret"`
Period uint `toml:"period"`
Pprof string `toml:"pprof"`
}
type tomlConfig struct {
Client Config `toml:"client"`
}
func New(path string) (Config, error) {
config := new(tomlConfig)
if _, err := toml.DecodeFile(path, config); err != nil {
return Config{}, errors.Errorf("new client config failed: %w", err)
}
switch config.Client.Type {
default:
return Config{}, errors.Errorf("unknown type %s", config.Client.Type)
case TypeWebsocket, TypeQuic:
}
return config.Client, nil
}