From 49a8ef7441ac9355a7a8e4890f0a1c9004571f37 Mon Sep 17 00:00:00 2001 From: mo khan Date: Thu, 17 Apr 2025 16:20:27 -0600 Subject: refactor: move html template to a separate file --- app/controllers/dashboard/controller.go | 51 +++++++++----------------------- app/controllers/dashboard/dto.go | 9 ++++++ app/controllers/dashboard/show.html.tmpl | 27 +++++++++++++++++ 3 files changed, 50 insertions(+), 37 deletions(-) create mode 100644 app/controllers/dashboard/dto.go create mode 100644 app/controllers/dashboard/show.html.tmpl diff --git a/app/controllers/dashboard/controller.go b/app/controllers/dashboard/controller.go index b0477ba..26b182f 100644 --- a/app/controllers/dashboard/controller.go +++ b/app/controllers/dashboard/controller.go @@ -2,10 +2,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/pkg/domain" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key" ) @@ -30,44 +31,20 @@ func (c *Controller) Show(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Header().Add("Content-Type", "text/html") - const tpl = ` - - - - - - - SparkleLab - {{.Title}} - - - -
- - {{range .Sparkles}} -
{{ . }}
- {{else}} -
No Sparkles
- {{end}} -
- -` + b, err := ioutil.ReadFile("show.html.tmpl") + if err != nil { + log.WithFields(r.Context(), log.Fields{"error": err}) + w.WriteHeader(http.StatusInternalServerError) + return + } - t := x.Must(template.New("show").Parse(tpl)) - data := struct { - Title string - CurrentUser *domain.User - Sparkles []string - }{ - Title: "SparkleLab", + t := template.Must(template.New("show").Parse(string(b))) + if err := t.Execute(w, &ViewDashboardDTO{ CurrentUser: currentUser, Sparkles: []string{}, + }); err != nil { + log.WithFields(r.Context(), log.Fields{"error": err}) + w.WriteHeader(http.StatusInternalServerError) + return } - t.Execute(w, data) } diff --git a/app/controllers/dashboard/dto.go b/app/controllers/dashboard/dto.go new file mode 100644 index 0000000..4243adc --- /dev/null +++ b/app/controllers/dashboard/dto.go @@ -0,0 +1,9 @@ +package dashboard + +import "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain" + +type ViewDashboardDTO struct { + CurrentUser *domain.User + Sparkles []string + Title string +} diff --git a/app/controllers/dashboard/show.html.tmpl b/app/controllers/dashboard/show.html.tmpl new file mode 100644 index 0000000..5293117 --- /dev/null +++ b/app/controllers/dashboard/show.html.tmpl @@ -0,0 +1,27 @@ + + + + + + + SparkleLab + + + +
+ + {{range .Sparkles}} +
{{ . }}
+ {{else}} +
No Sparkles
+ {{end}} +
+ + -- cgit v1.2.3