diff options
Diffstat (limited to 'internal/config/config.go')
| -rw-r--r-- | internal/config/config.go | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index 2be9dd6..d212e1f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,10 +1,12 @@ package config import ( + "errors" "fmt" "net/url" "os" "path/filepath" + "reflect" "gopkg.in/ini.v1" ) @@ -13,6 +15,13 @@ type Serve struct { Addr string `ini:"addr"` } +type Database struct { + Name string `ini:"name"` + User string `ini:"user"` + Password string `ini:"password"` + Port string `ini:"port"` +} + type Instance struct { Name string `ini:"name"` ContactEmail string `ini:"contact-email"` @@ -21,9 +30,15 @@ type Instance struct { type Config struct { Serve Serve `ini:"serve"` + Database Database `ini:"database"` Instance Instance `ini:"instance"` } +var ( + ErrBadConfig = errors.New("no database configuration found") + ErrBadConfigUrl = errors.New("bad url in config") +) + var systemConfigPath = "/etc/openstore/openstore.conf" func DefaultPath() (string, error) { @@ -40,22 +55,11 @@ func Load(filename string) (*Config, error) { Loose: true, KeyValueDelimiters: "=", }, systemConfigPath, filename) - if err != nil { return nil, err } - config := &Config{ - Serve: Serve{ - Addr: ":3000", - }, - Instance: Instance{ - Name: "Openstore", - ContactEmail: "jane@doe.com", - BaseURL: "https://www.example.com", - }, - } - + config := new(Config) err = cfg.MapTo(config) if err != nil { return nil, err @@ -64,10 +68,25 @@ func Load(filename string) (*Config, error) { u, err := url.Parse(config.Instance.BaseURL) if err != nil || u.Host == "" || u.Scheme == "" { return nil, fmt.Errorf( - "config: base-url must be an absolute url, got %s", + "%w: base-url must be an absolute url, got %s", + ErrBadConfigUrl, config.Instance.BaseURL, ) } + v := reflect.ValueOf(config).Elem() + t := v.Type() + + for i := 0; i < v.NumField(); i++ { + key := t.Field(i).Name + value := v.Field(i).Interface() + if value == nil || value == "" { + return nil, fmt.Errorf( + "%w: config field %s empty", + ErrBadConfig, key, + ) + } + } + return config, nil } |
