diff --git a/comments/comments.go b/comments/comments.go index 10e85c9..8f3fecf 100644 --- a/comments/comments.go +++ b/comments/comments.go @@ -5,6 +5,7 @@ import ( "crypto/subtle" "encoding/json" "fmt" + "html/template" "io" "net/http" "sync" @@ -80,7 +81,8 @@ func (cs *Service) ServeComments(post string, w http.ResponseWriter, r *http.Req }) } - _, err := cs.WriteTo(post, w) + w.Header().Set("Content-Type", "text/html") + _, err := cs.WriteHTML(post, w) if err != nil { return err } @@ -96,7 +98,37 @@ func (cs *Service) Load(post string) ([]Comment, error) { return cs.Storage.Retreive(post) } -func (cs *Service) WriteTo(post string, w io.Writer) (int64, error) { +func (cs *Service) WriteHTML(post string, w io.Writer) (int64, error) { + buf := &bytes.Buffer{} + tmpl := template.Must(template.New("").Parse( + `
+ + + +
+ {{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 + } + return io.Copy(w, buf) +} + +func (cs *Service) WriteJSON(post string, w io.Writer) (int64, error) { r, err := cs.Storage.ReadAll(post) if err != nil { return 0, err