From a89cbe5a93aede3703cd5981ea71827b55db0866 Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 12 Feb 2024 18:16:16 +0800 Subject: initial version --- config/config.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 config/config.go (limited to 'config') diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..599f12c --- /dev/null +++ b/config/config.go @@ -0,0 +1,52 @@ +package config + +import ( + "errors" + "fmt" + "github.com/BurntSushi/toml" + "net/url" + "os" +) + +type Config struct { + Servers []struct { + Name string `toml:"name"` + Prefix string `toml:"prefix"` + Proxy string `toml:"proxy"` + } `toml:"servers"` + Debug bool `toml:"debug"` + Listen string `toml:"listen"` +} + +func (c Config) Validate() error { + if len(c.Servers) == 0 { + return errors.New("no yggdrasil server specified") + } + for _, s := range c.Servers { + if s.Prefix == "" { + return fmt.Errorf("missing prefix for yggdrasil server `%v`", s.Name) + } + _, err := url.Parse(s.Prefix) + if err != nil { + return fmt.Errorf("invalid prefix for yggdrasil server `%v`: %w", s.Name, err) + } + } + return nil +} + +func Read(path string) (*Config, error) { + f, err := os.Open(path) + if err != nil { + return nil, fmt.Errorf("open: %w", err) + } + defer func() { + _ = f.Close() + }() + dec := toml.NewDecoder(f) + var c Config + _, err = dec.Decode(&c) + if err != nil { + return nil, fmt.Errorf("decode: %w", err) + } + return &c, nil +} -- cgit v1.2.3