cadoles-profile/cmd/server/config/config.go

74 lines
1.4 KiB
Go
Raw Normal View History

2019-05-13 09:19:33 +02:00
package config
import (
"io"
ini "gopkg.in/ini.v1"
)
type Config struct {
HTTP HTTPConfig
LDAP LDAPConfig
Contact ContactConfig
2019-05-13 09:19:33 +02:00
}
type HTTPConfig struct {
Address string
TemplateDir string
PublicDir string
}
type LDAPConfig struct {
URL string
BaseDN string
UserSearchFilterPattern string
}
type ContactConfig struct {
Email string
Subject string
}
2019-05-13 09:19:33 +02:00
// NewFromFile retrieves the configuration from the given file
func NewFromFile(filepath string) (*Config, error) {
config := NewDefault()
cfg, err := ini.Load(filepath)
if err != nil {
return nil, err
}
if err := cfg.MapTo(config); err != nil {
return nil, err
}
return config, nil
}
func NewDefault() *Config {
return &Config{
HTTP: HTTPConfig{
Address: ":3000",
TemplateDir: "./templates",
PublicDir: "./public",
},
LDAP: LDAPConfig{
URL: "ldap://127.0.0.1:389",
BaseDN: "o=org,c=fr",
UserSearchFilterPattern: "(&(objectClass=person)(uid=%s))",
},
Contact: ContactConfig{
Email: "contact@cadoles-profile.local",
Subject: "[Cadoles Profile] Help needed",
},
2019-05-13 09:19:33 +02:00
}
}
func Dump(config *Config, w io.Writer) error {
cfg := ini.Empty()
if err := cfg.ReflectFrom(config); err != nil {
return err
}
if _, err := cfg.WriteTo(w); err != nil {
return err
}
return nil
}