64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
|
package config
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
|
||
|
ini "gopkg.in/ini.v1"
|
||
|
)
|
||
|
|
||
|
type Config struct {
|
||
|
HTTP HTTPConfig
|
||
|
LDAP LDAPConfig
|
||
|
}
|
||
|
|
||
|
type HTTPConfig struct {
|
||
|
Address string
|
||
|
TemplateDir string
|
||
|
PublicDir string
|
||
|
}
|
||
|
|
||
|
type LDAPConfig struct {
|
||
|
URL string
|
||
|
BaseDN string
|
||
|
UserSearchFilterPattern 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))",
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|