diff options
| author | mo khan <mo@mokhan.ca> | 2025-04-21 22:40:14 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-04-21 22:40:14 -0600 |
| commit | 04120f5bfe0934f82745ea6783f0c785bbcd1050 (patch) | |
| tree | 89b7946f120bb91444c204c71cbd5923a67a357a | |
| parent | d674365b49f94b7d8f50411350287113a13509c1 (diff) | |
refactor: extract views package to render html templates
| -rw-r--r-- | app/controllers/dashboard/controller.go | 12 | ||||
| -rw-r--r-- | app/views/dashboard/show.html.tmpl (renamed from app/controllers/dashboard/show.html.tmpl) | 0 | ||||
| -rw-r--r-- | app/views/render.go | 27 |
3 files changed, 29 insertions, 10 deletions
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/views/dashboard/show.html.tmpl index 5293117..5293117 100644 --- a/app/controllers/dashboard/show.html.tmpl +++ b/app/views/dashboard/show.html.tmpl 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 +} |
