125 lines
3.8 KiB
Go
125 lines
3.8 KiB
Go
package config
|
|
|
|
import (
|
|
"io"
|
|
"io/ioutil"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/caarlos0/env/v6"
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
type Config struct {
|
|
HTTP HTTPConfig `yaml:"http"`
|
|
SMTP SMTPConfig `yaml:"smtp"`
|
|
Hydra HydraConfig `yaml:"hydra"`
|
|
}
|
|
|
|
// 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 {
|
|
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"`
|
|
TokenSigningKey string `yaml:"tokenSigningKey" env:"HTTP_TOKEN_SIGNING_KEY"`
|
|
TokenEncryptionKey string `yaml:"tokenEncryptionKey" env:"HTTP_TOKEN_ENCRYPTION_KEY"`
|
|
BasePublicURL string `yaml:"basePublicUrl" env:"HTTP_BASE_PUBLIC_URL"`
|
|
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"`
|
|
PublicAddress string `yaml:"publicAddress" env:"HTTP_PUBLIC_ADDRESS"`
|
|
PublicScheme string `yaml:"publicScheme" env:"HTTP_PUBLIC_SCHEME"`
|
|
}
|
|
|
|
type SMTPConfig struct {
|
|
Host string `yaml:"host" env:"SMTP_HOST"`
|
|
Port int `yaml:"port" env:"SMTP_PORT"`
|
|
UseStartTLS bool `yaml:"useStartTLS" env:"SMTP_USE_START_TLS"`
|
|
User string `yaml:"user" env:"SMTP_USER"`
|
|
Password string `yaml:"password" env:"SMTP_PASSWORD"`
|
|
InsecureSkipVerify bool `yaml:"insecureSkipVerify" env:"SMTP_INSECURE_SKIP_VERIFY"`
|
|
SenderAddress string `yaml:"senderAddress" env:"SMTP_SENDER_ADDRESS"`
|
|
SenderName string `yaml:"senderName" env:"SMTP_SENDER_NAME"`
|
|
}
|
|
|
|
type HydraConfig struct {
|
|
BaseURL string `yaml:"baseURL" env:"HYDRA_BASE_URL"`
|
|
// Fake upstream SSL termination adding the "X-Forwarded-Proto: https" to the OIDC client
|
|
// HTTP request headers.
|
|
// Required by ory/hydra in some networks topologies
|
|
FakeSSLTermination bool `yaml:"fakeSSLTermination" env:"HYDRA_FAKE_SSL_TERMINATION"`
|
|
HTTPClientTimeout time.Duration `yaml:"httpClientTimeout" env:"HYDRA_HTTP_CLIENT_TIMEOUT"`
|
|
}
|
|
|
|
func NewDumpDefault() *Config {
|
|
config := NewDefault()
|
|
return config
|
|
}
|
|
|
|
func NewDefault() *Config {
|
|
return &Config{
|
|
HTTP: HTTPConfig{
|
|
Address: ":3000",
|
|
CookieAuthenticationKey: "",
|
|
CookieEncryptionKey: "",
|
|
TokenEncryptionKey: "",
|
|
TokenSigningKey: "",
|
|
CookieMaxAge: int((time.Hour * 1).Seconds()), // 1 hour
|
|
TemplateDir: "template",
|
|
PublicDir: "public",
|
|
PublicAddress: "",
|
|
PublicScheme: "",
|
|
},
|
|
SMTP: SMTPConfig{
|
|
Host: "localhost",
|
|
Port: 2525,
|
|
User: "",
|
|
Password: "",
|
|
SenderAddress: "noreply@localhost",
|
|
SenderName: "noreply",
|
|
},
|
|
Hydra: HydraConfig{
|
|
BaseURL: "http://localhost:4445/",
|
|
FakeSSLTermination: false,
|
|
HTTPClientTimeout: time.Second * 30, //nolint: gomnb
|
|
},
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func WithEnvironment(conf *Config) error {
|
|
if err := env.Parse(conf); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|