aboutsummaryrefslogtreecommitdiff
path: root/config
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--config/config.go56
-rw-r--r--config/config_test.go68
-rw-r--r--config/test_data/invalid_address.yaml3
-rw-r--r--config/test_data/invalid_address_unresolvable.yaml3
-rw-r--r--config/test_data/invalid_port.yaml3
-rw-r--r--config/test_data/invalid_token.yaml3
-rw-r--r--config/test_data/invalid_yaml1
-rw-r--r--config/test_data/minimal.yaml3
-rw-r--r--config/test_data/minimal_with_hostname.yaml3
9 files changed, 143 insertions, 0 deletions
diff --git a/config/config.go b/config/config.go
new file mode 100644
index 0000000..653aeb6
--- /dev/null
+++ b/config/config.go
@@ -0,0 +1,56 @@
+package config
+
+import (
+ "net"
+ "os"
+ "regexp"
+
+ "github.com/pkg/errors"
+ "gopkg.in/yaml.v3"
+)
+
+var validToken = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
+
+type Config struct {
+ // Address is the hostname or ip the web server will listen to
+ Address string `yaml:"address",default:"127.0.0.1"`
+ Port string `yaml:"port",default:"8080"`
+ // Token is the sncf api token
+ Token string `yaml:"token"`
+}
+
+func (c *Config) validate() error {
+ // address
+ if ip := net.ParseIP(c.Address); ip == nil {
+ if _, err := net.LookupIP(c.Address); err != nil {
+ return errors.New("Invalid address " + c.Address + ", it must be a valid ipv4 address, ipv6 address, or resolvable name.")
+ }
+ }
+ // port
+ if _, err := net.LookupPort("tcp", c.Port); err != nil {
+ return errors.New("Invalid port " + c.Port + ", it must be a valid port number or tcp service name. Got error : " + err.Error())
+ }
+ // token
+ if ok := validToken.MatchString(c.Token); !ok {
+ return errors.New("Invalid token, must be an hexadecimal string that lookslike 12345678-9abc-def0-1234-56789abcdef0, got " + c.Token + " instead.")
+ }
+ return nil
+}
+
+// LoadFile loads the c from a given file
+func LoadFile(path string) (*Config, error) {
+ var c *Config
+ f, errOpen := os.Open(path)
+ if errOpen != nil {
+ return nil, errors.Wrapf(errOpen, "Failed to open configuration file %s", path)
+ }
+ defer f.Close()
+ decoder := yaml.NewDecoder(f)
+ if err := decoder.Decode(&c); err != nil {
+ return nil, errors.Wrap(err, "Failed to decode configuration file")
+ }
+ if err := c.validate(); err != nil {
+ return nil, errors.Wrap(err, "Failed to validate configuration")
+ }
+ return c, nil
+}
diff --git a/config/config_test.go b/config/config_test.go
new file mode 100644
index 0000000..0b78260
--- /dev/null
+++ b/config/config_test.go
@@ -0,0 +1,68 @@
+package config
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestLoadFile(t *testing.T) {
+ // Non existant file
+ _, err := LoadFile("test_data/non-existant")
+ if err == nil {
+ t.Fatal("non-existant config file failed without error")
+ }
+
+ // Invalid yaml file
+ _, err = LoadFile("test_data/invalid_yaml")
+ if err == nil {
+ t.Fatal("invalid_yaml config file failed without error")
+ }
+
+ // Invalid address
+ if _, err = LoadFile("test_data/invalid_address.yaml"); err == nil {
+ t.Fatal("Invalid address should fail to load")
+ }
+
+ // Invalid address unreasolvable
+ if _, err = LoadFile("test_data/invalid_address_unresolvable.yaml"); err == nil {
+ t.Fatal("Unresolvable address should fail to load")
+ }
+
+ // Invalid port
+ if _, err = LoadFile("test_data/invalid_port.yaml"); err == nil {
+ t.Fatal("Invalid port should fail to load")
+ }
+
+ // Invalid token
+ if _, err = LoadFile("test_data/invalid_token.yaml"); err == nil {
+ t.Fatal("Invalid token should fail to load")
+ }
+
+ // Minimal yaml file
+ want := Config{
+ Address: "127.0.0.2",
+ Port: "8082",
+ Token: "12345678-9abc-def0-1234-56789abcdef0",
+ }
+ config, err := LoadFile("test_data/minimal.yaml")
+ if err != nil {
+ t.Fatalf("minimal example failed with error: %v", err)
+ }
+ if config != nil && !reflect.DeepEqual(want, *config) {
+ t.Fatalf("minimal example failed:\nwant:%+v\ngot: %+v", want, *config)
+ }
+
+ // Minimal yaml file with hostname resolving
+ want = Config{
+ Address: "localhost",
+ Port: "8082",
+ Token: "12345678-9abc-def0-1234-56789abcdef0",
+ }
+ config, err = LoadFile("test_data/minimal_with_hostname.yaml")
+ if err != nil {
+ t.Fatalf("minimal example failed with error: %v", err)
+ }
+ if config != nil && !reflect.DeepEqual(want, *config) {
+ t.Fatalf("minimal example failed:\nwant:%+v\ngot: %+v", want, *config)
+ }
+}
diff --git a/config/test_data/invalid_address.yaml b/config/test_data/invalid_address.yaml
new file mode 100644
index 0000000..e3682b2
--- /dev/null
+++ b/config/test_data/invalid_address.yaml
@@ -0,0 +1,3 @@
+address: "0.0.0.0.0"
+port: 8082
+token: 12345678-9abc-def0-1234-56789abcdef0
diff --git a/config/test_data/invalid_address_unresolvable.yaml b/config/test_data/invalid_address_unresolvable.yaml
new file mode 100644
index 0000000..d476093
--- /dev/null
+++ b/config/test_data/invalid_address_unresolvable.yaml
@@ -0,0 +1,3 @@
+address: "invalid"
+port: 8082
+token: 12345678-9abc-def0-1234-56789abcdef0
diff --git a/config/test_data/invalid_port.yaml b/config/test_data/invalid_port.yaml
new file mode 100644
index 0000000..2790b3f
--- /dev/null
+++ b/config/test_data/invalid_port.yaml
@@ -0,0 +1,3 @@
+address: localhost
+port: invalid
+token: 12345678-9abc-def0-1234-56789abcdef0
diff --git a/config/test_data/invalid_token.yaml b/config/test_data/invalid_token.yaml
new file mode 100644
index 0000000..6f9e297
--- /dev/null
+++ b/config/test_data/invalid_token.yaml
@@ -0,0 +1,3 @@
+address: 127.0.0.1
+port: 8080
+token: invalid
diff --git a/config/test_data/invalid_yaml b/config/test_data/invalid_yaml
new file mode 100644
index 0000000..db1ddad
--- /dev/null
+++ b/config/test_data/invalid_yaml
@@ -0,0 +1 @@
+blargh(ads)
diff --git a/config/test_data/minimal.yaml b/config/test_data/minimal.yaml
new file mode 100644
index 0000000..2944645
--- /dev/null
+++ b/config/test_data/minimal.yaml
@@ -0,0 +1,3 @@
+address: 127.0.0.2
+port: 8082
+token: 12345678-9abc-def0-1234-56789abcdef0
diff --git a/config/test_data/minimal_with_hostname.yaml b/config/test_data/minimal_with_hostname.yaml
new file mode 100644
index 0000000..716b2d5
--- /dev/null
+++ b/config/test_data/minimal_with_hostname.yaml
@@ -0,0 +1,3 @@
+address: localhost
+port: 8082
+token: 12345678-9abc-def0-1234-56789abcdef0