feat(webui): implement states list

This commit is contained in:
Julien Dessaux 2025-01-22 00:46:49 +01:00
parent 7885b19b54
commit 09885ef1e4
Signed by: adyxax
GPG key ID: F92E51B86E07177E
10 changed files with 117 additions and 12 deletions

View file

@ -9,12 +9,20 @@
<title>tfstated</title>
</head>
<body>
<header>
<header class="container">
<nav>
<ul>
<li><a href="/"><strong>TFSTATED</strong></a></li>
</ul>
<ul>
<li><a href="/states">States</a></li>
</ul>
</nav>
</header>
<main class="container">
{{ template "main" . }}
</main>
<footer>
<footer class="container">
</footer>
</body>
</html>

View file

@ -1,3 +0,0 @@
{{ define "main" }}
<h1>TODO</h1>
{{ end }}

View file

@ -0,0 +1,23 @@
{{ define "main" }}
<h1>States</h1>
<table class="striped">
<thead>
<tr>
<th scope="col">Path</th>
<th scope="col">Created</th>
<th scope="col">Updated</th>
<th scope="col">Locked</th>
</tr>
</thead>
<tbody>
{{ range .States }}
<tr>
<th scope="row">{{ .Path }}</th>
<td>{{ .Created }}</td>
<td>{{ .Updated }}</td>
<td>{{ .Lock }}</td>
</tr>
{{ end }}
</tbody>
</table>
{{ end }}

View file

@ -1,16 +1,16 @@
package webui
import (
"html/template"
"fmt"
"net/http"
)
var indexTemplates = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/index.html"))
func handleIndexGET() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store, no-cache")
render(w, indexTemplates, http.StatusOK, nil)
if r.URL.Path == "/" {
http.Redirect(w, r, "/states", http.StatusFound)
} else {
errorResponse(w, http.StatusNotFound, fmt.Errorf("Page not found"))
}
})
}

View file

@ -24,7 +24,7 @@ func handleLoginGET() http.Handler {
session := r.Context().Value(model.SessionContextKey{})
if session != nil {
http.Redirect(w, r, "/", http.StatusFound)
http.Redirect(w, r, "/states", http.StatusFound)
return
}

View file

@ -16,6 +16,7 @@ func addRoutes(
mux.Handle("GET /login", requireSession(handleLoginGET()))
mux.Handle("POST /login", requireSession(handleLoginPOST(db)))
mux.Handle("GET /logout", requireLogin(handleLogoutGET(db)))
mux.Handle("GET /states", requireLogin(handleStatesGET(db)))
mux.Handle("GET /static/", cache(http.FileServer(http.FS(staticFS))))
mux.Handle("GET /", requireLogin(handleIndexGET()))
}

29
pkg/webui/states.go Normal file
View file

@ -0,0 +1,29 @@
package webui
import (
"html/template"
"net/http"
"git.adyxax.org/adyxax/tfstated/pkg/database"
"git.adyxax.org/adyxax/tfstated/pkg/model"
)
var statesTemplates = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/states.html"))
func handleStatesGET(db *database.DB) http.Handler {
type StatesData struct {
States []model.State
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store, no-cache")
states, err := db.LoadStatesByPath()
if err != nil {
errorResponse(w, http.StatusInternalServerError, err)
return
}
render(w, statesTemplates, http.StatusOK, StatesData{
States: states,
})
})
}