dispatch/config/config.go

120 lines
2.2 KiB
Go
Raw Normal View History

2018-12-11 09:51:20 +00:00
package config
import (
"time"
"github.com/fsnotify/fsnotify"
"github.com/khlieng/dispatch/storage"
2018-12-11 09:51:20 +00:00
"github.com/spf13/viper"
)
type Config struct {
Address string
Port string
Dev bool
2020-06-16 09:26:07 +00:00
Identd bool
2018-12-11 09:51:20 +00:00
HexIP bool
2020-06-15 23:22:23 +00:00
AutoCTCP bool `mapstructure:"auto_ctcp"`
2020-05-17 00:14:35 +00:00
VerifyCertificates bool `mapstructure:"verify_certificates"`
2018-12-20 10:51:31 +00:00
Headers map[string]string
Defaults Defaults
HTTPS HTTPS
LetsEncrypt LetsEncrypt
Auth Auth
2020-05-20 05:21:12 +00:00
DCC DCC
2020-06-16 01:04:27 +00:00
Proxy Proxy
2018-12-11 09:51:20 +00:00
}
type Defaults struct {
2020-05-23 06:05:37 +00:00
Name string
Host string
Port string
Channels []string
ServerPassword string `mapstructure:"server_password"`
SSL bool
ReadOnly bool
ShowDetails bool `mapstructure:"show_details"`
2018-12-11 09:51:20 +00:00
}
type HTTPS struct {
Enabled bool
Port string
Cert string
Key string
HSTS HSTS
2018-12-11 09:51:20 +00:00
}
type HSTS struct {
Enabled bool
MaxAge string `mapstructure:"max_age"`
IncludeSubdomains bool `mapstructure:"include_subdomains"`
Preload bool
}
type LetsEncrypt struct {
Domain string
Email string
}
type Auth struct {
Anonymous bool
Login bool
Registration bool
Providers map[string]Provider
}
type Provider struct {
Key string
Secret string
}
2020-05-20 05:21:12 +00:00
type DCC struct {
Enabled bool
Autoget Autoget
}
type Autoget struct {
Enabled bool
Delete bool
DeleteAfter time.Duration `mapstructure:"delete_after"`
}
2020-06-16 01:04:27 +00:00
type Proxy struct {
Enabled bool
Protocol string
Host string
Port string
Username string
Password string
}
2018-12-11 09:51:20 +00:00
func LoadConfig() (*Config, chan *Config) {
viper.SetConfigName("config")
2020-04-20 01:02:15 +00:00
viper.AddConfigPath(storage.Path.ConfigRoot())
2018-12-11 09:51:20 +00:00
viper.ReadInConfig()
config := &Config{}
viper.Unmarshal(config)
viper.WatchConfig()
configCh := make(chan *Config, 1)
prev := time.Now()
viper.OnConfigChange(func(e fsnotify.Event) {
now := time.Now()
// fsnotify sometimes fires twice
if now.Sub(prev) > time.Second {
config := &Config{}
err := viper.Unmarshal(config)
if err == nil {
configCh <- config
}
prev = now
}
})
return config, configCh
}