initial work for better configuration

This commit is contained in:
stryan 2020-08-20 13:24:52 -04:00
parent b549ff0d12
commit 043fa955ec
2 changed files with 26 additions and 15 deletions

View File

@ -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
}

13
main.go
View File

@ -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)
}