summaryrefslogtreecommitdiff
path: root/app/controllers/dashboard/controller.go
blob: d805027a6f0d0c8199fc65a04c4e4963bccd06b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package dashboard

import (
	"html/template"
	"net/http"

	"github.com/xlgmokha/x/pkg/context"
	"github.com/xlgmokha/x/pkg/x"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
)

var CurrentUserKey context.Key[*domain.User] = context.Key[*domain.User]("current_user")

type Controller struct {
}

func New() *Controller {
	return &Controller{}
}

func (c *Controller) MountTo(mux *http.ServeMux) {
	mux.HandleFunc("GET /dashboard", c.Show)
}

func (c *Controller) Show(w http.ResponseWriter, r *http.Request) {
	currentUser := CurrentUserKey.From(r.Context())

	if x.IsZero(currentUser) {
		http.Redirect(w, r, "/", http.StatusFound)
		return
	}

	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>`

	t := x.Must(template.New("show").Parse(tpl))
	data := struct {
		Title    string
		Sparkles []string
	}{
		Title:    "SparkleLab",
		Sparkles: []string{},
	}
	t.Execute(w, data)
}