diff --git a/render.go b/render.go index ce6f394..79644b3 100644 --- a/render.go +++ b/render.go @@ -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 diff --git a/server.go b/server.go index 071c481..afd35ab 100644 --- a/server.go +++ b/server.go @@ -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) - defer incViews(n) - if fraudelent(n) { - code = http.StatusForbidden - n = statusNote(code) - } - c.Logger().Debugf("/%s/export requested; response code: %d", id, code) + content := statuses[code] 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 { @@ -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) } - c.Logger().Infof("note %s updated", n.ID) return c.JSON(http.StatusOK, postResp{true, n.ID}) }) diff --git a/storage.go b/storage.go index 4a85989..b944a2e 100644 --- a/storage.go +++ b/storage.go @@ -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,