Add comments per heading

This commit is contained in:
Bel LaPointe
2020-08-07 12:30:27 -06:00
parent 785215bd3c
commit 8bf7503c8e
5 changed files with 206 additions and 3 deletions

37
server/comment.go Executable file
View File

@@ -0,0 +1,37 @@
package server
import (
"html"
"local/notes-server/filetree"
"log"
"net/http"
"path"
"strconv"
"strings"
)
func (s *Server) comment(w http.ResponseWriter, r *http.Request) {
log.Println("COMMAND", r.Method, r.FormValue("lineno"), r.FormValue("content"))
if r.Method != "POST" {
http.NotFound(w, r)
return
}
linenos := r.FormValue("lineno")
lineno, err := strconv.Atoi(linenos)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
comment := r.FormValue("content")
comment = html.UnescapeString(comment)
comment = strings.ReplaceAll(comment, "\r", "")
err = s.Notes.Comment(r.URL.Path, lineno, comment)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
url := *r.URL
url.Path = path.Join("/notes/", filetree.NewPathFromURL(r.URL.Path).BaseHREF)
http.Redirect(w, r, url.String(), http.StatusSeeOther)
}

View File

@@ -33,6 +33,9 @@ func (s *Server) Routes() error {
fmt.Sprintf("create/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.create)),
},
fmt.Sprintf("comment/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.comment)),
},
fmt.Sprintf("search"): {
handler: s.gzip(s.authenticate(s.search)),
},