Ok, avec titre pour chacun

This commit is contained in:
Laurent Ulrich
2025-07-31 17:06:30 +02:00
parent 67700bcb42
commit 8877efaeca
7 changed files with 106 additions and 70 deletions

View File

@@ -13,5 +13,6 @@ type Post struct {
type Blog struct { type Blog struct {
Title string Title string
Lang string
Posts []Post Posts []Post
} }

View File

@@ -18,7 +18,6 @@ type BlogConfiguration struct {
Title string Title string
ShortName string ShortName string
MailBox MailBoxConfiguration MailBox MailBoxConfiguration
WWWRoot string
} }
func (configuration *BlogConfiguration) Prompt() error { func (configuration *BlogConfiguration) Prompt() error {
@@ -35,10 +34,12 @@ func (configuration *BlogConfiguration) Prompt() error {
return fmt.Errorf("short same invalid") return fmt.Errorf("short same invalid")
} }
configuration.WWWRoot, err = prompt("Web Root directory", nil, fmt.Sprintf("/var/www/html/%s", configuration.ShortName), configuration.WWWRoot, false) /*
if err != nil { configuration.WWWRoot, err = prompt("Web Root directory", nil, fmt.Sprintf("/var/www/html/%s", configuration.ShortName), configuration.WWWRoot, false)
return err if err != nil {
} return err
}
*/
configuration.MailBox.Server, err = prompt("IMAP Server Hostname or IP", nil, "", configuration.MailBox.Server, false) configuration.MailBox.Server, err = prompt("IMAP Server Hostname or IP", nil, "", configuration.MailBox.Server, false)
if err != nil { if err != nil {
return err return err
@@ -71,5 +72,6 @@ func (configuration *BlogConfiguration) Prompt() error {
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }

16
html/entry.tmpl Normal file
View File

@@ -0,0 +1,16 @@
<!doctype html>
<html lang="{{.Lang}}">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>{{.Title}}</title>
</head>
<h1>{{.Title}}</h1>
<body>
<h2>{{(index .Posts 0).Title}}</h2>
<div>
{{(index .Posts 0).HTML}}
</div>
</body>
</html>

18
html/index.tmpl Normal file
View File

@@ -0,0 +1,18 @@
<!doctype html>
<html lang="{{.Lang}}">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>{{.Title}}</title>
</head>
<body>
<h1>{{.Title}}</h1>
{{range .Posts}}
<a href=/proxy/8080/post/{{ .Id }}>
<h2>{{.Title}}</h2>
</a>
<p>{{.HTML}}</p>
{{ end }}
</body>
</html>

View File

@@ -99,7 +99,7 @@ func (mb *MailBox) GetMessages() ([]Post, error) {
if inbox.NumMessages <= 0 { if inbox.NumMessages <= 0 {
return nil, fmt.Errorf("nomessages") return nil, fmt.Errorf("nomessages")
} }
entries := make([]Post, 0) posts := make([]Post, 0)
for i := uint32(1); i <= inbox.NumMessages; i++ { for i := uint32(1); i <= inbox.NumMessages; i++ {
var post Post var post Post
seqSet := imap.SeqSetNum(i) seqSet := imap.SeqSetNum(i)
@@ -193,7 +193,7 @@ func (mb *MailBox) GetMessages() ([]Post, error) {
} }
posts = append(posts, post) posts = append(posts, post)
} }
return entries, nil return posts, nil
} }
func (mb *MailBox) Close() { func (mb *MailBox) Close() {

View File

@@ -4,9 +4,7 @@ import (
_ "embed" _ "embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"html/template"
"log" "log"
"net/http"
"os" "os"
) )
@@ -63,68 +61,12 @@ func main() {
} }
defer mb.Close() defer mb.Close()
folders, err := mb.ListFolders() var blog Blog
blog.Lang = "fr-FR"
blog.Title = configuration.Title
blog.Posts, err = mb.GetMessages()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
log.Println(folders) StartServer(&blog)
messages, err := mb.GetMessages()
if err != nil {
log.Fatal(err)
}
/*
for _, m := range msgs {
log.Println(m.Id)
log.Println(m.Title)
log.Println(m.Author)
log.Println("Text version")
log.Println(m.Text)
log.Println("HTML version")
log.Println(m.HTML)
log.Println(m.Id)
log.Println()
}
*/
http.HandleFunc("GET /{$}",
func(w http.ResponseWriter, r *http.Request) {
tpl, err := template.New("index").Parse(indexTemplate)
if err != nil {
log.Fatal(err)
}
err = tpl.ExecuteTemplate(w, "index", messages)
if err != nil {
log.Fatal(err)
}
})
http.HandleFunc("GET /post/{id}",
func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
log.Println("showing message:", id)
for _, m := range messages {
log.Println("Examining:", m.Id)
if m.Id == id {
tpl, err := template.New("entry").Parse(entryTemplate)
if err != nil {
log.Fatal(err)
}
err = tpl.ExecuteTemplate(w, "entry", m)
if err != nil {
log.Fatal(err)
}
return
}
}
w.WriteHeader(404)
})
http.HandleFunc("GET /favicon.ico",
func(w http.ResponseWriter, r *http.Request) {
log.Println("favicon.ico")
w.WriteHeader(200)
})
http.ListenAndServe("0.0.0.0:8080", nil)
} }

57
web.go Normal file
View File

@@ -0,0 +1,57 @@
package main
import (
"html/template"
"log"
"net/http"
)
func StartServer(blog *Blog) {
http.HandleFunc("GET /{$}",
func(w http.ResponseWriter, r *http.Request) {
tpl, err := template.New("index").Parse(indexTemplate)
if err != nil {
log.Fatal(err)
}
err = tpl.ExecuteTemplate(w, "index", blog)
if err != nil {
log.Fatal(err)
}
})
http.HandleFunc("GET /post/{id}",
func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
log.Println("showing post:", id)
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.Lang = blog.Lang
post.Posts = make([]Post, 1)
post.Posts[0] = p
err = tpl.ExecuteTemplate(w, "entry", post)
if err != nil {
log.Fatal(err)
}
return
}
}
w.WriteHeader(404)
})
http.HandleFunc("GET /favicon.ico",
func(w http.ResponseWriter, r *http.Request) {
log.Println("favicon.ico")
w.WriteHeader(200)
})
http.ListenAndServe("0.0.0.0:8080", nil)
}