minor improvements
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
errorCodes = map[int]string{
|
statuses = map[int]string{
|
||||||
400: "Bad request",
|
400: "Bad request",
|
||||||
401: "Unauthorized",
|
401: "Unauthorized",
|
||||||
404: "Not found",
|
404: "Not found",
|
||||||
@@ -32,8 +32,8 @@ var (
|
|||||||
errorBadRequest = errors.New("password is empty")
|
errorBadRequest = errors.New("password is empty")
|
||||||
)
|
)
|
||||||
|
|
||||||
func errPage(code int, details ...string) *Note {
|
func responsePage(code int, details ...string) *Note {
|
||||||
text := errorCodes[code]
|
text := statuses[code]
|
||||||
body := text
|
body := text
|
||||||
if len(details) > 0 {
|
if len(details) > 0 {
|
||||||
body += ": " + strings.Join(details, ";")
|
body += ": " + strings.Join(details, ";")
|
||||||
@@ -69,7 +69,7 @@ func md2html(c echo.Context, name string) (*Note, int) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
c.Logger().Errorf("couldn't open markdown page %q: %v", path, err)
|
c.Logger().Errorf("couldn't open markdown page %q: %v", path, err)
|
||||||
code := http.StatusServiceUnavailable
|
code := http.StatusServiceUnavailable
|
||||||
return errPage(code), code
|
return responsePage(code), code
|
||||||
}
|
}
|
||||||
c.Logger().Debugf("rendering markdown page %q", name)
|
c.Logger().Debugf("rendering markdown page %q", name)
|
||||||
return &Note{Title: name, Content: mdTmplHTML(mdContent)}, http.StatusOK
|
return &Note{Title: name, Content: mdTmplHTML(mdContent)}, http.StatusOK
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -115,29 +114,25 @@ func main() {
|
|||||||
if !legitAccess(c) {
|
if !legitAccess(c) {
|
||||||
code := http.StatusTooManyRequests
|
code := http.StatusTooManyRequests
|
||||||
c.Logger().Errorf("rate limit exceeded for %s", c.Request().RemoteAddr)
|
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 c.FormValue("tos") != "on" {
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if get(vals, "tos") != "on" {
|
|
||||||
code := http.StatusPreconditionFailed
|
code := http.StatusPreconditionFailed
|
||||||
c.Logger().Errorf("POST /note error: %d", code)
|
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 {
|
if 10 > len(text) || len(text) > 50000 {
|
||||||
code := http.StatusBadRequest
|
code := http.StatusBadRequest
|
||||||
c.Logger().Errorf("POST /note error: %d", code)
|
c.Logger().Errorf("POST /note error: %d", code)
|
||||||
return c.Render(code, "Note",
|
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{
|
n := &Note{
|
||||||
ID: id,
|
ID: id,
|
||||||
Text: text,
|
Text: text,
|
||||||
Password: get(vals, "password"),
|
Password: c.FormValue("password"),
|
||||||
}
|
}
|
||||||
n, err = save(c, db, n)
|
n, err = save(c, db, n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -149,7 +144,7 @@ func main() {
|
|||||||
code = http.StatusBadRequest
|
code = http.StatusBadRequest
|
||||||
}
|
}
|
||||||
c.Logger().Errorf("POST /note error: %d", code)
|
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)
|
c.Logger().Debugf("note %q saved", n.ID)
|
||||||
return c.Redirect(http.StatusMovedPermanently, "/"+n.ID)
|
return c.Redirect(http.StatusMovedPermanently, "/"+n.ID)
|
||||||
@@ -158,15 +153,6 @@ func main() {
|
|||||||
e.Logger.Fatal(e.Start(":3000"))
|
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 {
|
func fraudelent(n *Note) bool {
|
||||||
stripped := rexpLink.ReplaceAllString(n.Text, "")
|
stripped := rexpLink.ReplaceAllString(n.Text, "")
|
||||||
l1 := len(n.Text)
|
l1 := len(n.Text)
|
||||||
|
|||||||
+1
-1
@@ -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 {
|
if err := row.Scan(&id, &text, &published, &editedVal, &password, &views); err != nil {
|
||||||
c.Logger().Error(err)
|
c.Logger().Error(err)
|
||||||
code := http.StatusNotFound
|
code := http.StatusNotFound
|
||||||
return errPage(code), code
|
return responsePage(code), code
|
||||||
}
|
}
|
||||||
n := &Note{
|
n := &Note{
|
||||||
ID: id,
|
ID: id,
|
||||||
|
|||||||
Reference in New Issue
Block a user