aboutsummaryrefslogtreecommitdiff
path: root/cmd/http/handle.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/http/handle.go')
-rw-r--r--cmd/http/handle.go73
1 files changed, 52 insertions, 21 deletions
diff --git a/cmd/http/handle.go b/cmd/http/handle.go
index ce0e8c0..f5ee67a 100644
--- a/cmd/http/handle.go
+++ b/cmd/http/handle.go
@@ -17,7 +17,7 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
app.serverError(w, err)
} */
- err := renderTemplate(w, "index", nil)
+ err := renderPage(w, "index")
if err != nil {
app.serverError(w, err)
}
@@ -29,51 +29,82 @@ func (app *application) page(w http.ResponseWriter, r *http.Request) {
path := strings.Split(r.URL.Path, "/")
- /* Redirect paths like /about/sth/oeusth to /about */
+ // 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)
+ err := renderPage(w, path[1])
if err != nil {
app.serverError(w, err)
}
}
-/*
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] != "blog" {
- app.notFound(w)
- } else {
- err := renderTemplate(w, "blog", nil)
+
+ // Setup redirects for trailing / and extraneous path elements
+ if len(path) > 3 {
+ // Redirect to /blog/article
+ http.Redirect(w, r, "/"+path[1]+"/"+path[2], http.StatusFound)
+ } else if len(path) == 3 && path[2] == "" {
+ // Redirect to /blog
+ http.Redirect(w, r, "/"+path[1], http.StatusFound)
+ } else if len(path) == 2 {
+ p, err := app.aggregate("html/blog")
if err != nil {
app.serverError(w, err)
+ return
+ }
+ a, err := app.aggregate("html/archive")
+ if err != nil {
+ app.serverError(w, err)
+ return
}
- }
- 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")
+ err = renderBlog(w, "blog2", p, a)
if err != nil {
+ app.serverError(w, err)
+ return
+ }
+ } else if len(path) == 3 {
+ if !app.fileExists("html/" + path[1] + "/" + path[2] + ".tmpl.html") {
app.notFound(w)
return
}
+ post := app.parseFileName(path[2])
- var posts []*Post
- posts = append(posts, post)
- p := &Posts{Contents: posts}
-
- err = renderTemplate(w, path[1]+"/"+path[2], p)
+ err := renderPost(w, path[2], post)
if err != nil {
app.serverError(w, err)
}
}
+}
+
+func (app *application) archive(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
+
+ path := strings.Split(r.URL.Path, "/")
+
+ // Setup redirects for trailing / and extraneous path elements
+ if len(path) > 3 {
+ // Redirect to /archive/article
+ http.Redirect(w, r, "/"+path[1]+"/"+path[2], http.StatusFound)
+ } else if len(path) == 3 && path[2] == "" {
+ // Redirect to /blog
+ http.Redirect(w, r, "/blog", http.StatusFound)
+ } else if len(path) == 3 {
+ if !app.fileExists("html/" + path[1] + "/" + path[2] + ".tmpl.html") {
+ app.notFound(w)
+ return
+ }
+ post := app.parseFileName(path[2])
+ err := renderArchivePost(w, path[2], post)
+ if err != nil {
+ app.serverError(w, err)
+ }
+ }
}
-*/