aboutsummaryrefslogtreecommitdiff
path: root/handle.go
diff options
context:
space:
mode:
authorthinkpadmaster <a.scerba02@gmail.com>2023-07-21 22:41:35 -0500
committerAlex Scerba <alex@scerba.org>2024-10-29 13:19:57 -0400
commitab653f7555495d674104d1d41ba4a79bc55fd104 (patch)
tree45c4b735ddfe460f1ce62631c157092f2e7ba7eb /handle.go
parentfa4169e37b6d913cb3fd3e79ae1f00459399eccb (diff)
Handle trailing slashes and extraneous URL Paths for /about, /projects, and /blog
Diffstat (limited to 'handle.go')
-rw-r--r--handle.go16
1 files changed, 13 insertions, 3 deletions
diff --git a/handle.go b/handle.go
index 5059c1d..5b84d4e 100644
--- a/handle.go
+++ b/handle.go
@@ -26,9 +26,15 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
}
func (app *application) about(w http.ResponseWriter, r *http.Request) {
- if r.URL.Path == "/about/" {
+ path := strings.Split(r.URL.Path, "/")
+ pathLen := len(path)
+
+ if pathLen == 3 && path[2] == "" {
http.Redirect(w, r, "/about", http.StatusFound)
return
+ } else if pathLen == 3 && path[2] != "" || pathLen > 3 {
+ app.notFound(w)
+ return
}
err := renderTemplate(w, "main/about", nil)
if err != nil {
@@ -48,11 +54,15 @@ func (app *application) aggregate(w http.ResponseWriter, r *http.Request) {
func (app *application) post(w http.ResponseWriter, r *http.Request) {
path := strings.Split(r.URL.Path, "/")
- if r.URL.Path == "/blog" || r.URL.Path == "/projects" {
+ if len(path) > 4 {
+ app.notFound(w)
+ } else if r.URL.Path == "/blog" || r.URL.Path == "/projects" {
app.aggregate(w, r)
} else if path[2] == "" {
- http.Redirect(w, r, strings.TrimSuffix(r.URL.Path, "/"), http.StatusFound)
+ http.Redirect(w, r, "/"+path[1], http.StatusFound)
return
+ } else if len(path) == 4 && path[3] == "" {
+ http.Redirect(w, r, "/"+path[1]+"/"+path[2], http.StatusFound)
} else {
post, err := app.readFile("html" + strings.TrimSuffix(r.URL.Path, "/") + ".tmpl.html")
if err != nil {