database insert util function

This commit is contained in:
2021-01-14 23:05:56 +01:00
parent 41583afb47
commit 936f2ec6f5
3 changed files with 15 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"net/http"
"database/sql"
"encoding/json"
"github.com/bwmarrin/discordgo"

View File

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

View File

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