diff options
author | Alex Scerba <alex@scerba.org> | 2024-09-27 18:56:29 -0400 |
---|---|---|
committer | Alex Scerba <alex@scerba.org> | 2024-09-27 18:56:29 -0400 |
commit | 2782bafcfdb69ef7b69a51a717a6bd35095e9369 (patch) | |
tree | 32d69f68fe2dbc6b9cc5a5ac8ff970d79f348156 /cmd/http/handle.go | |
parent | 1208c2e7e7e79cfe122f8d5f38160a0611cc9dfe (diff) |
Add base files
Diffstat (limited to 'cmd/http/handle.go')
-rw-r--r-- | cmd/http/handle.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/cmd/http/handle.go b/cmd/http/handle.go new file mode 100644 index 0000000..9c1f62a --- /dev/null +++ b/cmd/http/handle.go @@ -0,0 +1,65 @@ +package main + +import ( + "net/http" + "strings" +) + +func (app *application) home(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] != "" { + app.notFound(w) + } else { + p, err := app.aggregate("html/projects") + if err != nil { + app.serverError(w, err) + } + + err = renderTemplate(w, "index", p) + if err != nil { + app.serverError(w, err) + } + } +} + +func (app *application) post(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + + path := strings.Split(r.URL.Path, "/") + 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) + } + } +} + +func (app *application) about(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" { + app.notFound(w) + } else { + err := renderTemplate(w, "about", nil) + if err != nil { + app.serverError(w, err) + } + } +} |