2020-05-20 10:43:12 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-05-22 16:03:59 +02:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
2020-05-20 10:43:12 +02:00
|
|
|
|
2020-05-20 19:23:01 +02:00
|
|
|
"github.com/caarlos0/env/v6"
|
2020-05-20 10:43:12 +02:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2020-05-22 16:03:59 +02:00
|
|
|
Log LogConfig `yaml:"log"`
|
2020-05-20 10:43:12 +02:00
|
|
|
HTTP HTTPConfig `yaml:"http"`
|
|
|
|
OIDC OIDCConfig `yaml:"oidc"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPConfig struct {
|
2020-05-20 19:23:01 +02:00
|
|
|
Address string `yaml:"address" env:"HTTP_ADDRESS"`
|
|
|
|
CookieAuthenticationKey string `yaml:"cookieAuthenticationKey" env:"HTTP_COOKIE_AUTHENTICATION_KEY"`
|
|
|
|
CookieEncryptionKey string `yaml:"cookieEncryptionKey" env:"HTTP_COOKIE_ENCRYPTION_KEY"`
|
|
|
|
CookieMaxAge int `yaml:"cookieMaxAge" env:"HTTP_COOKIE_MAX_AGE"`
|
|
|
|
TemplateDir string `yaml:"templateDir" env:"HTTP_TEMPLATE_DIR"`
|
|
|
|
PublicDir string `yaml:"publicDir" env:"HTTP_PUBLIC_DIR"`
|
2020-05-20 10:43:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type OIDCConfig struct {
|
2020-05-20 19:23:01 +02:00
|
|
|
ClientID string `yaml:"clientId" env:"OIDC_CLIENT_ID"`
|
|
|
|
ClientSecret string `yaml:"clientSecret" env:"OIDC_CLIENT_SECRET"`
|
|
|
|
IssuerURL string `ymal:"issuerUrl" env:"OIDC_ISSUER_URL"`
|
|
|
|
RedirectURL string `yaml:"redirectUrl" env:"OIDC_REDIRECT_URL"`
|
|
|
|
PostLogoutRedirectURL string `yaml:"postLogoutRedirectURL" env:"OIDC_POST_LOGOUT_REDIRECT_URL"`
|
2020-05-20 10:43:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 16:03:59 +02:00
|
|
|
type LogConfig struct {
|
|
|
|
Level logger.Level `yaml:"level" env:"LOG_LEVEL"`
|
|
|
|
Format logger.Format `yaml:"format" env:"LOG_FORMAT"`
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:43:12 +02:00
|
|
|
func NewDumpDefault() *Config {
|
|
|
|
config := NewDefault()
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefault() *Config {
|
|
|
|
return &Config{
|
2020-05-22 16:03:59 +02:00
|
|
|
Log: LogConfig{
|
2020-05-22 16:10:31 +02:00
|
|
|
Level: logger.LevelInfo,
|
2020-05-22 16:03:59 +02:00
|
|
|
Format: logger.FormatHuman,
|
|
|
|
},
|
2020-05-20 10:43:12 +02:00
|
|
|
HTTP: HTTPConfig{
|
|
|
|
Address: ":3002",
|
|
|
|
CookieAuthenticationKey: "",
|
|
|
|
CookieEncryptionKey: "",
|
|
|
|
CookieMaxAge: int((time.Hour * 1).Seconds()), // 1 hour
|
|
|
|
TemplateDir: "template",
|
|
|
|
PublicDir: "public",
|
|
|
|
},
|
|
|
|
OIDC: OIDCConfig{
|
2020-05-20 19:23:01 +02:00
|
|
|
IssuerURL: "http://localhost:4444/",
|
|
|
|
RedirectURL: "http://localhost:3002/oauth2/callback",
|
|
|
|
PostLogoutRedirectURL: "http://localhost:3002",
|
2020-05-20 10:43:12 +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
|
|
|
|
}
|
2020-05-20 19:23:01 +02:00
|
|
|
|
|
|
|
func WithEnvironment(conf *Config) error {
|
|
|
|
if err := env.Parse(conf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|