comments appearing; need better embedding approach

pull/10/head
Stephen Searles 7 years ago
parent c1fc9ce6d1
commit 432affea76
  1. 52
      comments/comments.go
  2. 2
      http.go

@ -75,7 +75,7 @@ func (cs *Service) ServeComments(post string, w http.ResponseWriter, r *http.Req
if r.Method == "POST" { if r.Method == "POST" {
return cs.Comment(post, Comment{ return cs.Comment(post, Comment{
Name: user, Name: user,
Text: r.FormValue("Text"), Text: readCommentText(r),
IP: r.RemoteAddr, IP: r.RemoteAddr,
Date: time.Now(), Date: time.Now(),
}) })
@ -90,6 +90,12 @@ func (cs *Service) ServeComments(post string, w http.ResponseWriter, r *http.Req
return nil 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 { func (cs *Service) Comment(post string, c Comment) error {
return cs.Storage.Store(post, c) 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) { func (cs *Service) WriteHTML(post string, w io.Writer) (int64, error) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
tmpl := template.Must(template.New("").Parse( tmpl := template.Must(template.New("").Parse(
`<form onsubmit="javascript:submitComment(event)"> `<style>
comment {
margin: 10px 0;
}
comment, comment name, comment text {
display: block;
}
comment name::after {
content: " says:";
}
comment text {
margin-left: 5px;
padding-left: 5px;
border-left: 2px solid gray;
}
</style>
{{range .}}{{ if .Text }}<comment>
<name>{{.Name}}</name>
<text>{{.Text}}</text>
</comment>
{{end}}{{end}}
<form onsubmit="javascript:submitComment(event)">
<script> <script>
function submitComment(evt) { function submitComment(evt) {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
@ -109,18 +136,24 @@ func (cs *Service) WriteHTML(post string, w io.Writer) (int64, error) {
xhr.send(JSON.stringify({"text": evt.target.querySelector('textarea').value})); xhr.send(JSON.stringify({"text": evt.target.querySelector('textarea').value}));
} }
</script> </script>
<label>comment<textarea placeholder="comment..."></textarea></label> <p>
<div>
<label for="comment">post a comment:</label>
</div>
<div>
<textarea placeholder="comment..." name="comment"></textarea>
</div>
<div>
<button>post</button> <button>post</button>
</form> </div>
{{range $idx, $elem := .}}<comment> </p>
<name>$elem.Name</name> </form>`))
<text>$elem.Text</name>
</comment>
{{end}}`))
comments, err := cs.Load(post) comments, err := cs.Load(post)
if err != nil { if err != nil {
return 0, err return 0, err
} }
err = tmpl.Execute(buf, comments) err = tmpl.Execute(buf, comments)
if err != nil { if err != nil {
return 0, err return 0, err
@ -260,6 +293,7 @@ func (d *diskvStorage) Retreive(post string) ([]Comment, error) {
if err != nil { if err != nil {
return comments, fmt.Errorf("error unmarshaling comment: %v", err) return comments, fmt.Errorf("error unmarshaling comment: %v", err)
} }
comments = append(comments, comment)
} }
return comments, nil return comments, nil

@ -26,7 +26,7 @@ func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, c *caddy.Control
docName := docNameFromCommentRequest(r) docName := docNameFromCommentRequest(r)
err := ch.Comments.ServeComments(docName, w, r) err := ch.Comments.ServeComments(docName, w, r)
if err != nil { if err != nil {
return 500, fmt.Errorf("couldn't load comments") return 500, fmt.Errorf("couldn't load comments:", err)
} }
return 200, nil return 200, nil

Loading…
Cancel
Save