Browse Source

minor improvements

master
Christian Müller 8 years ago
parent
commit
05cefcef88
  1. 8
      render.go
  2. 30
      server.go
  3. 2
      storage.go

8
render.go

@ -14,7 +14,7 @@ import ( @@ -14,7 +14,7 @@ import (
)
var (
errorCodes = map[int]string{
statuses = map[int]string{
400: "Bad request",
401: "Unauthorized",
404: "Not found",
@ -32,8 +32,8 @@ var ( @@ -32,8 +32,8 @@ var (
errorBadRequest = errors.New("password is empty")
)
func errPage(code int, details ...string) *Note {
text := errorCodes[code]
func responsePage(code int, details ...string) *Note {
text := statuses[code]
body := text
if len(details) > 0 {
body += ": " + strings.Join(details, ";")
@ -69,7 +69,7 @@ func md2html(c echo.Context, name string) (*Note, int) { @@ -69,7 +69,7 @@ func md2html(c echo.Context, name string) (*Note, int) {
if err != nil {
c.Logger().Errorf("couldn't open markdown page %q: %v", path, err)
code := http.StatusServiceUnavailable
return errPage(code), code
return responsePage(code), code
}
c.Logger().Debugf("rendering markdown page %q", name)
return &Note{Title: name, Content: mdTmplHTML(mdContent)}, http.StatusOK

30
server.go

@ -7,7 +7,6 @@ import ( @@ -7,7 +7,6 @@ import (
"io/ioutil"
"math"
"net/http"
"net/url"
"os"
"sync"
@ -115,29 +114,25 @@ func main() { @@ -115,29 +114,25 @@ func main() {
if !legitAccess(c) {
code := http.StatusTooManyRequests
c.Logger().Errorf("rate limit exceeded for %s", c.Request().RemoteAddr)
return c.Render(code, "Note", errPage(code))
return c.Render(code, "Note", responsePage(code))
}
vals, err := c.FormParams()
if err != nil {
return err
}
if get(vals, "tos") != "on" {
if c.FormValue("tos") != "on" {
code := http.StatusPreconditionFailed
c.Logger().Errorf("POST /note error: %d", code)
return c.Render(code, "Note", errPage(code))
return c.Render(code, "Note", responsePage(code))
}
text := get(vals, "text")
text := c.FormValue("text")
if 10 > len(text) || len(text) > 50000 {
code := http.StatusBadRequest
c.Logger().Errorf("POST /note error: %d", code)
return c.Render(code, "Note",
errPage(code, "note length not accepted"))
responsePage(code, "note length not accepted"))
}
id := get(vals, "id")
id := c.FormValue("id")
n := &Note{
ID: id,
Text: text,
Password: get(vals, "password"),
Password: c.FormValue("password"),
}
n, err = save(c, db, n)
if err != nil {
@ -149,7 +144,7 @@ func main() { @@ -149,7 +144,7 @@ func main() {
code = http.StatusBadRequest
}
c.Logger().Errorf("POST /note error: %d", code)
return c.Render(code, "Note", errPage(code, err.Error()))
return c.Render(code, "Note", responsePage(code, err.Error()))
}
c.Logger().Debugf("note %q saved", n.ID)
return c.Redirect(http.StatusMovedPermanently, "/"+n.ID)
@ -158,15 +153,6 @@ func main() { @@ -158,15 +153,6 @@ func main() {
e.Logger.Fatal(e.Start(":3000"))
}
func get(vals url.Values, key string) string {
if list, found := vals[key]; found {
if len(list) == 1 {
return list[0]
}
}
return ""
}
func fraudelent(n *Note) bool {
stripped := rexpLink.ReplaceAllString(n.Text, "")
l1 := len(n.Text)

2
storage.go

@ -115,7 +115,7 @@ func load(c echo.Context, db *sql.DB) (*Note, int) { @@ -115,7 +115,7 @@ func load(c echo.Context, db *sql.DB) (*Note, int) {
if err := row.Scan(&id, &text, &published, &editedVal, &password, &views); err != nil {
c.Logger().Error(err)
code := http.StatusNotFound
return errPage(code), code
return responsePage(code), code
}
n := &Note{
ID: id,

Loading…
Cancel
Save