optimized error status code responses
This commit is contained in:
@@ -31,12 +31,6 @@ var (
|
||||
errorBadRequest = errors.New("password is empty")
|
||||
)
|
||||
|
||||
func statusNote(code int) *Note {
|
||||
n := &Note{Text: "# " + statuses[code]}
|
||||
n.prepare()
|
||||
return n
|
||||
}
|
||||
|
||||
func (n *Note) prepare() {
|
||||
fstLine := rexpNewLine.Split(n.Text, -1)[0]
|
||||
maxLength := 25
|
||||
@@ -60,7 +54,7 @@ func md2html(c echo.Context, name string) (*Note, int) {
|
||||
if err != nil {
|
||||
c.Logger().Errorf("couldn't open markdown page %s: %v", path, err)
|
||||
code := http.StatusServiceUnavailable
|
||||
return statusNote(code), code
|
||||
return nil, code
|
||||
}
|
||||
c.Logger().Debugf("rendering markdown page %s", name)
|
||||
return &Note{Title: name, Content: mdTmplHTML(mdContent)}, http.StatusOK
|
||||
|
||||
@@ -64,32 +64,39 @@ func main() {
|
||||
|
||||
e.GET("/TOS.md", func(c echo.Context) error {
|
||||
n, code := md2html(c, "TOS")
|
||||
if code != http.StatusOK {
|
||||
c.String(code, statuses[code])
|
||||
}
|
||||
return c.Render(code, "Page", n)
|
||||
})
|
||||
|
||||
e.GET("/:id", func(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
n, code := load(c, db)
|
||||
c.Logger().Debugf("/%s requested; response code: %d", id, code)
|
||||
if code != http.StatusOK {
|
||||
return c.String(code, statuses[code])
|
||||
}
|
||||
defer incViews(n)
|
||||
if fraudelent(n) {
|
||||
n.Ads = mdTmplHTML(ads)
|
||||
}
|
||||
c.Logger().Debugf("/%s requested; response code: %d", n.ID, code)
|
||||
return c.Render(code, "Note", n)
|
||||
})
|
||||
|
||||
e.GET("/:id/export", func(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
n, code := load(c, db)
|
||||
content := statuses[code]
|
||||
if code == http.StatusOK {
|
||||
defer incViews(n)
|
||||
if fraudelent(n) {
|
||||
code = http.StatusForbidden
|
||||
n = statusNote(code)
|
||||
}
|
||||
content = n.Text
|
||||
}
|
||||
c.Logger().Debugf("/%s/export requested; response code: %d", id, code)
|
||||
if code == http.StatusOK {
|
||||
return c.String(code, n.Text)
|
||||
}
|
||||
return c.Render(code, "Note", n)
|
||||
return c.String(code, content)
|
||||
})
|
||||
|
||||
e.GET("/:id/stats", func(c echo.Context) error {
|
||||
@@ -171,9 +178,9 @@ func main() {
|
||||
return c.JSON(http.StatusCreated, postResp{true, n.ID})
|
||||
} else if text == "" {
|
||||
c.Logger().Infof("note %s deleted", n.ID)
|
||||
return c.JSON(http.StatusOK, postResp{true, n.ID})
|
||||
}
|
||||
} else {
|
||||
c.Logger().Infof("note %s updated", n.ID)
|
||||
}
|
||||
return c.JSON(http.StatusOK, postResp{true, n.ID})
|
||||
})
|
||||
|
||||
|
||||
+2
-2
@@ -123,7 +123,7 @@ func load(c echo.Context, db *sql.DB) (*Note, int) {
|
||||
q := c.Param("id")
|
||||
if !rexpNoteID.Match([]byte(q)) {
|
||||
code := http.StatusNotFound
|
||||
return statusNote(code), code
|
||||
return nil, code
|
||||
}
|
||||
c.Logger().Debugf("loading note %s", q)
|
||||
stmt, _ := db.Prepare("select * from notes where id = ?")
|
||||
@@ -135,7 +135,7 @@ func load(c echo.Context, db *sql.DB) (*Note, int) {
|
||||
var views int
|
||||
if err := row.Scan(&id, &text, &published, &editedVal, &password, &views); err != nil {
|
||||
code := http.StatusNotFound
|
||||
return statusNote(code), code
|
||||
return nil, code
|
||||
}
|
||||
n := &Note{
|
||||
ID: id,
|
||||
|
||||
Reference in New Issue
Block a user