From 04120f5bfe0934f82745ea6783f0c785bbcd1050 Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 21 Apr 2025 22:40:14 -0600 Subject: refactor: extract views package to render html templates --- app/controllers/dashboard/controller.go | 12 ++---------- app/controllers/dashboard/show.html.tmpl | 27 --------------------------- app/views/dashboard/show.html.tmpl | 27 +++++++++++++++++++++++++++ app/views/render.go | 27 +++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 37 deletions(-) delete mode 100644 app/controllers/dashboard/show.html.tmpl create mode 100644 app/views/dashboard/show.html.tmpl create mode 100644 app/views/render.go (limited to 'app') diff --git a/app/controllers/dashboard/controller.go b/app/controllers/dashboard/controller.go index 26b182f..106666b 100644 --- a/app/controllers/dashboard/controller.go +++ b/app/controllers/dashboard/controller.go @@ -1,12 +1,11 @@ package dashboard import ( - "html/template" - "io/ioutil" "net/http" "github.com/xlgmokha/x/pkg/log" "github.com/xlgmokha/x/pkg/x" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/views" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key" ) @@ -31,15 +30,8 @@ func (c *Controller) Show(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Header().Add("Content-Type", "text/html") - b, err := ioutil.ReadFile("show.html.tmpl") - if err != nil { - log.WithFields(r.Context(), log.Fields{"error": err}) - w.WriteHeader(http.StatusInternalServerError) - return - } - t := template.Must(template.New("show").Parse(string(b))) - if err := t.Execute(w, &ViewDashboardDTO{ + if err := views.Render(w, "dashboard/show", &ViewDashboardDTO{ CurrentUser: currentUser, Sparkles: []string{}, }); err != nil { diff --git a/app/controllers/dashboard/show.html.tmpl b/app/controllers/dashboard/show.html.tmpl deleted file mode 100644 index 5293117..0000000 --- a/app/controllers/dashboard/show.html.tmpl +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - SparkleLab - - - -
- - {{range .Sparkles}} -
{{ . }}
- {{else}} -
No Sparkles
- {{end}} -
- - diff --git a/app/views/dashboard/show.html.tmpl b/app/views/dashboard/show.html.tmpl new file mode 100644 index 0000000..5293117 --- /dev/null +++ b/app/views/dashboard/show.html.tmpl @@ -0,0 +1,27 @@ + + + + + + + SparkleLab + + + +
+ + {{range .Sparkles}} +
{{ . }}
+ {{else}} +
No Sparkles
+ {{end}} +
+ + diff --git a/app/views/render.go b/app/views/render.go new file mode 100644 index 0000000..d06a4f7 --- /dev/null +++ b/app/views/render.go @@ -0,0 +1,27 @@ +package views + +import ( + "embed" + "html/template" + "io" +) + +//go:embed **/*.html.tmpl +var fs embed.FS + +func Get(path string) (*template.Template, error) { + return template.ParseFS(fs, path+".html.tmpl") +} + +func Render[T any](w io.Writer, path string, data T) error { + tmpl, err := Get(path) + if err != nil { + return err + } + + if err := tmpl.Execute(w, data); err != nil { + return err + } + + return nil +} -- cgit v1.2.3