Ajout d'un mutex pour pouvoir mettre à jour via une goroutine la liste des messages
This commit is contained in:
20
blog.go
20
blog.go
@@ -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
|
||||||
}
|
}
|
||||||
|
@@ -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>
|
||||||
|
|
||||||
|
7
imap.go
7
imap.go
@@ -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
|
||||||
|
27
mailblog.go
27
mailblog.go
@@ -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
11
web.go
@@ -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)
|
||||||
|
Reference in New Issue
Block a user