hydra-passwordless/internal/config/config.go

109 lines
2.7 KiB
Go
Raw Normal View History

2020-04-08 08:56:42 +02:00
package config
import (
"io"
"io/ioutil"
"time"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type Config struct {
2020-05-20 11:13:14 +02:00
HTTP HTTPConfig `yaml:"http"`
SMTP SMTPConfig `yaml:"smtp"`
Hydra HydraConfig `yaml:"hydra"`
2020-04-08 08:56:42 +02:00
}
// 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"`
CookieAuthenticationKey string `yaml:"cookieAuthenticationKey"`
CookieEncryptionKey string `yaml:"cookieEncryptionKey"`
2020-05-20 11:13:14 +02:00
TokenSigningKey string `yaml:"tokenSigningKey"`
TokenEncryptionKey string `yaml:"tokenEncryptionKey"`
BasePublicURL string `yaml:"basePublicUrl"`
2020-04-08 08:56:42 +02:00
CookieMaxAge int `yaml:"cookieMaxAge"`
TemplateDir string `yaml:"templateDir"`
PublicDir string `yaml:"publicDir"`
2020-05-20 11:13:14 +02:00
PublicAddress string `yaml:"publicAddress"`
PublicScheme string `yaml:"publicScheme"`
2020-04-08 08:56:42 +02:00
}
type SMTPConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
UseStartTLS bool `yaml:"useStartTLS"`
User string `yaml:"user"`
Password string `yaml:"password"`
InsecureSkipVerify bool `yaml:"insecureSkipVerify"`
2020-04-24 09:27:07 +02:00
SenderAddress string `yaml:"senderAddress"`
SenderName string `yaml:"senderName"`
2020-04-08 08:56:42 +02:00
}
type HydraConfig struct {
BaseURL string `yaml:"baseURL"`
}
func NewDumpDefault() *Config {
config := NewDefault()
return config
}
func NewDefault() *Config {
return &Config{
HTTP: HTTPConfig{
Address: ":3000",
CookieAuthenticationKey: "",
CookieEncryptionKey: "",
2020-05-20 11:13:14 +02:00
TokenEncryptionKey: "",
TokenSigningKey: "",
2020-04-08 08:56:42 +02:00
CookieMaxAge: int((time.Hour * 1).Seconds()), // 1 hour
TemplateDir: "template",
PublicDir: "public",
2020-05-20 11:13:14 +02:00
PublicAddress: "",
PublicScheme: "",
2020-04-08 08:56:42 +02:00
},
2020-04-24 09:27:07 +02:00
SMTP: SMTPConfig{
Host: "localhost",
Port: 2525,
User: "hydra-passwordless",
Password: "hydra-passwordless",
SenderAddress: "noreply@localhost",
SenderName: "noreply",
},
2020-04-08 08:56:42 +02:00
Hydra: HydraConfig{
2020-04-24 09:27:07 +02:00
BaseURL: "http://localhost:4445/",
2020-04-08 08:56:42 +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
}