From 043fa955ecd74a810edcab8197d724a15eaa086c Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 20 Aug 2020 13:24:52 -0400 Subject: [PATCH] initial work for better configuration --- config.go | 28 +++++++++++++++++++--------- main.go | 13 +++++++------ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/config.go b/config.go index 1945289..e1d405c 100644 --- a/config.go +++ b/config.go @@ -6,13 +6,19 @@ import ( "github.com/spf13/viper" ) -type Config struct { +type LdapConfig struct { Url string AdminUser string UserAttr string UserOu string LdapDc string - Secret string +} + +type Config struct { + Ldap *LdapConfig + Secret string + Tls bool + Port string } func validateConfigEntry(entry string, name string) bool { @@ -33,18 +39,22 @@ func LoadConfig() (*Config, error) { } viper.SetConfigType("yaml") c := &Config{} + l := &LdapConfig{} + viper.SetDefault("port", "8080") //Load configs - c.Url = viper.GetString("ldapUrl") - c.AdminUser = viper.GetString("adminUser") - c.UserAttr = viper.GetString("userAttr") - c.UserOu = viper.GetString("userOu") - c.LdapDc = viper.GetString("ldapDc") + l.Url = viper.GetString("ldapUrl") + l.AdminUser = viper.GetString("adminUser") + l.UserAttr = viper.GetString("userAttr") + l.UserOu = viper.GetString("userOu") + l.LdapDc = viper.GetString("ldapDc") c.Secret = viper.GetString("secret") + c.Tls = viper.GetBool("tls") + c.Port = viper.GetString("port") //Validate configs - if validateConfigEntry(c.Url, "ldapUrl") || validateConfigEntry(c.AdminUser, "adminUser") || validateConfigEntry(c.UserOu, "userOu") || validateConfigEntry(c.LdapDc, "ldapDc") || validateConfigEntry(c.UserAttr, "userAttr") { + if validateConfigEntry(l.Url, "ldapUrl") || validateConfigEntry(l.AdminUser, "adminUser") || validateConfigEntry(l.UserOu, "userOu") || validateConfigEntry(l.LdapDc, "ldapDc") || validateConfigEntry(l.UserAttr, "userAttr") { log.Fatalf("FATAL: Error in config file, bailing") } - + c.Ldap = l return c, nil } diff --git a/main.go b/main.go index 4509b2c..e61f7e4 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,7 @@ import ( "net/http" ) -var LdapConfig *Config +var Conf *Config func signupPage(res http.ResponseWriter, req *http.Request) { if req.Method != "POST" { @@ -18,12 +18,13 @@ func signupPage(res http.ResponseWriter, req *http.Request) { email := req.FormValue("email") secret := req.FormValue("secret") - if LdapConfig.Secret != "" && LdapConfig.Secret != secret { + if Conf.Secret != "" && Conf.Secret != secret { + log.Printf("Bad secret entered\n") res.Write([]byte("Get a load of this guy, not knowing the secret code")) return } //insert into LDAP - log.Printf("Got %v %v %v %v\n", username, password, email, secret) + log.Printf("Attempting to create account for %v", username) err := createLDAPAccount(username, password, email) if err == nil { res.Write([]byte("User created!")) @@ -39,11 +40,11 @@ func homePage(res http.ResponseWriter, req *http.Request) { } func main() { - LdapConfig, _ = LoadConfig() + Conf, _ = LoadConfig() log.Println("Loaded config") http.HandleFunc("/register", signupPage) http.HandleFunc("/", homePage) - log.Println("Guildgate starting") - http.ListenAndServe(":8080", nil) + log.Printf("Guildgate starting on %v\n", Conf.Port) + http.ListenAndServe(":"+Conf.Port, nil) }