guildgate/config.go

92 lines
2.2 KiB
Go
Raw Permalink Normal View History

2020-08-20 12:51:02 -04:00
package main
import (
"log"
2020-09-24 16:14:54 -04:00
"sync"
2020-08-20 12:51:02 -04:00
"github.com/spf13/viper"
)
2020-08-20 13:24:52 -04:00
type LdapConfig struct {
2020-11-10 17:58:24 -05:00
Url string
AdminUser string
UserAttr string
UserOu string
MineUserOu string
LdapDc string
LdapPass string
2020-08-20 13:24:52 -04:00
}
2020-09-22 18:21:01 -04:00
type MailConfig struct {
Username string
Password string
SmtpServer string
SmtpPort int
}
2020-08-20 13:24:52 -04:00
type Config struct {
2020-11-12 14:51:48 -05:00
Ldap *LdapConfig
Mail *MailConfig
Secret string
TplPath string
UserTplPath string
Tls bool
Key string
Cert string
Port string
MaxID int
lock sync.Mutex
2020-08-20 12:51:02 -04:00
}
func validateConfigEntry(entry string, name string) bool {
if entry == "" {
log.Printf("Error: %v unset", name)
2020-08-20 13:06:44 -04:00
return true
2020-08-20 12:51:02 -04:00
}
2020-08-20 13:06:44 -04:00
return false
2020-08-20 12:51:02 -04:00
}
func LoadConfig() (*Config, error) {
2020-08-20 13:03:43 -04:00
viper.SetConfigName("guildgate")
2020-10-05 12:28:54 -04:00
viper.AddConfigPath("/etc/guildgate")
2020-08-20 12:51:02 -04:00
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("FATAL: Fatal error reading config file: %v \n", err)
}
viper.SetConfigType("yaml")
c := &Config{}
2020-08-20 13:24:52 -04:00
l := &LdapConfig{}
2020-09-22 18:21:01 -04:00
m := &MailConfig{}
2020-08-20 13:24:52 -04:00
viper.SetDefault("port", "8080")
2020-08-23 17:53:48 -04:00
viper.SetDefault("secret", "")
viper.SetDefault("Tls", false)
2020-08-20 12:51:02 -04:00
//Load configs
2020-08-20 13:24:52 -04:00
l.Url = viper.GetString("ldapUrl")
l.AdminUser = viper.GetString("adminUser")
l.UserAttr = viper.GetString("userAttr")
l.UserOu = viper.GetString("userOu")
2020-11-10 17:58:24 -05:00
l.MineUserOu = viper.GetString("mineUserOu")
2020-08-20 13:24:52 -04:00
l.LdapDc = viper.GetString("ldapDc")
l.LdapPass = viper.GetString("ldapPass")
2020-08-20 12:51:02 -04:00
c.Secret = viper.GetString("secret")
2020-08-20 13:24:52 -04:00
c.Tls = viper.GetBool("tls")
c.Port = viper.GetString("port")
2020-08-23 17:53:48 -04:00
c.Key = viper.GetString("tls_key")
c.Cert = viper.GetString("tls_cert")
c.TplPath = viper.GetString("templates_path")
2020-11-12 14:51:48 -05:00
c.UserTplPath = viper.GetString("user_templates_path")
2020-09-22 18:21:01 -04:00
m.SmtpServer = viper.GetString("SmtpServer")
m.Username = viper.GetString("SmtpUsername")
m.Password = viper.GetString("SmtpPassword")
m.SmtpPort = viper.GetInt("SmtpPort")
2020-08-20 12:51:02 -04:00
//Validate configs
2020-08-20 13:24:52 -04:00
if validateConfigEntry(l.Url, "ldapUrl") || validateConfigEntry(l.AdminUser, "adminUser") || validateConfigEntry(l.UserOu, "userOu") || validateConfigEntry(l.LdapDc, "ldapDc") || validateConfigEntry(l.UserAttr, "userAttr") {
2020-08-20 12:51:02 -04:00
log.Fatalf("FATAL: Error in config file, bailing")
}
2020-08-20 13:24:52 -04:00
c.Ldap = l
2020-09-22 18:21:01 -04:00
c.Mail = m
2020-08-20 12:51:02 -04:00
return c, nil
}