From 800412d3159ca181e9fd6e912173a2e84dc20524 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L de Mello" Date: Fri, 7 Mar 2025 20:51:43 -0300 Subject: [PATCH] feat: injection of templates into router --- comicverse.go | 1 + router/router.go | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/comicverse.go b/comicverse.go index bf46121..aeb4321 100644 --- a/comicverse.go +++ b/comicverse.go @@ -91,6 +91,7 @@ func (app *app) setup() error { var err error app.handler, err = router.New(router.Config{ + Templates: templates.Templates(), DisableCache: app.developmentMode, StaticFiles: app.staticFiles, diff --git a/router/router.go b/router/router.go index bc06e30..041c602 100644 --- a/router/router.go +++ b/router/router.go @@ -2,6 +2,7 @@ package router import ( "errors" + "html/template" "io/fs" "log/slog" "net/http" @@ -13,6 +14,7 @@ import ( ) type router struct { + templates *template.Template staticFiles fs.FS cache bool @@ -21,6 +23,9 @@ type router struct { } func New(cfg Config) (http.Handler, error) { + if cfg.Templates == nil { + return nil, errors.New("templates are nil") + } if cfg.StaticFiles == nil { return nil, errors.New("static files handler is nil") } @@ -32,6 +37,7 @@ func New(cfg Config) (http.Handler, error) { } r := &router{ + templates: cfg.Templates, staticFiles: cfg.StaticFiles, cache: !cfg.DisableCache, @@ -43,6 +49,7 @@ func New(cfg Config) (http.Handler, error) { } type Config struct { + Templates *template.Template StaticFiles fs.FS DisableCache bool @@ -84,4 +91,8 @@ func (router *router) dashboard(w http.ResponseWriter, r *http.Request) { router.assert.NotNil(r) w.WriteHeader(http.StatusOK) + err := router.templates.ExecuteTemplate(w, "dashboard", nil) + if err != nil { + exception.InternalServerError(err).ServeHTTP(w, r) + } }