summaryrefslogtreecommitdiff
path: root/pkg/webui/state.go
diff options
context:
space:
mode:
authorJulien Dessaux2025-01-27 22:00:51 +0100
committerJulien Dessaux2025-01-27 22:00:51 +0100
commit26e10a9399bd6185741d0912ffa54c807ffef671 (patch)
tree0cf9e81c6880b07d9c029e3abd6ee03b6a2acdb1 /pkg/webui/state.go
parentchore(webui): remove redundant set of the Cache-Control header in state get h... (diff)
downloadtfstated-26e10a9399bd6185741d0912ffa54c807ffef671.tar.gz
tfstated-26e10a9399bd6185741d0912ffa54c807ffef671.tar.bz2
tfstated-26e10a9399bd6185741d0912ffa54c807ffef671.zip
feat(webui): implement state versions list
Diffstat (limited to 'pkg/webui/state.go')
-rw-r--r--pkg/webui/state.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/pkg/webui/state.go b/pkg/webui/state.go
new file mode 100644
index 0000000..c7a6aaf
--- /dev/null
+++ b/pkg/webui/state.go
@@ -0,0 +1,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,
+ })
+ })
+}