diff --git a/main.go b/main.go index b23527b..20a4667 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ func main() { views.SecretKey = os.Getenv("SECRET_KEY") // Ex: "s6JM1e8JTAphtKNR2y27XA8kkAaXOSYB" // 32 byte long if len(views.SecretKey) != 32 { - glog.Errorf("Invalid Secret Key") + glog.Errorf("Invalid Secret Key. Using randomly generated key. This will invalidate any active sessions.") var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") b := make([]rune, 32) diff --git a/models/config.go b/models/config.go index f0676eb..a2ee7bb 100644 --- a/models/config.go +++ b/models/config.go @@ -4,6 +4,8 @@ package models +import "database/sql" + const ( ForumName = "forum_name" @@ -14,10 +16,6 @@ const ( DefaultFromMail = "default_from_mail" ) -func CreateConfigValue(key string, val string) { - DB.Exec("INSERT INTO configs(name, val) VALUES($1, $2);", key, val) -} - func GetConfigValue(key string) string { var val string err := DB.Get(&val, "SELECT val FROM configs WHERE name=$1;", key) @@ -27,6 +25,12 @@ func GetConfigValue(key string) string { return val } -func UpdateConfigValue(key string, val string) { - DB.Exec("UPDATE configs SET val = $1 WHERE name=$2;", val, key) +func SetConfigValue(key string, val string) { + var dummy string + err := DB.Get(&dummy, "SELECT val FROM configs WHERE name=$1;", key) + if err == sql.ErrNoRows { + DB.Exec("INSERT INTO configs(name, val) VALUES($1, $2);", key, val) + } else { + DB.Exec("UPDATE configs SET val = $1 WHERE name=$2;", val, key) + } }