123 lines
3.4 KiB
Go
123 lines
3.4 KiB
Go
package config
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/caarlos0/env/v6"
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
type Config struct {
|
|
Debug bool `yaml:"debug"`
|
|
HTTP HTTPConfig `yaml:"http"`
|
|
Hydra HydraConfig `yaml:"hydra"`
|
|
Session SessionConfig `yaml:"session"`
|
|
Sentry SentryConfig `yaml:"sentry"`
|
|
WebAuthn WebAuthnConfig `yaml:"webauthn"`
|
|
API APIConfig `yaml:"api"`
|
|
Storage StorageConfig `yaml:"storage"`
|
|
}
|
|
|
|
// NewFromFile retrieves the configuration from the given file
|
|
func NewFromFile(filepath string) (*Config, error) {
|
|
config := NewDefault()
|
|
|
|
data, err := os.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 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"`
|
|
}
|
|
|
|
type SessionConfig struct {
|
|
DefaultDuration int `yaml:"defaultDuration" env:"HYDRA_SESSION_DEFAULT_DURATION"`
|
|
RememberMeDuration int `yaml:"rememberMeDuration" env:"HYDRA_SESSION_REMEMBER_ME_DURATION"`
|
|
}
|
|
|
|
type SentryConfig struct {
|
|
DSN string `yaml:"dsn" env:"SENTRY_DSN"`
|
|
// Server events sampling rate, see https://docs.sentry.io/platforms/go/configuration/options/
|
|
ServerSampleRate float64 `yaml:"serverSampleRate" env:"SENTRY_SERVER_SAMPLE_RATE"`
|
|
ServerFlushTimeout time.Duration `yaml:"serverFlushTimeout" env:"SENTRY_SERVER_FLUSH_TIMEOUT"`
|
|
Environment string `yaml:"environment" env:"SENTRY_ENVIRONMENT"`
|
|
}
|
|
|
|
func NewDumpDefault() *Config {
|
|
config := NewDefault()
|
|
return config
|
|
}
|
|
|
|
func NewDefault() *Config {
|
|
return &Config{
|
|
Debug: false,
|
|
HTTP: HTTPConfig{
|
|
Address: ":3000",
|
|
CookieAuthenticationKey: "",
|
|
CookieEncryptionKey: "",
|
|
TokenEncryptionKey: "",
|
|
TokenSigningKey: "",
|
|
CookieMaxAge: int((time.Hour * 1).Seconds()), // 1 hour
|
|
TemplateDir: "template",
|
|
PublicDir: "public",
|
|
BaseURL: "/",
|
|
},
|
|
Hydra: HydraConfig{
|
|
BaseURL: "http://localhost:4445/",
|
|
FakeSSLTermination: false,
|
|
HTTPClientTimeout: time.Second * 30, //nolint: gomnb
|
|
},
|
|
Session: SessionConfig{
|
|
DefaultDuration: int((time.Hour * 1).Seconds()), // 1 hour
|
|
RememberMeDuration: int((time.Hour * 24 * 30).Seconds()), // 30 days
|
|
},
|
|
Sentry: SentryConfig{
|
|
DSN: "",
|
|
ServerSampleRate: 1,
|
|
ServerFlushTimeout: 2 * time.Second,
|
|
Environment: "",
|
|
},
|
|
WebAuthn: NewDefaultWebAuthnConfig(),
|
|
API: NewDefaultAPIConfig(),
|
|
Storage: NewDefaultStorageConfig(),
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|