canape/backend/config/canape.go

85 lines
2.0 KiB
Go

package config
import (
"io/ioutil"
"os"
polochonConfig "github.com/odwrtw/polochon/lib/configuration"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// Config represents the Config of the canape app
type Config 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"`
PolochonConfig *polochonConfig.Config
}
// 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()
cf := &Config{}
polochonConfigFile := &polochonConfig.ConfigFileRoot{}
polochonConfig := &polochonConfig.Config{}
// Read it
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
// Unmarshal its content into the Config obj
err = yaml.Unmarshal(b, cf)
if err != nil {
return nil, err
}
// Unmarshal its content into the polochon.ConfigFileRoot obj
err = yaml.Unmarshal(b, polochonConfigFile)
if err != nil {
return nil, err
}
// Init all the show torrenters / detailers / searchers / explorers
err = polochonConfig.InitShow(polochonConfigFile)
if err != nil {
return nil, err
}
// Init all the movie torrenters / detailers / searchers / explorers
err = polochonConfig.InitMovie(polochonConfigFile)
if err != nil {
return nil, err
}
cf.PolochonConfig = polochonConfig
return cf, nil
}