summaryrefslogtreecommitdiff
path: root/pkg/webui/state.go
blob: c7a6aaf8cd7b8c6e0b806e5d02ee5ee1e539f782 (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
package webui

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

	"git.adyxax.org/adyxax/tfstated/pkg/database"
	"git.adyxax.org/adyxax/tfstated/pkg/model"
)

var stateTemplate = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/state.html"))

func handleStateGET(db *database.DB) http.Handler {
	type StatesData struct {
		Page
		State     *model.State
		Usernames map[int]string
		Versions  []model.Version
	}
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		stateIdStr := r.PathValue("id")
		stateId, err := strconv.Atoi(stateIdStr)
		if err != nil {
			errorResponse(w, http.StatusBadRequest, err)
			return
		}
		state, err := db.LoadStateById(stateId)
		if err != nil {
			errorResponse(w, http.StatusInternalServerError, err)
			return
		}
		versions, err := db.LoadVersionsByState(state)
		if err != nil {
			errorResponse(w, http.StatusInternalServerError, err)
			return
		}
		usernames, err := db.LoadAccountUsernames()
		if err != nil {
			errorResponse(w, http.StatusInternalServerError, err)
			return
		}
		render(w, stateTemplate, http.StatusOK, StatesData{
			Page: Page{
				Precedent: "/states",
				Section:   "states",
				Title:     state.Path,
			},
			State:     state,
			Usernames: usernames,
			Versions:  versions,
		})
	})
}