2020-04-27 22:43:42 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/caarlos0/env/v6"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2020-06-15 21:05:28 +02:00
|
|
|
// Config is the configuration struct
|
2020-04-27 22:43:42 +02:00
|
|
|
type Config struct {
|
2020-06-15 21:05:28 +02:00
|
|
|
HTTP HTTPConfig `yaml:"http"`
|
|
|
|
Client ClientConfig `yaml:"client"`
|
|
|
|
Data DataConfig `ymal:"data"`
|
2020-04-27 22:43:42 +02:00
|
|
|
}
|
|
|
|
|
2020-06-15 21:05:28 +02:00
|
|
|
// HTTPConfig is the configuration part which defines HTTP related params
|
2020-04-27 22:43:42 +02:00
|
|
|
type HTTPConfig struct {
|
2020-05-03 19:19:28 +02:00
|
|
|
Address string `yaml:"address" env:"GUESSTIMATE_HTTP_ADDRESS"`
|
|
|
|
PublicDir string `yaml:"publicDir" env:"GUESSTIMATE_PUBLIC_DIR"`
|
2020-04-27 22:43:42 +02:00
|
|
|
}
|
|
|
|
|
2020-06-15 21:05:28 +02:00
|
|
|
// ClientConfig is the configuration part which defines the client app related params
|
|
|
|
type ClientConfig struct {
|
2020-06-17 00:02:28 +02:00
|
|
|
PublicBaseURL string `yaml:"publicbaseurl" env:"GUESSTIMATE_CLIENT_PUBLIC_BASE_URL"`
|
2020-06-15 21:05:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DataConfig is the configuration part which defines data related params
|
2020-04-27 22:43:42 +02:00
|
|
|
type DataConfig struct {
|
|
|
|
Path string `yaml:"path" env:"GUESSTIMATE_DATA_PATH"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFromFile retrieves the configuration from the given file
|
|
|
|
func NewFromFile(filepath string) (*Config, error) {
|
|
|
|
config := NewDefault()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not read file '%s'", filepath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(data, config); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not unmarshal configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:05:28 +02:00
|
|
|
// WithEnvironment retrieves the configuration from env vars
|
2020-04-27 22:43:42 +02:00
|
|
|
func WithEnvironment(conf *Config) error {
|
|
|
|
if err := env.Parse(conf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:05:28 +02:00
|
|
|
// NewDumpDefault retrieves the default configuration
|
2020-04-27 22:43:42 +02:00
|
|
|
func NewDumpDefault() *Config {
|
|
|
|
config := NewDefault()
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:05:28 +02:00
|
|
|
// NewDefault creates and returns a new default configuration
|
2020-04-27 22:43:42 +02:00
|
|
|
func NewDefault() *Config {
|
|
|
|
return &Config{
|
|
|
|
HTTP: HTTPConfig{
|
2020-05-03 19:19:28 +02:00
|
|
|
Address: ":8081",
|
|
|
|
PublicDir: "public",
|
2020-04-27 22:43:42 +02:00
|
|
|
},
|
2020-06-15 21:05:28 +02:00
|
|
|
Client: ClientConfig{
|
2020-06-17 00:02:28 +02:00
|
|
|
PublicBaseURL: "http://localhost:8080",
|
2020-06-15 21:05:28 +02:00
|
|
|
},
|
2020-04-27 22:43:42 +02:00
|
|
|
Data: DataConfig{
|
|
|
|
Path: "guesstimate.db",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:05:28 +02:00
|
|
|
// Dump writes a given config to a config file
|
2020-04-27 22:43:42 +02:00
|
|
|
func Dump(config *Config, w io.Writer) error {
|
|
|
|
data, err := yaml.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not dump config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|