clean up responses and logging, change endpoint
- return an error with invalid request types - simplify json decoding - return error and success consistent with app conventions - endpoint change from /api/generate/markdownify to /api/markdown - fix nil pointer dereference when passing a base_url
This commit is contained in:
parent
a266d8e032
commit
26d906ae92
|
@ -15,7 +15,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -24,7 +23,9 @@ import (
|
||||||
|
|
||||||
"github.com/microcosm-cc/bluemonday"
|
"github.com/microcosm-cc/bluemonday"
|
||||||
stripmd "github.com/writeas/go-strip-markdown"
|
stripmd "github.com/writeas/go-strip-markdown"
|
||||||
|
"github.com/writeas/impart"
|
||||||
blackfriday "github.com/writeas/saturday"
|
blackfriday "github.com/writeas/saturday"
|
||||||
|
"github.com/writeas/web-core/log"
|
||||||
"github.com/writeas/web-core/stringmanip"
|
"github.com/writeas/web-core/stringmanip"
|
||||||
"github.com/writeas/writefreely/config"
|
"github.com/writeas/writefreely/config"
|
||||||
"github.com/writeas/writefreely/parse"
|
"github.com/writeas/writefreely/parse"
|
||||||
|
@ -240,7 +241,7 @@ func shortPostDescription(content string) string {
|
||||||
|
|
||||||
func handleRenderMarkdown(app *App, w http.ResponseWriter, r *http.Request) error {
|
func handleRenderMarkdown(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
if !IsJSON(r) {
|
if !IsJSON(r) {
|
||||||
fmt.Println("missing header")
|
return impart.HTTPError{Status: http.StatusUnsupportedMediaType, Message: "Markdown API only supports JSON requests"}
|
||||||
}
|
}
|
||||||
|
|
||||||
in := struct {
|
in := struct {
|
||||||
|
@ -250,28 +251,12 @@ func handleRenderMarkdown(app *App, w http.ResponseWriter, r *http.Request) erro
|
||||||
BaseURL: "",
|
BaseURL: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
|
err := decoder.Decode(&in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrInternalGeneral
|
log.Error("Couldn't parse markdown JSON request: %v", err)
|
||||||
|
return ErrBadJSON
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &in)
|
return impart.WriteSuccess(w, applyMarkdown([]byte(in.RawBody), in.BaseURL, app.cfg), http.StatusOK)
|
||||||
if err != nil {
|
|
||||||
return ErrInternalGeneral
|
|
||||||
}
|
|
||||||
|
|
||||||
out := struct {
|
|
||||||
Body string `json:"body"`
|
|
||||||
}{
|
|
||||||
Body: applyMarkdown([]byte(in.RawBody), in.BaseURL, nil),
|
|
||||||
}
|
|
||||||
|
|
||||||
js, err := json.Marshal(out)
|
|
||||||
if err != nil {
|
|
||||||
return ErrInternalGeneral
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write(js)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,8 +110,7 @@ func InitRoutes(apper Apper, r *mux.Router) *mux.Router {
|
||||||
// Sign up validation
|
// Sign up validation
|
||||||
write.HandleFunc("/api/alias", handler.All(handleUsernameCheck)).Methods("POST")
|
write.HandleFunc("/api/alias", handler.All(handleUsernameCheck)).Methods("POST")
|
||||||
|
|
||||||
apiGenerate := write.PathPrefix("/api/generate/").Subrouter()
|
write.HandleFunc("/api/markdown", handler.All(handleRenderMarkdown)).Methods("POST")
|
||||||
apiGenerate.HandleFunc("/markdownify", handler.All(handleRenderMarkdown)).Methods("POST")
|
|
||||||
|
|
||||||
// Handle collections
|
// Handle collections
|
||||||
write.HandleFunc("/api/collections", handler.All(newCollection)).Methods("POST")
|
write.HandleFunc("/api/collections", handler.All(newCollection)).Methods("POST")
|
||||||
|
|
Loading…
Reference in New Issue