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

20
blog.go
View File

@@ -1,18 +1,24 @@
package main package main
import "html/template" import (
"html/template"
"sync"
)
type Post struct { type Post struct {
Id string BlogTitle string
Title string Lang string
Author string Id string
Date string Title string
HTML template.HTML Author string
Text string Date string
HTML template.HTML
Text string
} }
type Blog struct { type Blog struct {
Title string Title string
Lang string Lang string
Posts []Post Posts []Post
mutex sync.Mutex
} }

View File

@@ -3,13 +3,13 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width" /> <meta name="viewport" content="width=device-width" />
<title>{{.Title}}</title> <title>{{.BlogTitle}}</title>
</head> </head>
<h1>{{.Title}}</h1> <h1>{{.BlogTitle}}</h1>
<body> <body>
<h2>{{(index .Posts 0).Title}}</h2> <h2>{{.Title}}</h2>
<div> <div>
{{(index .Posts 0).HTML}} {{.HTML}}
</div> </div>
</body> </body>

View File

@@ -168,11 +168,9 @@ func (mb *MailBox) GetMessages() ([]Post, error) {
post.HTML = "" post.HTML = ""
break break
} }
log.Println("Trying to generate html for", htmlNode.Data)
var buf bytes.Buffer var buf bytes.Buffer
err = nil err = nil
for child := htmlNode.FirstChild; child != nil; child = child.NextSibling { for child := htmlNode.FirstChild; child != nil; child = child.NextSibling {
log.Println("html for", child.Data)
if err = html.Render(&buf, child); err != nil { if err = html.Render(&buf, child); err != nil {
post.HTML = "" post.HTML = ""
break break
@@ -201,15 +199,10 @@ func (mb *MailBox) Close() {
} }
func findHTMLNode(node *html.Node) (*html.Node, error) { func findHTMLNode(node *html.Node) (*html.Node, error) {
if node.Type == html.ElementNode {
log.Println(node.Data)
}
if node.Type == html.ElementNode && node.Data == "body" { if node.Type == html.ElementNode && node.Data == "body" {
log.Println("Found", node.Data)
return node, nil return node, nil
} }
for e := node.FirstChild; e != nil; e = node.NextSibling { for e := node.FirstChild; e != nil; e = node.NextSibling {
log.Println("Search in", e.Data)
n, err := findHTMLNode(e) n, err := findHTMLNode(e)
if err == nil { if err == nil {
return n, nil return n, nil

View File

@@ -25,7 +25,10 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
file, err := os.Open(fmt.Sprintf("%s/.config/mailblog.json", home)) configurationFileName := fmt.Sprintf("%s/.config/mailblog.json", home)
log.Println("Mailblog starting using", configurationFileName)
file, err := os.Open(configurationFileName)
if err == nil { if err == nil {
defer file.Close() defer file.Close()
decoder := json.NewDecoder(file) decoder := json.NewDecoder(file)
@@ -53,20 +56,28 @@ func main() {
} }
} }
var blog Blog
blog.Lang = "fr-FR"
blog.Title = configuration.Title
go MailboxFetcher(configuration, &blog)
StartServer(&blog)
}
func MailboxFetcher(configuration BlogConfiguration, blog *Blog) {
var mb MailBox var mb MailBox
mb.Configure(&configuration.MailBox) mb.Configure(&configuration.MailBox)
err = mb.Connect() err := mb.Connect()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer mb.Close() defer mb.Close()
var blog Blog posts, err := mb.GetMessages()
blog.Lang = "fr-FR"
blog.Title = configuration.Title
blog.Posts, err = mb.GetMessages()
if err != nil { if err != nil {
log.Fatal(err) log.Println(err)
} }
StartServer(&blog) blog.mutex.Lock()
defer blog.mutex.Unlock()
blog.Posts = posts
} }

11
web.go
View File

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