2023-04-24 20:52:12 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2024-03-26 17:28:38 +01:00
|
|
|
"os"
|
2023-04-24 20:52:12 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config definition
|
|
|
|
type Config struct {
|
2024-03-26 17:28:38 +01:00
|
|
|
Admin AdminServerConfig `yaml:"admin"`
|
|
|
|
Proxy ProxyServerConfig `yaml:"proxy"`
|
|
|
|
Redis RedisConfig `yaml:"redis"`
|
|
|
|
Logger LoggerConfig `yaml:"logger"`
|
|
|
|
Layers LayersConfig `yaml:"layers"`
|
|
|
|
Bootstrap BootstrapConfig `yaml:"bootstrap"`
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFromFile retrieves the configuration from the given file
|
|
|
|
func NewFromFile(path string) (*Config, error) {
|
|
|
|
config := NewDefault()
|
|
|
|
|
2024-03-26 17:28:38 +01:00
|
|
|
data, err := os.ReadFile(path)
|
2023-04-24 20:52:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not read file '%s'", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(data, config); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not unmarshal configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDumpDefault dump the new default configuration
|
|
|
|
func NewDumpDefault() *Config {
|
|
|
|
config := NewDefault()
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefault return new default configuration
|
|
|
|
func NewDefault() *Config {
|
|
|
|
return &Config{
|
2024-03-26 17:28:38 +01:00
|
|
|
Admin: NewDefaultAdminServerConfig(),
|
|
|
|
Proxy: NewDefaultProxyServerConfig(),
|
|
|
|
Logger: NewDefaultLoggerConfig(),
|
|
|
|
Redis: NewDefaultRedisConfig(),
|
|
|
|
Layers: NewDefaultLayersConfig(),
|
|
|
|
Bootstrap: NewDefaultBootstrapConfig(),
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump the given configuration in the given writer
|
|
|
|
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 errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|