|
|
@ -5,6 +5,7 @@ import ( |
|
|
|
"crypto/subtle" |
|
|
|
"crypto/subtle" |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"html/template" |
|
|
|
"io" |
|
|
|
"io" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"sync" |
|
|
|
"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 { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -96,7 +98,37 @@ func (cs *Service) Load(post string) ([]Comment, error) { |
|
|
|
return cs.Storage.Retreive(post) |
|
|
|
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( |
|
|
|
|
|
|
|
`<form onsubmit="javascript:submitComment(event)"> |
|
|
|
|
|
|
|
<script> |
|
|
|
|
|
|
|
function submitComment(evt) { |
|
|
|
|
|
|
|
var xhr = new XMLHttpRequest(); |
|
|
|
|
|
|
|
xhr.open('POST', document.location.pathname, true); |
|
|
|
|
|
|
|
xhr.send(JSON.stringify({"text": evt.target.querySelector('textarea').value})); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
</script> |
|
|
|
|
|
|
|
<label>comment<textarea placeholder="comment..."></textarea></label> |
|
|
|
|
|
|
|
<button>post</button> |
|
|
|
|
|
|
|
</form> |
|
|
|
|
|
|
|
{{range $idx, $elem := .}}<comment> |
|
|
|
|
|
|
|
<name>$elem.Name</name> |
|
|
|
|
|
|
|
<text>$elem.Text</name> |
|
|
|
|
|
|
|
</comment> |
|
|
|
|
|
|
|
{{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) |
|
|
|
r, err := cs.Storage.ReadAll(post) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return 0, err |
|
|
|
return 0, err |
|
|
|