summaryrefslogtreecommitdiff
path: root/config
diff options
context:
space:
mode:
Diffstat (limited to 'config')
-rw-r--r--config/config.go52
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
+}