aboutsummaryrefslogtreecommitdiff
path: root/config/config.go
diff options
context:
space:
mode:
authorJulien Dessaux2021-03-20 21:11:41 +0100
committerJulien Dessaux2021-03-20 21:11:41 +0100
commit775c81977e0e23aeab9bf48f9738353a4115a42d (patch)
tree1e1abc48e238f7fb1f09e5c8b9eca9e3170d7d51 /config/config.go
parentAdded api module with departures feature (diff)
downloadtrains-775c81977e0e23aeab9bf48f9738353a4115a42d.tar.gz
trains-775c81977e0e23aeab9bf48f9738353a4115a42d.tar.bz2
trains-775c81977e0e23aeab9bf48f9738353a4115a42d.zip
Added basic config module
Diffstat (limited to '')
-rw-r--r--config/config.go56
1 files changed, 56 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
+}