74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"io"
|
|
|
|
ini "gopkg.in/ini.v1"
|
|
)
|
|
|
|
// Config is the config
|
|
type Config struct {
|
|
HTTP HTTPConfig
|
|
LDAP LDAPConfig
|
|
}
|
|
|
|
// HTTPConfig is the http config
|
|
type HTTPConfig struct {
|
|
Address string
|
|
Secret string
|
|
}
|
|
|
|
// LDAPConfig is the ldap config
|
|
type LDAPConfig struct {
|
|
Base string
|
|
Host string
|
|
Port int
|
|
BindDN string
|
|
BindPassword string
|
|
UserFilter string
|
|
Attributes []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
|
|
}
|
|
|
|
// NewDefault set a default config
|
|
func NewDefault() *Config {
|
|
return &Config{
|
|
HTTP: HTTPConfig{
|
|
Address: ":3001",
|
|
},
|
|
LDAP: LDAPConfig{
|
|
Base: "dc=example,dc=com",
|
|
Host: "ldap.example.com",
|
|
Port: 389,
|
|
BindDN: "uid=readonlysuer,ou=People,dc=example,dc=com",
|
|
BindPassword: "readonlypassword",
|
|
UserFilter: "(uid=%s)",
|
|
Attributes: []string{"givenName", "sn", "mail", "uid"},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Dump return the config dump
|
|
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
|
|
}
|