diff options
author | Matthew Lemon <y@yulqen.org> | 2024-02-08 15:37:49 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-02-08 15:37:49 +0000 |
commit | 0d7989c353d06397e18e4562d97b4b0cd2160a0e (patch) | |
tree | 1d8bff19395e96e9a0382971a40c8656df31680c | |
parent | 145b0e74247a161c4c22e1aa0c5568604a3a83b6 (diff) |
First appearance of HTML on the home page
-rw-r--r-- | cmd/web/handlers.go | 34 | ||||
-rw-r--r-- | ui/html/base.tmpl.html | 13 |
2 files changed, 46 insertions, 1 deletions
diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index aed4103..7a8f79e 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -1,18 +1,50 @@ package main import ( + "log" "net/http" + "runtime/debug" + "text/template" ) func (app *application) notFound(w http.ResponseWriter) { http.Error(w, http.StatusText(401), 401) } +func (app *application) serverError(w http.ResponseWriter, r *http.Request, err error) { + var ( + method = r.Method + uri = r.URL.RequestURI() + trace = string(debug.Stack()) + ) + + // TODO: reintroduce: app.logger.Error(err.Error(), "method", method, "uri", uri, "trace", trace) + log.Printf("Crash! %s %s %s", method, uri, trace) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) +} + func (app *application) home(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { app.notFound(w) return } - w.Write([]byte("Hello from DED")) + files := []string{ + "./ui/html/base.tmpl.html", + // "./ui/html/pages/home.tmpl.html", + // "./ui/html/partials/nav.tmpl.html", + } + + ts, err := template.ParseFiles(files...) + if err != nil { + app.serverError(w, r, err) // Use the serverError() helper + return + } + + err = ts.ExecuteTemplate(w, "base", nil) + if err != nil { + app.serverError(w, r, err) // Use the serverError() helper + } + + // w.Write([]byte("Hello from DED")) } diff --git a/ui/html/base.tmpl.html b/ui/html/base.tmpl.html new file mode 100644 index 0000000..77ea1e9 --- /dev/null +++ b/ui/html/base.tmpl.html @@ -0,0 +1,13 @@ +{{ define "base" }} +<!DOCTYPE html> +<html> + <head> + <meta charset="UTF-8" /> + <meta name="viewport" content="width=device-width" /> + <title>DED</title> + </head> + <body> + <h1>Hello from DED!</h1> + </body> +</html> +{{ end }} |