canape/backend/config/canape.go

82 lines
1.8 KiB
Go

package config
import (
"io/ioutil"
"os"
polochonConfig "github.com/odwrtw/polochon/lib/configuration"
"github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
// Canape holds the canape specific config
type Canape struct {
Authorizer AuthorizerConfig `yaml:"authorizer"`
PGDSN string `yaml:"pgdsn"`
Port string `yaml:"listen_port"`
PublicDir string `yaml:"public_dir"`
ImgURLPrefix string `yaml:"img_url_prefix"`
PeriodicRefresh PeriodicRefreshConfig `yaml:"periodic_refresh"`
}
// Config represents the Config of the canape app
type Config struct {
polochonConfig.Config
Canape
}
// UnmarshalYAML implements the Unmarshaler interface
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
polochonParams := struct {
polochonConfig.Config `yaml:",inline"`
}{}
if err := unmarshal(&polochonParams); err != nil {
return err
}
c.Config = polochonParams.Config
canapeParams := struct {
Canape `yaml:",inline"`
}{}
if err := unmarshal(&canapeParams); err != nil {
return err
}
c.Canape = canapeParams.Canape
return nil
}
// AuthorizerConfig is the config for the authentication
type AuthorizerConfig struct {
Pepper string `yaml:"pepper"`
Cost int `yaml:"cost"`
Secret string `yaml:"secret"`
}
// PeriodicRefreshConfig is the config for the periodic refresh
type PeriodicRefreshConfig struct {
Enabled bool `yaml:"enabled"`
Interval string `yaml:"interval"`
}
// Load loads a config file from a path and return a Config object
func Load(path string, log *logrus.Entry) (*Config, error) {
// Open the file
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
conf := &Config{}
return conf, yaml.Unmarshal(b, conf)
}