Ajout d'un mutex pour pouvoir mettre à jour via une goroutine la liste des messages
This commit is contained in:
8
blog.go
8
blog.go
@@ -1,8 +1,13 @@
|
||||
package main
|
||||
|
||||
import "html/template"
|
||||
import (
|
||||
"html/template"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Post struct {
|
||||
BlogTitle string
|
||||
Lang string
|
||||
Id string
|
||||
Title string
|
||||
Author string
|
||||
@@ -15,4 +20,5 @@ type Blog struct {
|
||||
Title string
|
||||
Lang string
|
||||
Posts []Post
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
@@ -3,13 +3,13 @@
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<title>{{.Title}}</title>
|
||||
<title>{{.BlogTitle}}</title>
|
||||
</head>
|
||||
<h1>{{.Title}}</h1>
|
||||
<h1>{{.BlogTitle}}</h1>
|
||||
<body>
|
||||
<h2>{{(index .Posts 0).Title}}</h2>
|
||||
<h2>{{.Title}}</h2>
|
||||
<div>
|
||||
{{(index .Posts 0).HTML}}
|
||||
{{.HTML}}
|
||||
</div>
|
||||
</body>
|
||||
|
||||
|
7
imap.go
7
imap.go
@@ -168,11 +168,9 @@ func (mb *MailBox) GetMessages() ([]Post, error) {
|
||||
post.HTML = ""
|
||||
break
|
||||
}
|
||||
log.Println("Trying to generate html for", htmlNode.Data)
|
||||
var buf bytes.Buffer
|
||||
err = nil
|
||||
for child := htmlNode.FirstChild; child != nil; child = child.NextSibling {
|
||||
log.Println("html for", child.Data)
|
||||
if err = html.Render(&buf, child); err != nil {
|
||||
post.HTML = ""
|
||||
break
|
||||
@@ -201,15 +199,10 @@ func (mb *MailBox) Close() {
|
||||
}
|
||||
|
||||
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" {
|
||||
log.Println("Found", node.Data)
|
||||
return node, nil
|
||||
}
|
||||
for e := node.FirstChild; e != nil; e = node.NextSibling {
|
||||
log.Println("Search in", e.Data)
|
||||
n, err := findHTMLNode(e)
|
||||
if err == nil {
|
||||
return n, nil
|
||||
|
27
mailblog.go
27
mailblog.go
@@ -25,7 +25,10 @@ func main() {
|
||||
if err != nil {
|
||||
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 {
|
||||
defer file.Close()
|
||||
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
|
||||
mb.Configure(&configuration.MailBox)
|
||||
err = mb.Connect()
|
||||
err := mb.Connect()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer mb.Close()
|
||||
|
||||
var blog Blog
|
||||
blog.Lang = "fr-FR"
|
||||
blog.Title = configuration.Title
|
||||
blog.Posts, err = mb.GetMessages()
|
||||
posts, err := mb.GetMessages()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Println(err)
|
||||
}
|
||||
StartServer(&blog)
|
||||
blog.mutex.Lock()
|
||||
defer blog.mutex.Unlock()
|
||||
blog.Posts = posts
|
||||
}
|
||||
|
11
web.go
11
web.go
@@ -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)
|
||||
|
Reference in New Issue
Block a user