Browse Source

optimized error status code responses

master
Christian Müller 8 years ago
parent
commit
28de7bacda
  1. 8
      render.go
  2. 29
      server.go
  3. 4
      storage.go

8
render.go

@ -31,12 +31,6 @@ var (
errorBadRequest = errors.New("password is empty") errorBadRequest = errors.New("password is empty")
) )
func statusNote(code int) *Note {
n := &Note{Text: "# " + statuses[code]}
n.prepare()
return n
}
func (n *Note) prepare() { func (n *Note) prepare() {
fstLine := rexpNewLine.Split(n.Text, -1)[0] fstLine := rexpNewLine.Split(n.Text, -1)[0]
maxLength := 25 maxLength := 25
@ -60,7 +54,7 @@ func md2html(c echo.Context, name string) (*Note, int) {
if err != nil { if err != nil {
c.Logger().Errorf("couldn't open markdown page %s: %v", path, err) c.Logger().Errorf("couldn't open markdown page %s: %v", path, err)
code := http.StatusServiceUnavailable code := http.StatusServiceUnavailable
return statusNote(code), code return nil, code
} }
c.Logger().Debugf("rendering markdown page %s", name) c.Logger().Debugf("rendering markdown page %s", name)
return &Note{Title: name, Content: mdTmplHTML(mdContent)}, http.StatusOK return &Note{Title: name, Content: mdTmplHTML(mdContent)}, http.StatusOK

29
server.go

@ -64,32 +64,39 @@ func main() {
e.GET("/TOS.md", func(c echo.Context) error { e.GET("/TOS.md", func(c echo.Context) error {
n, code := md2html(c, "TOS") n, code := md2html(c, "TOS")
if code != http.StatusOK {
c.String(code, statuses[code])
}
return c.Render(code, "Page", n) return c.Render(code, "Page", n)
}) })
e.GET("/:id", func(c echo.Context) error { e.GET("/:id", func(c echo.Context) error {
id := c.Param("id")
n, code := load(c, db) 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) defer incViews(n)
if fraudelent(n) { if fraudelent(n) {
n.Ads = mdTmplHTML(ads) n.Ads = mdTmplHTML(ads)
} }
c.Logger().Debugf("/%s requested; response code: %d", n.ID, code)
return c.Render(code, "Note", n) return c.Render(code, "Note", n)
}) })
e.GET("/:id/export", func(c echo.Context) error { e.GET("/:id/export", func(c echo.Context) error {
id := c.Param("id") id := c.Param("id")
n, code := load(c, db) n, code := load(c, db)
defer incViews(n) content := statuses[code]
if fraudelent(n) {
code = http.StatusForbidden
n = statusNote(code)
}
c.Logger().Debugf("/%s/export requested; response code: %d", id, code)
if code == http.StatusOK { if code == http.StatusOK {
return c.String(code, n.Text) defer incViews(n)
if fraudelent(n) {
code = http.StatusForbidden
}
content = n.Text
} }
return c.Render(code, "Note", n) c.Logger().Debugf("/%s/export requested; response code: %d", id, code)
return c.String(code, content)
}) })
e.GET("/:id/stats", func(c echo.Context) error { e.GET("/:id/stats", func(c echo.Context) error {
@ -171,9 +178,9 @@ func main() {
return c.JSON(http.StatusCreated, postResp{true, n.ID}) return c.JSON(http.StatusCreated, postResp{true, n.ID})
} else if text == "" { } else if text == "" {
c.Logger().Infof("note %s deleted", n.ID) 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)
} }
c.Logger().Infof("note %s updated", n.ID)
return c.JSON(http.StatusOK, postResp{true, n.ID}) return c.JSON(http.StatusOK, postResp{true, n.ID})
}) })

4
storage.go

@ -123,7 +123,7 @@ func load(c echo.Context, db *sql.DB) (*Note, int) {
q := c.Param("id") q := c.Param("id")
if !rexpNoteID.Match([]byte(q)) { if !rexpNoteID.Match([]byte(q)) {
code := http.StatusNotFound code := http.StatusNotFound
return statusNote(code), code return nil, code
} }
c.Logger().Debugf("loading note %s", q) c.Logger().Debugf("loading note %s", q)
stmt, _ := db.Prepare("select * from notes where id = ?") 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 var views int
if err := row.Scan(&id, &text, &published, &editedVal, &password, &views); err != nil { if err := row.Scan(&id, &text, &published, &editedVal, &password, &views); err != nil {
code := http.StatusNotFound code := http.StatusNotFound
return statusNote(code), code return nil, code
} }
n := &Note{ n := &Note{
ID: id, ID: id,

Loading…
Cancel
Save