diff options
| author | mo khan <mo@mokhan.ca> | 2025-04-17 16:20:27 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-04-17 16:20:27 -0600 |
| commit | 49a8ef7441ac9355a7a8e4890f0a1c9004571f37 (patch) | |
| tree | d939c2465bb31113468ef3e9562befc291495019 /app/controllers/dashboard/controller.go | |
| parent | 1a1da99c2bde90bfd0cb78ddca5276deed10bdc5 (diff) | |
refactor: move html template to a separate file
Diffstat (limited to 'app/controllers/dashboard/controller.go')
| -rw-r--r-- | app/controllers/dashboard/controller.go | 51 |
1 files changed, 14 insertions, 37 deletions
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 = ` -<!doctype html> -<html lang="en"> - <head> - <meta charset="utf-8"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <meta name="color-scheme" content="light dark"> - <title>SparkleLab - {{.Title}}</title> - <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"> - </head> - <body> - <main class="container"> - <nav> - <ul> - <li><strong>SparkleLab</strong></li> - </ul> - <ul> - <li><a href="/session/new">Login</a></li> - </ul> - </nav> - {{range .Sparkles}} - <div>{{ . }}</div> - {{else}} - <div><strong>No Sparkles</strong></div> - {{end}} - </main> - </body> -</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 := 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) } |
