2020-07-10 18:07:41 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"time"
|
|
|
|
|
2020-10-05 14:16:25 +02:00
|
|
|
"github.com/lithammer/dedent"
|
|
|
|
|
2020-07-10 18:07:41 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
|
|
|
|
"github.com/caarlos0/env/v6"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2020-07-13 09:20:14 +02:00
|
|
|
Debug bool `yaml:"debug" env:"DEBUG"`
|
|
|
|
Log LogConfig `yaml:"log"`
|
|
|
|
HTTP HTTPConfig `yaml:"http"`
|
|
|
|
OIDC OIDCConfig `yaml:"oidc"`
|
|
|
|
Database DatabaseConfig `yaml:"database"`
|
2020-08-13 10:29:52 +02:00
|
|
|
Auth AuthConfig `yaml:"auth"`
|
2020-10-05 14:16:25 +02:00
|
|
|
SMTP SMTPConfig `yaml:"smtp"`
|
|
|
|
Task TaskConfig `yaml:"task"`
|
2020-10-12 14:35:02 +02:00
|
|
|
Sentry SentryConfig `ymal:"sentry"`
|
2020-07-10 18:07:41 +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 {
|
2020-07-13 18:49:44 +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"`
|
2020-10-13 14:59:25 +02:00
|
|
|
CookieSecure bool `yaml:"cookieSecure" env:"HTTP_COOKIE_SECURE"`
|
2020-07-13 18:49:44 +02:00
|
|
|
TemplateDir string `yaml:"templateDir" env:"HTTP_TEMPLATE_DIR"`
|
|
|
|
PublicDir string `yaml:"publicDir" env:"HTTP_PUBLIC_DIR"`
|
|
|
|
FrontendURL string `yaml:"frontendURL" env:"HTTP_FRONTEND_URL"`
|
|
|
|
CORS CORSConfig `yaml:"cors"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CORSConfig struct {
|
|
|
|
AllowedOrigins []string `yaml:"allowedOrigins" env:"HTTP_CORS_ALLOWED_ORIGINS"`
|
|
|
|
AllowCredentials bool `yaml:"allowCredentials" env:"HTTP_CORS_ALLOW_CREDENTIALS"`
|
2020-07-10 18:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type OIDCConfig struct {
|
|
|
|
ClientID string `yaml:"clientId" env:"OIDC_CLIENT_ID"`
|
|
|
|
ClientSecret string `yaml:"clientSecret" env:"OIDC_CLIENT_SECRET"`
|
2020-07-16 22:29:33 +02:00
|
|
|
IssuerURL string `yaml:"issuerUrl" env:"OIDC_ISSUER_URL"`
|
2020-07-10 18:07:41 +02:00
|
|
|
RedirectURL string `yaml:"redirectUrl" env:"OIDC_REDIRECT_URL"`
|
|
|
|
PostLogoutRedirectURL string `yaml:"postLogoutRedirectURL" env:"OIDC_POST_LOGOUT_REDIRECT_URL"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type LogConfig struct {
|
|
|
|
Level logger.Level `yaml:"level" env:"LOG_LEVEL"`
|
|
|
|
Format logger.Format `yaml:"format" env:"LOG_FORMAT"`
|
|
|
|
}
|
|
|
|
|
2020-07-13 09:20:14 +02:00
|
|
|
type DatabaseConfig struct {
|
|
|
|
DSN string `yaml:"dsn" env:"DATABASE_DSN"`
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:29:52 +02:00
|
|
|
type AuthConfig struct {
|
|
|
|
Rules []string `yaml:"rules" env:"AUTH_RULES"`
|
|
|
|
}
|
|
|
|
|
2020-10-05 14:16:25 +02:00
|
|
|
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 TaskConfig struct {
|
|
|
|
Newsletter NewsletterTaskConfig `yaml:"newsletter"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type NewsletterTaskConfig struct {
|
|
|
|
CronSpec string `yaml:"cronSpec" env:"TASK_NEWSLETTER_CRON_SPEC"`
|
|
|
|
TimeRange time.Duration `yaml:"timeRange" env:"TASK_NEWSLETTER_TIME_RANGE"`
|
|
|
|
BaseURL string `yaml:"baseUrl" env:"TASK_NEWSLETTER_BASE_URL"`
|
|
|
|
ContentTemplate string `yaml:"contentTemplate" env:"TASK_NEWSLETTER_CONTENT_TEMPLATE"`
|
|
|
|
SubjectTemplate string `yaml:"subjectTemplate" env:"TASK_NEWSLETTER_SUBJECT_TEMPLATE"`
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:35:02 +02:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:07:41 +02:00
|
|
|
func NewDumpDefault() *Config {
|
|
|
|
config := NewDefault()
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefault() *Config {
|
|
|
|
return &Config{
|
2020-07-13 09:20:14 +02:00
|
|
|
Debug: false,
|
2020-07-10 18:07:41 +02:00
|
|
|
Log: LogConfig{
|
|
|
|
Level: logger.LevelInfo,
|
|
|
|
Format: logger.FormatHuman,
|
|
|
|
},
|
|
|
|
HTTP: HTTPConfig{
|
|
|
|
Address: ":8081",
|
|
|
|
CookieAuthenticationKey: "",
|
|
|
|
CookieEncryptionKey: "",
|
2020-09-04 10:10:32 +02:00
|
|
|
CookieMaxAge: int((time.Hour * 24).Seconds()), // 24 hours
|
2020-07-10 18:07:41 +02:00
|
|
|
TemplateDir: "template",
|
|
|
|
PublicDir: "public",
|
|
|
|
FrontendURL: "http://localhost:8080",
|
2020-07-13 18:49:44 +02:00
|
|
|
CORS: CORSConfig{
|
|
|
|
AllowedOrigins: []string{"http://localhost:8080"},
|
|
|
|
AllowCredentials: true,
|
|
|
|
},
|
2020-07-10 18:07:41 +02:00
|
|
|
},
|
|
|
|
OIDC: OIDCConfig{
|
|
|
|
IssuerURL: "http://localhost:4444/",
|
|
|
|
RedirectURL: "http://localhost:8081/oauth2/callback",
|
|
|
|
PostLogoutRedirectURL: "http://localhost:8081",
|
|
|
|
},
|
2020-07-13 09:20:14 +02:00
|
|
|
Database: DatabaseConfig{
|
|
|
|
DSN: "host=localhost database=daddy",
|
|
|
|
},
|
2020-08-13 10:29:52 +02:00
|
|
|
Auth: AuthConfig{
|
|
|
|
Rules: []string{
|
|
|
|
"user.Email endsWith 'cadoles.com'",
|
|
|
|
},
|
|
|
|
},
|
2020-10-05 14:16:25 +02:00
|
|
|
SMTP: SMTPConfig{
|
|
|
|
Host: "localhost",
|
|
|
|
Port: 2525,
|
|
|
|
User: "",
|
|
|
|
Password: "",
|
|
|
|
SenderAddress: "noreply@localhost",
|
|
|
|
SenderName: "noreply",
|
|
|
|
},
|
|
|
|
Task: TaskConfig{
|
|
|
|
Newsletter: NewsletterTaskConfig{
|
|
|
|
CronSpec: "0 9 * * 1",
|
|
|
|
TimeRange: 24 * 7 * time.Hour,
|
|
|
|
BaseURL: "http://localhost:8080",
|
|
|
|
ContentTemplate: dedent.Dedent(`
|
|
|
|
{{- $root := . -}}
|
2020-10-05 15:49:09 +02:00
|
|
|
Bonjour{{if .User.Name}} {{ .User.Name }}{{end}},
|
2020-10-05 14:16:25 +02:00
|
|
|
|
|
|
|
{{ if not .HasEvents -}}
|
|
|
|
Aucun évènement notoire ces derniers jours.
|
|
|
|
{{ else -}}
|
|
|
|
Voici les évènements de ces derniers jours:
|
|
|
|
{{- end}}
|
|
|
|
|
|
|
|
{{- with .ReadyToVote }}
|
|
|
|
|
|
|
|
Dossiers récemment prêts à voter
|
|
|
|
--------------------------------
|
|
|
|
|
|
|
|
{{range . -}}
|
|
|
|
- "{{ .Title }}" - {{ $root.BaseURL }}/decisions/{{ .ID }} - créé le {{ .CreatedAt.Format "02/01/2006" }}
|
|
|
|
{{ end }}
|
|
|
|
{{- end}}
|
|
|
|
|
2020-10-12 13:11:57 +02:00
|
|
|
{{- with .Voted }}
|
|
|
|
|
|
|
|
Récemment votés
|
|
|
|
---------------
|
|
|
|
|
|
|
|
{{range . -}}
|
|
|
|
- "{{ .Title }}" - {{ $root.BaseURL }}/decisions/{{ .ID }} - voté le {{ .VotedAt.Format "02/01/2006" }}
|
|
|
|
{{ end }}
|
|
|
|
{{- end}}
|
|
|
|
|
2020-10-05 14:16:25 +02:00
|
|
|
{{- with .NewDecisionSupportFiles }}
|
|
|
|
|
|
|
|
Nouveaux dossiers d'aide à la décision
|
|
|
|
--------------------------------------
|
|
|
|
|
|
|
|
{{range . -}}
|
|
|
|
- "{{ .Title }}" - {{ $root.BaseURL }}/decisions/{{ .ID }} - créé le {{ .CreatedAt.Format "02/01/2006" }}
|
|
|
|
{{ end }}
|
|
|
|
{{- end}}
|
|
|
|
|
|
|
|
{{- with .NewWorkgroups}}
|
|
|
|
|
|
|
|
Nouveaux groupes de travail
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
{{range . -}}
|
|
|
|
- "{{ .Name }}" - {{ $root.BaseURL }}/workgroups/{{ .ID }} - créé le {{ .CreatedAt.Format "02/01/2006" }}
|
|
|
|
{{ end }}
|
|
|
|
{{- end}}
|
|
|
|
|
|
|
|
Bonne semaine,
|
|
|
|
|
|
|
|
Daddy
|
|
|
|
`),
|
|
|
|
SubjectTemplate: `[Daddy] Évènements du {{ .From.Format "02/01/2006" }} au {{ .To.Format "02/01/2006" }}`,
|
|
|
|
},
|
|
|
|
},
|
2020-10-12 14:35:02 +02:00
|
|
|
Sentry: SentryConfig{
|
|
|
|
DSN: "",
|
|
|
|
ServerSampleRate: 1,
|
|
|
|
ServerFlushTimeout: 2 * time.Second,
|
|
|
|
Environment: "",
|
|
|
|
},
|
2020-07-10 18:07:41 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithEnvironment(conf *Config) error {
|
|
|
|
if err := env.Parse(conf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|