diff options
author | thinkpadmaster <a.scerba02@gmail.com> | 2023-07-13 23:34:25 -0500 |
---|---|---|
committer | thinkpadmaster <a.scerba02@gmail.com> | 2023-07-13 23:34:25 -0500 |
commit | e864807341990f5c72e198d96740983bf7671584 (patch) | |
tree | d0c9dd12da81272e7bd30cae4643bacdc030d47b /handle.go | |
parent | 8106f531198952489de53c93dd3c955e8ea4d78f (diff) |
Move to multifile system and prepare for new file format
Diffstat (limited to 'handle.go')
-rw-r--r-- | handle.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/handle.go b/handle.go new file mode 100644 index 0000000..20fa259 --- /dev/null +++ b/handle.go @@ -0,0 +1,66 @@ +package main + +import ( + "net/http" + "strings" +) + +func (app *application) home(w http.ResponseWriter, r *http.Request) { + path := strings.Split(r.URL.Path, "/") + if path[1] != "" { + app.notFound(w) + return + } else { + p, err := app.loadPosts("html/projects", 3) + if err != nil { + app.serverError(w, err) + return + } + + err = renderTemplate(w, "main/index", p) + if err != nil { + app.serverError(w, err) + return + } + } +} + +func (app *application) about(w http.ResponseWriter, r *http.Request) { + err := renderTemplate(w, "main/about", nil) + if err != nil { + app.serverError(w, err) + return + } +} + +func (app *application) aggregate(w http.ResponseWriter, r *http.Request) { + p, err := app.loadPosts("html"+strings.TrimSuffix(r.URL.Path, "/"), -1) + if err != nil { + app.notFound(w) + } + + renderTemplate(w, "main/"+strings.TrimPrefix(strings.TrimSuffix(r.URL.Path, "/"), "/"), p) +} + +func (app *application) post(w http.ResponseWriter, r *http.Request) { + path := strings.Split(r.URL.Path, "/") + if path[2] == "" { + app.aggregate(w, r) + } 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) + return + } + } +} |