Generierte UUID in einen asynchronen Speicher schreiben

This commit is contained in:
Jason Streifling 2023-10-10 19:50:19 +02:00
parent 616df72041
commit 608879d008
3 changed files with 11 additions and 4 deletions

View File

@ -18,13 +18,16 @@ func main() {
log.Fatalln(err)
}
uuidChan := make(chan string)
defer close(uuidChan)
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
mux.HandleFunc("/", server.DisplayTable(db))
mux.HandleFunc("/search/", server.DisplayResults(db))
mux.HandleFunc("/new-briefing/", server.DisplayForm(&i))
mux.HandleFunc("/add-participant/", server.AddParticipant(&i))
mux.HandleFunc("/submit-form/", server.SubmitForm(db, &i, &j))
mux.HandleFunc("/generate-uuid/", server.GenerateUUID())
mux.HandleFunc("/generate-uuid/", server.GenerateUUID(uuidChan))
log.Fatalln(http.ListenAndServe(":8080", mux))
}

View File

@ -0,0 +1 @@
package data

View File

@ -7,7 +7,6 @@ import (
"html/template"
"log"
"net/http"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
"streifling.com/jason/sicherheitsunterweisung/packages/types"
)
@ -73,11 +72,12 @@ func SubmitForm(db *data.DB, i, j *int64) http.HandlerFunc {
if err != nil {
_ = fmt.Errorf("SubmitForm: db.ReadAll(): %v\n", err)
}
template.Must(template.ParseFiles("templates/table.html")).Execute(w, bs)
}
}
func GenerateUUID() http.HandlerFunc {
func GenerateUUID(ch chan<- string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bs := make([]byte, 4)
@ -86,6 +86,9 @@ func GenerateUUID() http.HandlerFunc {
_ = fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
}
template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "uuid", hex.EncodeToString(bs))
uuid := hex.EncodeToString(bs)
ch <- uuid
template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "uuid", uuid)
}
}