From b933083d20b3db4a3d6a8134efe312eb6ff3d8e2 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 9 Mar 2024 20:19:35 +0800 Subject: initial version --- config/client.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 config/client.go (limited to 'config/client.go') diff --git a/config/client.go b/config/client.go new file mode 100644 index 0000000..a2d1500 --- /dev/null +++ b/config/client.go @@ -0,0 +1,43 @@ +package config + +import ( + "fmt" + "github.com/pelletier/go-toml/v2" + "os" +) + +type Server struct { + Listen string `toml:"listen"` + HandshakeTimeout int `toml:"handshake_timeout"` + Token string `toml:"token"` + Targets []Target `toml:"targets"` +} + +type Client struct { + ObserverID string `toml:"observer_id"` + ReportServer string `toml:"report_server"` + CheckInterval int `toml:"check_interval"` + SendBuffer int `toml:"send_buffer"` + ReconnectInterval int `toml:"reconnect_interval"` + ReportConnectTimeout int `toml:"report_connect_timeout"` + Token string `toml:"token"` +} + +type Target struct { + Host string `toml:"host"` + Port int `toml:"port"` +} + +func Read[T any](path string) (ret *T, err error) { + f, err := os.Open(path) + if err != nil { + return nil, fmt.Errorf("open: %w", err) + } + defer func() { + _ = f.Close() + }() + dec := toml.NewDecoder(f) + ret = new(T) + err = dec.Decode(ret) + return ret, err +} -- cgit v1.2.3