Don't use EDIT method, only POSTs should use alt methods

main
Brian Picciano 2 years ago
parent c4692f72e3
commit 1181af0318
  1. 3
      srv/src/http/api.go
  2. 17
      srv/src/http/posts.go
  3. 4
      srv/src/http/tpl/posts.html

@ -204,8 +204,7 @@ func (a *api) handler() http.Handler {
mux.Handle("/posts/", http.StripPrefix("/posts", mux.Handle("/posts/", http.StripPrefix("/posts",
apiutil.MethodMux(map[string]http.Handler{ apiutil.MethodMux(map[string]http.Handler{
"GET": a.renderPostHandler(), "GET": a.renderPostHandler(),
"EDIT": a.editPostHandler(),
"POST": authMiddleware(a.auther, "POST": authMiddleware(a.auther,
formMiddleware(a.postPostHandler()), formMiddleware(a.postPostHandler()),
), ),

@ -84,14 +84,20 @@ func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload,
func (a *api) renderPostHandler() http.Handler { func (a *api) renderPostHandler() http.Handler {
tpl := a.mustParseBasedTpl("post.html") tpl := a.mustParseBasedTpl("post.html")
renderIndexHandler := a.renderPostsIndexHandler() renderPostsIndexHandler := a.renderPostsIndexHandler()
renderEditPostHandler := a.renderEditPostHandler()
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
id := strings.TrimSuffix(filepath.Base(r.URL.Path), ".html") id := strings.TrimSuffix(filepath.Base(r.URL.Path), ".html")
if id == "/" { if id == "/" {
renderIndexHandler.ServeHTTP(rw, r) renderPostsIndexHandler.ServeHTTP(rw, r)
return
}
if _, ok := r.URL.Query()["edit"]; ok {
renderEditPostHandler.ServeHTTP(rw, r)
return return
} }
@ -167,7 +173,7 @@ func (a *api) renderPostsIndexHandler() http.Handler {
}) })
} }
func (a *api) editPostHandler() http.Handler { func (a *api) renderEditPostHandler() http.Handler {
tpl := a.mustParseBasedTpl("edit-post.html") tpl := a.mustParseBasedTpl("edit-post.html")
@ -208,8 +214,9 @@ func postFromPostReq(r *http.Request) (post.Post, error) {
} }
// textareas encode newlines as CRLF for historical reasons // textareas encode newlines as CRLF for historical reasons
p.Body = r.PostFormValue("body")
p.Body = strings.ReplaceAll(p.Body, "\r\n", "\n") p.Body = strings.ReplaceAll(p.Body, "\r\n", "\n")
p.Body = strings.TrimSpace(r.PostFormValue("body")) p.Body = strings.TrimSpace(p.Body)
if p.ID == "" || if p.ID == "" ||
p.Title == "" || p.Title == "" ||
@ -239,7 +246,7 @@ func (a *api) postPostHandler() http.Handler {
return return
} }
redirectPath := fmt.Sprintf("posts/%s?method=edit", p.ID) redirectPath := fmt.Sprintf("posts/%s?edit", p.ID)
a.executeRedirectTpl(rw, r, redirectPath) a.executeRedirectTpl(rw, r, redirectPath)
}) })

@ -22,7 +22,7 @@
<p style="text-align: center;"> <p style="text-align: center;">
<a href="{{ BlogURL "posts/" }}?method=edit"> <a href="{{ BlogURL "posts/" }}?edit">
<button>New Post</button> <button>New Post</button>
</a> </a>
</p> </p>
@ -36,7 +36,7 @@
<td>{{ .PublishedAt }}</td> <td>{{ .PublishedAt }}</td>
<td><a href="{{ PostURL .ID }}" target="_blank">{{ .Title }}</a></td> <td><a href="{{ PostURL .ID }}" target="_blank">{{ .Title }}</a></td>
<td> <td>
<a href="{{ PostURL .ID }}?method=edit"> <a href="{{ PostURL .ID }}?edit">
<button>Edit</button> <button>Edit</button>
</a> </a>
</td> </td>

Loading…
Cancel
Save