diff --git a/src/main.go b/src/main.go index e79b55f..8dd847b 100644 --- a/src/main.go +++ b/src/main.go @@ -1,6 +1,7 @@ package main import ( + "net/http" "database/sql" "encoding/json" "github.com/bwmarrin/discordgo" diff --git a/src/register.go b/src/register.go index 66803dd..7b8481a 100644 --- a/src/register.go +++ b/src/register.go @@ -6,8 +6,6 @@ import ( "strings" "regexp" "github.com/dlclark/regexp2" - "context" - "time" "code.gitea.io/sdk/gitea" "crypto/rand" "github.com/cornelk/hashmap" @@ -103,14 +101,7 @@ func register(w http.ResponseWriter, r *http.Request) { log(err) hash := hashFunc([]byte(account.password), salt) // add user to the database - query := "INSERT INTO account(username, email, hash, salt, discordUserId) VALUES (?, ?, ?, ?, ?)" - ctx, cancelfunc := context.WithTimeout(context.Background(), 5*time.Second) - defer cancelfunc() - stmt, err := db.PrepareContext(ctx, query) - log(err) - defer stmt.Close() - _, err = stmt.ExecContext(ctx, account.username, account.email, hash, salt, account.discordId) - log(err) + go databaseInsert("INSERT INTO account(username, email, hash, salt, discordUserId)", account.username, account.email, hash, salt, account.discordId) //_, err = moodle.AddUser(account.username + "wg", account.username, account.email, account.username, account.password) log(err) if config.CreateGiteaAccount { diff --git a/src/util.go b/src/util.go index 5f11dce..9404ecd 100644 --- a/src/util.go +++ b/src/util.go @@ -3,6 +3,9 @@ import ( "golang.org/x/crypto/argon2" "net/http" "html/template" + "strings" + "context" + "time" ) func log(err error) { @@ -19,3 +22,13 @@ func runTemplate(w http.ResponseWriter, template *template.Template, templateDat var err error = template.Execute(w, templateData) log(err) } +func databaseInsert(query string, values ...interface{}) { + query += " VALUES (" + strings.Repeat("?,", len(values) - 1) + "?);" + ctx, cancelfunc := context.WithTimeout(context.Background(), 5*time.Second) + defer cancelfunc() + stmt, err := db.PrepareContext(ctx, query) + log(err) + defer stmt.Close() + _, err = stmt.ExecContext(ctx, values) + log(err) +}