diff options
author | Keuin <[email protected]> | 2024-02-12 18:16:16 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2024-02-12 18:18:45 +0800 |
commit | a89cbe5a93aede3703cd5981ea71827b55db0866 (patch) | |
tree | 165089cb6c94e00c5f00d57da1a83e84c46722cb /config |
initial version
Diffstat (limited to 'config')
-rw-r--r-- | config/config.go | 52 |
1 files changed, 52 insertions, 0 deletions
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 +} |