Initial commit
This commit is contained in:
108
internal/config/config.go
Normal file
108
internal/config/config.go
Normal file
@ -0,0 +1,108 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
"github.com/caarlos0/env/v6"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
HTTP HTTPConfig `yaml:"http"`
|
||||
SMTP SMTPConfig `yaml:"smtp"`
|
||||
Data DataConfig `ymal:"data"`
|
||||
}
|
||||
|
||||
type HTTPConfig struct {
|
||||
Address string `yaml:"address" env:"FAKESMTP_HTTP_ADDRESS"`
|
||||
TemplateDir string `yaml:"templateDir" env:"FAKESMTP_HTTP_TEMPLATEDIR"`
|
||||
PublicDir string `yaml:"publicDir" env:"FAKESMTP_HTTP_PUBLICDIR"`
|
||||
}
|
||||
|
||||
type SMTPConfig struct {
|
||||
Address string `yaml:"address" env:"FAKESMTP_SMTP_ADDRESS"`
|
||||
Username string `yaml:"username" env:"FAKESMTP_SMTP_USERNAME"`
|
||||
Password string `yaml:"password" env:"FAKESMTP_SMTP_PASSWORD"`
|
||||
Domain string `yaml:"domain" env:"FAKESMTP_SMTP_DOMAIN"`
|
||||
ReadTimeout time.Duration `yaml:"readTimeout" env:"FAKESMTP_SMTP_READTIMEOUT"`
|
||||
WriteTimeout time.Duration `yaml:"writeTimeout" env:"FAKESMTP_SMTP_WRITETIMEOUT"`
|
||||
MaxMessageBytes int `yaml:"maxMessageBytes" env:"FAKESMTP_SMTP_MAXMESSAGEBYTES"`
|
||||
MaxRecipients int `yaml:"maxRecipients" env:"FAKESMTP_SMTP_MAXRECIPIENTS"`
|
||||
AllowInsecureAuth bool `yaml:"allowInsecureAuth" env:"FAKESMTP_SMTP_ALLOWINSECUREAUTH"`
|
||||
Debug bool `yaml:"debug" env:"FAKESMTP_SMTP_DEBUG"`
|
||||
}
|
||||
|
||||
type DataConfig struct {
|
||||
Path string `yaml:"path" env:"FAKESMTP_DATA_PATH"`
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func WithEnvironment(conf *Config) error {
|
||||
if err := env.Parse(conf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewDumpDefault() *Config {
|
||||
config := NewDefault()
|
||||
return config
|
||||
}
|
||||
|
||||
func NewDefault() *Config {
|
||||
return &Config{
|
||||
HTTP: HTTPConfig{
|
||||
Address: ":8080",
|
||||
TemplateDir: "template",
|
||||
PublicDir: "public",
|
||||
},
|
||||
SMTP: SMTPConfig{
|
||||
Address: ":2525",
|
||||
Username: "",
|
||||
Password: "",
|
||||
Domain: "localhost",
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
MaxMessageBytes: 1024 * 1024,
|
||||
MaxRecipients: 50,
|
||||
AllowInsecureAuth: true,
|
||||
Debug: true,
|
||||
},
|
||||
Data: DataConfig{
|
||||
Path: "fakesmtp.db",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
9
internal/config/provider.go
Normal file
9
internal/config/provider.go
Normal file
@ -0,0 +1,9 @@
|
||||
package config
|
||||
|
||||
import "gitlab.com/wpetit/goweb/service"
|
||||
|
||||
func ServiceProvider(config *Config) service.Provider {
|
||||
return func(ctn *service.Container) (interface{}, error) {
|
||||
return config, nil
|
||||
}
|
||||
}
|
33
internal/config/service.go
Normal file
33
internal/config/service.go
Normal file
@ -0,0 +1,33 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/service"
|
||||
)
|
||||
|
||||
const ServiceName service.Name = "config"
|
||||
|
||||
// From retrieves the config service in the given container
|
||||
func From(container *service.Container) (*Config, error) {
|
||||
service, err := container.Service(ServiceName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
srv, ok := service.(*Config)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
// Must retrieves the config service in the given container or panic otherwise
|
||||
func Must(container *service.Container) *Config {
|
||||
srv, err := From(container)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return srv
|
||||
}
|
Reference in New Issue
Block a user