summaryrefslogtreecommitdiffstats
path: root/web/handler/index.go
blob: 8b3d43c4f2b06dedba4f26d3af9952870f5c7e6f (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
package handler

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

	"git.leonardobishop.net/instancer/pkg/registry"
	"git.leonardobishop.net/instancer/pkg/session"
)

func GetIndex(tmpl *template.Template, registryClient *registry.RegistryClient) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		challenges, err := registryClient.ListRepositories()
		if err != nil {
			slog.Error("could not list repositories", "cause", err)
			tmpl.ExecuteTemplate(w, "problem.html", struct {
				Error      string
				ShowLogout bool
			}{
				Error:      "Error occured fetching available challenges. Please try again.",
				ShowLogout: false,
			})
			return
		}

		session := r.Context().Value("session").(*session.UserSession)

		tmpl.ExecuteTemplate(w, "index.html", struct {
			Challenges []string
			Name       string
			Team       string
		}{
			Challenges: challenges,
			Name:       session.Name,
			Team:       session.TeamName,
		})
	}
}