From 9de239a9badbbc075f1cc8079a6435f0780e5d26 Mon Sep 17 00:00:00 2001 From: Samuel Johnson Date: Mon, 10 Feb 2025 02:19:21 -0500 Subject: But it was a beginning --- cmd/web/handlers/blog.go | 32 ++++++++++++++++++++++++++++++++ cmd/web/handlers/context.go | 5 +++++ cmd/web/handlers/routes.go | 14 ++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 cmd/web/handlers/blog.go create mode 100644 cmd/web/handlers/context.go create mode 100644 cmd/web/handlers/routes.go (limited to 'cmd/web/handlers') diff --git a/cmd/web/handlers/blog.go b/cmd/web/handlers/blog.go new file mode 100644 index 0000000..5a874f4 --- /dev/null +++ b/cmd/web/handlers/blog.go @@ -0,0 +1,32 @@ +package handlers + +import ( + "html/template" + "log" + "net/http" +) + +func (ctx *HandlerContext) blogIndex(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.NotFound(w, r) + return + } + + files := []string{ + "./ui/html/base.tmpl.html", + "./ui/html/pages/index.tmpl.html", + } + + compiled, err := template.ParseFiles(files...) + if err != nil { + log.Println(err.Error()) + http.Error(w, "Internal Server Error", 500) + return + } + + err = compiled.ExecuteTemplate(w, "base", ctx) + if err != nil { + log.Println(err.Error()) + http.Error(w, "Internal Server Error", 500) + } +} diff --git a/cmd/web/handlers/context.go b/cmd/web/handlers/context.go new file mode 100644 index 0000000..7df2915 --- /dev/null +++ b/cmd/web/handlers/context.go @@ -0,0 +1,5 @@ +package handlers + +type HandlerContext struct { + Webmaster string +} diff --git a/cmd/web/handlers/routes.go b/cmd/web/handlers/routes.go new file mode 100644 index 0000000..5a57440 --- /dev/null +++ b/cmd/web/handlers/routes.go @@ -0,0 +1,14 @@ +package handlers + +import ( + "github.com/gorilla/mux" +) + +func RegisterEndpoints(webmaster string) *mux.Router { + ctx := HandlerContext{webmaster} + + blogRouter := mux.NewRouter() + blogRouter.HandleFunc("/", ctx.blogIndex) + + return blogRouter +} -- cgit v1.2.3