From f13dc0fe0f9908c593eed5611d12e51390db07fb Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L de Mello" Date: Wed, 5 Mar 2025 13:45:19 -0300 Subject: [PATCH] feat: templates prototype --- router/router.go | 11 +++++++++-- templates/index.html | 1 + templates/templates.go | 18 ++++++++++++++++++ templates/test/test.html | 12 ++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 templates/templates.go create mode 100644 templates/test/test.html diff --git a/router/router.go b/router/router.go index 9841ee0..406ff11 100644 --- a/router/router.go +++ b/router/router.go @@ -5,6 +5,7 @@ import ( "log/slog" "net/http" + "forge.capytal.company/capytalcode/project-comicverse/templates" "forge.capytal.company/loreddev/x/smalltrip" "forge.capytal.company/loreddev/x/smalltrip/exceptions" "forge.capytal.company/loreddev/x/smalltrip/middleware" @@ -24,9 +25,12 @@ func New(assertions tinyssert.Assertions, log *slog.Logger, dev bool) http.Handl r.Use(exceptions.PanicMiddleware()) r.Use(exceptions.Middleware()) - r.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { + r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - _, _ = w.Write([]byte("Hello world")) + err := templates.Templates().ExecuteTemplate(w, "test.html", nil) + if err != nil { + exceptions.InternalServerError(err).ServeHTTP(w, r) + } }) r.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { exceptions.InternalServerError(errors.New("TEST ERROR"), @@ -39,3 +43,6 @@ func New(assertions tinyssert.Assertions, log *slog.Logger, dev bool) http.Handl return r } + +func dashboard(w http.ResponseWriter, r *http.Request) { +} diff --git a/templates/index.html b/templates/index.html index 8fef075..ec165bd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,3 +1,4 @@ + diff --git a/templates/templates.go b/templates/templates.go new file mode 100644 index 0000000..7bb6723 --- /dev/null +++ b/templates/templates.go @@ -0,0 +1,18 @@ +package templates + +import ( + "embed" + "html/template" +) + +//go:embed *.html test/*.html +var embedded embed.FS + +var temps = template.Must(template.ParseFS(embedded, + "*.html", + "test/*.html", +)) + +func Templates() *template.Template { + return temps +} diff --git a/templates/test/test.html b/templates/test/test.html new file mode 100644 index 0000000..459a14f --- /dev/null +++ b/templates/test/test.html @@ -0,0 +1,12 @@ + + + + + + + + +

Hello, world 2

+ + +