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" "github.com/spf13/viper"
) )
type Config struct { type LdapConfig struct {
Url string Url string
AdminUser string AdminUser string
UserAttr string UserAttr string
UserOu string UserOu string
LdapDc string LdapDc string
Secret string }
type Config struct {
Ldap *LdapConfig
Secret string
Tls bool
Port string
} }
func validateConfigEntry(entry string, name string) bool { func validateConfigEntry(entry string, name string) bool {
@ -33,18 +39,22 @@ func LoadConfig() (*Config, error) {
} }
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
c := &Config{} c := &Config{}
l := &LdapConfig{}
viper.SetDefault("port", "8080")
//Load configs //Load configs
c.Url = viper.GetString("ldapUrl") l.Url = viper.GetString("ldapUrl")
c.AdminUser = viper.GetString("adminUser") l.AdminUser = viper.GetString("adminUser")
c.UserAttr = viper.GetString("userAttr") l.UserAttr = viper.GetString("userAttr")
c.UserOu = viper.GetString("userOu") l.UserOu = viper.GetString("userOu")
c.LdapDc = viper.GetString("ldapDc") l.LdapDc = viper.GetString("ldapDc")
c.Secret = viper.GetString("secret") c.Secret = viper.GetString("secret")
c.Tls = viper.GetBool("tls")
c.Port = viper.GetString("port")
//Validate configs //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") log.Fatalf("FATAL: Error in config file, bailing")
} }
c.Ldap = l
return c, nil return c, nil
} }

13
main.go
View File

@ -5,7 +5,7 @@ import (
"net/http" "net/http"
) )
var LdapConfig *Config var Conf *Config
func signupPage(res http.ResponseWriter, req *http.Request) { func signupPage(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" { if req.Method != "POST" {
@ -18,12 +18,13 @@ func signupPage(res http.ResponseWriter, req *http.Request) {
email := req.FormValue("email") email := req.FormValue("email")
secret := req.FormValue("secret") 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")) res.Write([]byte("Get a load of this guy, not knowing the secret code"))
return return
} }
//insert into LDAP //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) err := createLDAPAccount(username, password, email)
if err == nil { if err == nil {
res.Write([]byte("User created!")) res.Write([]byte("User created!"))
@ -39,11 +40,11 @@ func homePage(res http.ResponseWriter, req *http.Request) {
} }
func main() { func main() {
LdapConfig, _ = LoadConfig() Conf, _ = LoadConfig()
log.Println("Loaded config") log.Println("Loaded config")
http.HandleFunc("/register", signupPage) http.HandleFunc("/register", signupPage)
http.HandleFunc("/", homePage) http.HandleFunc("/", homePage)
log.Println("Guildgate starting") log.Printf("Guildgate starting on %v\n", Conf.Port)
http.ListenAndServe(":8080", nil) http.ListenAndServe(":"+Conf.Port, nil)
} }