Ajout d'un mutex pour pouvoir mettre à jour via une goroutine la liste des messages

This commit is contained in:
Laurent Ulrich
2025-08-08 10:59:51 +02:00
parent 614a6059f5
commit 9f88d84513
5 changed files with 42 additions and 31 deletions

11
web.go
View File

@@ -14,6 +14,8 @@ func StartServer(blog *Blog) {
if err != nil {
log.Fatal(err)
}
blog.mutex.Lock()
defer blog.mutex.Unlock()
err = tpl.ExecuteTemplate(w, "index", blog)
if err != nil {
log.Fatal(err)
@@ -24,18 +26,17 @@ func StartServer(blog *Blog) {
func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
log.Println("showing post:", id)
blog.mutex.Lock()
defer blog.mutex.Unlock()
for _, p := range blog.Posts {
log.Println("Examining:", p.Id)
if p.Id == id {
tpl, err := template.New("entry").Parse(entryTemplate)
if err != nil {
log.Fatal(err)
}
var post Blog
post.Title = blog.Title
post := p
post.BlogTitle = blog.Title
post.Lang = blog.Lang
post.Posts = make([]Post, 1)
post.Posts[0] = p
err = tpl.ExecuteTemplate(w, "entry", post)
if err != nil {
log.Fatal(err)