aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan <me@jordan.im>2021-12-18 17:11:08 -0700
committerJordan <me@jordan.im>2021-12-18 17:11:08 -0700
commit69940e02717e0b9cee9e2453b0ddb88c6dcf786e (patch)
tree45dd5ead89b82c16f0d57b95e3b279a3826e70be
parentb2d0b6a1963e051de7652958da68f6c7fda3d5e9 (diff)
downloadkeep-69940e02717e0b9cee9e2453b0ddb88c6dcf786e.tar.gz
keep-69940e02717e0b9cee9e2453b0ddb88c6dcf786e.zip
web: return 404 on non-existent paths, housekeeping
-rw-r--r--web.go21
1 files changed, 9 insertions, 12 deletions
diff --git a/web.go b/web.go
index c3c8d67..e77fe39 100644
--- a/web.go
+++ b/web.go
@@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"strconv"
+ "strings"
)
type Resp struct {
@@ -23,7 +24,6 @@ var funcMap = template.FuncMap{
"add": add,
"minus": minus,
"setQuery": setQuery,
- "getRoot": getRoot,
"intToStr": intToStr,
}
@@ -104,7 +104,7 @@ const i = `
{{- if gt .Offset 0 -}}
<a href="{{ setQuery .URL "offset" (intToStr (minus .Offset 100)) }}">Previous</a>
{{- end -}}
- <a href="{{ getRoot .URL }}">Home</a>
+ <a href="./">Home</a>
{{- if ge (len .Entries) 100 -}}
<a href="{{ setQuery .URL "offset" (intToStr (add .Offset 100)) }}">Next</a>
{{- end -}}
@@ -133,7 +133,7 @@ const i = `
{{- if gt .Offset 0 -}}
<a href="{{ setQuery .URL "offset" (intToStr (minus .Offset 100)) }}">Previous</a>
{{- end -}}
- <a href="{{ getRoot .URL }}">Home</a>
+ <a href="./">Home</a>
{{- if ge (len .Entries) 100 -}}
<a href="{{ setQuery .URL "offset" (intToStr (add .Offset 100)) }}">Next</a>
{{- end -}}
@@ -165,19 +165,16 @@ func setQuery(urlStr string, query string, value string) string {
q := u.Query()
q.Set(query, value)
u.RawQuery = q.Encode()
- return u.String()
-}
-
-func getRoot(urlStr string) string {
-
- u, _ := url.Parse(urlStr)
- u.RawQuery = ""
- u.Fragment = ""
- return u.String()
+ return strings.TrimLeft(u.String(), "/")
}
func (db *SqliteDB) IndexHandler(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/" {
+ http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+ return
+ }
+
resp := Resp{}
resp.Stats, resp.Err = db.Stats()
if resp.Err != nil {