aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Scerba <alex@scerba.org>2024-10-28 11:38:15 -0400
committerAlex Scerba <alex@scerba.org>2024-10-28 11:38:15 -0400
commitb3adf57af3c73988f1882e52cd8a9cb93df2fd72 (patch)
tree3e88269076e69df1d12fd6ac6532c2669917f2cc
parentb1aeedd49129a4a9e9d20e58a0b8e563b27f284c (diff)
Switch to generic "page" function
-rw-r--r--cmd/http/handle.go74
1 files changed, 25 insertions, 49 deletions
diff --git a/cmd/http/handle.go b/cmd/http/handle.go
index a8b1da1..ce0e8c0 100644
--- a/cmd/http/handle.go
+++ b/cmd/http/handle.go
@@ -24,80 +24,56 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
}
}
-func (app *application) faq(w http.ResponseWriter, r *http.Request) {
+func (app *application) page(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
path := strings.Split(r.URL.Path, "/")
- if path[1] != "faq" {
- app.notFound(w)
- } else {
- err := renderTemplate(w, "faq", nil)
- if err != nil {
- app.serverError(w, err)
- }
+
+ /* Redirect paths like /about/sth/oeusth to /about */
+ if len(path) > 2 {
+ http.Redirect(w, r, "/"+path[1], http.StatusFound)
+ }
+
+ err := renderTemplate(w, path[1], nil)
+ if err != nil {
+ app.serverError(w, err)
}
}
-func (app *application) about(w http.ResponseWriter, r *http.Request) {
+/*
+func (app *application) blog(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
path := strings.Split(r.URL.Path, "/")
- if path[1] != "about" {
+ if path[1] != "blog" {
app.notFound(w)
} else {
- err := renderTemplate(w, "about", nil)
+ err := renderTemplate(w, "blog", nil)
if err != nil {
app.serverError(w, err)
}
}
-}
-
-func (app *application) gallery(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-Type", "text/html; charset=utf-8")
- path := strings.Split(r.URL.Path, "/")
- if path[1] != "gallery" {
+ if len(path) > 4 {
app.notFound(w)
+ } else if len(path) == 4 && path[3] == "" {
+ http.Redirect(w, r, "/"+path[1]+"/"+path[2], http.StatusFound)
} else {
- err := renderTemplate(w, "gallery", nil)
+ post, err := app.readFile("html" + strings.TrimSuffix(r.URL.Path, "/") + ".tmpl.html")
if err != nil {
- app.serverError(w, err)
+ app.notFound(w)
+ return
}
- }
-}
-func (app *application) blog(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-Type", "text/html; charset=utf-8")
+ var posts []*Post
+ posts = append(posts, post)
+ p := &Posts{Contents: posts}
- path := strings.Split(r.URL.Path, "/")
- if path[1] != "blog" {
- app.notFound(w)
- } else {
- err := renderTemplate(w, "blog", nil)
+ err = renderTemplate(w, path[1]+"/"+path[2], p)
if err != nil {
app.serverError(w, err)
}
}
- /*
- if len(path) > 4 {
- app.notFound(w)
- } 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 {
- app.notFound(w)
- return
- }
-
- var posts []*Post
- posts = append(posts, post)
- p := &Posts{Contents: posts}
- err = renderTemplate(w, path[1]+"/"+path[2], p)
- if err != nil {
- app.serverError(w, err)
- }
- }
- */
}
+*/