prepare statments

This commit is contained in:
2021-04-01 16:35:59 +02:00
parent 113bef6582
commit 6c2f8c286b
3 changed files with 3 additions and 14 deletions

View File

@ -23,6 +23,7 @@ var giteaClient *gitea.Client
var registerTmpl *template.Template
var submitTmpl *template.Template
var loginTmpl *template.Template
var stmtCreateAccount *sql.Stmt
type secrets_json struct {
DiscordToken string `json:"discordToken"`
MysqlIndentify string `json:"mysqlIndentify"`
@ -81,6 +82,7 @@ func main() {
remail = regexp2.MustCompile("^(?=.{0,255}$)(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])$", 0)
rusername = regexp.MustCompile("^([[:lower:]]|\\d|_|-|\\.){1,40}$")
rpassword = regexp2.MustCompile("^(?=.{8,255}$)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\\W).*$", 0)
stmtCreateAccount, err = db.Prepare("INSERT INTO account(username, email, hash, salt, discordUserId) VALUES(?,?,?,?,?)")
http.HandleFunc("/register", register)
http.HandleFunc("/submit", submit)
http.HandleFunc("/login", login)

View File

@ -101,7 +101,7 @@ func register(w http.ResponseWriter, r *http.Request) {
log(err)
hash := hashFunc([]byte(account.password), salt)
// add user to the database
go databaseInsert("INSERT INTO account(username, email, hash, salt, discordUserId)", account.username, account.email, hash, salt, account.discordId)
stmtCreateAccount.Exec(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,9 +3,6 @@ import (
"golang.org/x/crypto/argon2"
"net/http"
"html/template"
"strings"
"context"
"time"
)
func log(err error) {
@ -22,13 +19,3 @@ 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)
}