diff options
author | Jordan <me@jordan.im> | 2021-12-18 17:11:08 -0700 |
---|---|---|
committer | Jordan <me@jordan.im> | 2021-12-18 17:11:08 -0700 |
commit | 69940e02717e0b9cee9e2453b0ddb88c6dcf786e (patch) | |
tree | 45dd5ead89b82c16f0d57b95e3b279a3826e70be | |
parent | b2d0b6a1963e051de7652958da68f6c7fda3d5e9 (diff) | |
download | keep-69940e02717e0b9cee9e2453b0ddb88c6dcf786e.tar.gz keep-69940e02717e0b9cee9e2453b0ddb88c6dcf786e.zip |
web: return 404 on non-existent paths, housekeeping
-rw-r--r-- | web.go | 21 |
1 files changed, 9 insertions, 12 deletions
@@ -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 { |