74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"io"
|
|
|
|
ini "gopkg.in/ini.v1"
|
|
)
|
|
|
|
type Config struct {
|
|
HTTP HTTPConfig
|
|
LDAP LDAPConfig
|
|
Contact ContactConfig
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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",
|
|
},
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|