aboutsummaryrefslogtreecommitdiff
path: root/cmd/http/handle.go
diff options
context:
space:
mode:
authorAlex <alex@scerba.org>2024-05-04 17:28:37 -0400
committerAlex Scerba <alex@scerba.org>2024-08-15 22:54:25 -0500
commit811c9bb2f7358ff094fe13deb6d961088baa2d8f (patch)
tree694010fbc71c9ff67cee28f6da1447290e627afe /cmd/http/handle.go
parentd9fc74778ef86b02f0a743821263db8873417294 (diff)
Add Go webserver.
Diffstat (limited to 'cmd/http/handle.go')
-rw-r--r--cmd/http/handle.go103
1 files changed, 103 insertions, 0 deletions
diff --git a/cmd/http/handle.go b/cmd/http/handle.go
new file mode 100644
index 0000000..a8b1da1
--- /dev/null
+++ b/cmd/http/handle.go
@@ -0,0 +1,103 @@
+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", nil)
+ if err != nil {
+ app.serverError(w, err)
+ }
+ }
+}
+
+func (app *application) faq(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)
+ }
+ }
+}
+
+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)
+ }
+ }
+}
+
+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" {
+ app.notFound(w)
+ } else {
+ err := renderTemplate(w, "gallery", nil)
+ 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)
+ 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)
+ }
+ }
+ */
+}