diff --git a/post.go b/blog.go similarity index 93% rename from post.go rename to blog.go index 10c7d7e..6244f1a 100644 --- a/post.go +++ b/blog.go @@ -13,5 +13,6 @@ type Post struct { type Blog struct { Title string + Lang string Posts []Post } diff --git a/configuration.go b/configuration.go index 5b34633..cfc18fc 100644 --- a/configuration.go +++ b/configuration.go @@ -18,7 +18,6 @@ type BlogConfiguration struct { Title string ShortName string MailBox MailBoxConfiguration - WWWRoot string } func (configuration *BlogConfiguration) Prompt() error { @@ -35,10 +34,12 @@ func (configuration *BlogConfiguration) Prompt() error { 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 { - return err - } + /* + configuration.WWWRoot, err = prompt("Web Root directory", nil, fmt.Sprintf("/var/www/html/%s", configuration.ShortName), configuration.WWWRoot, false) + if err != nil { + return err + } + */ configuration.MailBox.Server, err = prompt("IMAP Server Hostname or IP", nil, "", configuration.MailBox.Server, false) if err != nil { return err @@ -71,5 +72,6 @@ func (configuration *BlogConfiguration) Prompt() error { if err != nil { return err } + return nil } diff --git a/html/entry.tmpl b/html/entry.tmpl new file mode 100644 index 0000000..cde9c8a --- /dev/null +++ b/html/entry.tmpl @@ -0,0 +1,16 @@ + + +
+ + +{{.HTML}}
+ + {{ end }} + + \ No newline at end of file diff --git a/imap.go b/imap.go index f768ada..945e0e2 100644 --- a/imap.go +++ b/imap.go @@ -99,7 +99,7 @@ func (mb *MailBox) GetMessages() ([]Post, error) { if inbox.NumMessages <= 0 { return nil, fmt.Errorf("nomessages") } - entries := make([]Post, 0) + posts := make([]Post, 0) for i := uint32(1); i <= inbox.NumMessages; i++ { var post Post seqSet := imap.SeqSetNum(i) @@ -193,7 +193,7 @@ func (mb *MailBox) GetMessages() ([]Post, error) { } posts = append(posts, post) } - return entries, nil + return posts, nil } func (mb *MailBox) Close() { diff --git a/mailblog.go b/mailblog.go index 4f724c1..fcfa992 100644 --- a/mailblog.go +++ b/mailblog.go @@ -4,9 +4,7 @@ import ( _ "embed" "encoding/json" "fmt" - "html/template" "log" - "net/http" "os" ) @@ -63,68 +61,12 @@ func main() { } 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 { log.Fatal(err) } - log.Println(folders) - 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) + StartServer(&blog) } diff --git a/web.go b/web.go new file mode 100644 index 0000000..3dc10aa --- /dev/null +++ b/web.go @@ -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) +}