From 432affea76497f82f2617c5e957943a07216e100 Mon Sep 17 00:00:00 2001 From: Stephen Searles Date: Tue, 5 Sep 2017 09:20:06 -0500 Subject: [PATCH] comments appearing; need better embedding approach --- comments/comments.go | 54 ++++++++++++++++++++++++++++++++++++-------- http.go | 2 +- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/comments/comments.go b/comments/comments.go index 8f3fecf..04451e1 100644 --- a/comments/comments.go +++ b/comments/comments.go @@ -75,7 +75,7 @@ func (cs *Service) ServeComments(post string, w http.ResponseWriter, r *http.Req if r.Method == "POST" { return cs.Comment(post, Comment{ Name: user, - Text: r.FormValue("Text"), + Text: readCommentText(r), IP: r.RemoteAddr, Date: time.Now(), }) @@ -90,6 +90,12 @@ func (cs *Service) ServeComments(post string, w http.ResponseWriter, r *http.Req return nil } +func readCommentText(r *http.Request) string { + comment := struct{ Text string }{} + json.NewDecoder(r.Body).Decode(&comment) + return comment.Text +} + func (cs *Service) Comment(post string, c Comment) error { return cs.Storage.Store(post, c) } @@ -101,7 +107,28 @@ func (cs *Service) Load(post string) ([]Comment, error) { func (cs *Service) WriteHTML(post string, w io.Writer) (int64, error) { buf := &bytes.Buffer{} tmpl := template.Must(template.New("").Parse( - `
+ ` + {{range .}}{{ if .Text }} + {{.Name}} + {{.Text}} + + {{end}}{{end}} + - - -
- {{range $idx, $elem := .}} - $elem.Name - $elem.Text - - {{end}}`)) +

+

+ +
+
+ +
+
+ +
+

+ `)) + comments, err := cs.Load(post) if err != nil { return 0, err } + err = tmpl.Execute(buf, comments) if err != nil { return 0, err @@ -260,6 +293,7 @@ func (d *diskvStorage) Retreive(post string) ([]Comment, error) { if err != nil { return comments, fmt.Errorf("error unmarshaling comment: %v", err) } + comments = append(comments, comment) } return comments, nil diff --git a/http.go b/http.go index 14849a4..5bfbbe6 100644 --- a/http.go +++ b/http.go @@ -26,7 +26,7 @@ func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, c *caddy.Control docName := docNameFromCommentRequest(r) err := ch.Comments.ServeComments(docName, w, r) if err != nil { - return 500, fmt.Errorf("couldn't load comments") + return 500, fmt.Errorf("couldn't load comments:", err) } return 200, nil