diff --git a/blog.go b/blog.go index 6244f1a..b855f5b 100644 --- a/blog.go +++ b/blog.go @@ -1,18 +1,24 @@ package main -import "html/template" +import ( + "html/template" + "sync" +) type Post struct { - Id string - Title string - Author string - Date string - HTML template.HTML - Text string + BlogTitle string + Lang string + Id string + Title string + Author string + Date string + HTML template.HTML + Text string } type Blog struct { Title string Lang string Posts []Post + mutex sync.Mutex } diff --git a/html/entry.tmpl b/html/entry.tmpl index cde9c8a..90a06ae 100644 --- a/html/entry.tmpl +++ b/html/entry.tmpl @@ -3,13 +3,13 @@ - {{.Title}} + {{.BlogTitle}} -

{{.Title}}

+

{{.BlogTitle}}

-

{{(index .Posts 0).Title}}

+

{{.Title}}

- {{(index .Posts 0).HTML}} + {{.HTML}}
diff --git a/imap.go b/imap.go index 945e0e2..d472faf 100644 --- a/imap.go +++ b/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 diff --git a/mailblog.go b/mailblog.go index fcfa992..15f93bd 100644 --- a/mailblog.go +++ b/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 } diff --git a/web.go b/web.go index 3dc10aa..397f114 100644 --- a/web.go +++ b/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)