2025-01-22 00:46:49 +01:00
|
|
|
package webui
|
|
|
|
|
|
|
|
import (
|
2025-02-22 00:35:17 +01:00
|
|
|
"fmt"
|
2025-01-22 00:46:49 +01:00
|
|
|
"html/template"
|
2025-02-22 00:35:17 +01:00
|
|
|
"io"
|
2025-01-22 00:46:49 +01:00
|
|
|
"net/http"
|
2025-02-22 00:35:17 +01:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2025-01-22 00:46:49 +01:00
|
|
|
|
|
|
|
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
|
|
|
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
|
|
|
)
|
|
|
|
|
2025-02-22 00:35:17 +01:00
|
|
|
type StatesPage struct {
|
|
|
|
ActiveTab int
|
|
|
|
Page *Page
|
|
|
|
Path string
|
|
|
|
PathError bool
|
|
|
|
PathDuplicate bool
|
|
|
|
States []model.State
|
|
|
|
}
|
|
|
|
|
2025-01-22 00:46:49 +01:00
|
|
|
var statesTemplates = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/states.html"))
|
|
|
|
|
|
|
|
func handleStatesGET(db *database.DB) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2025-01-27 22:00:51 +01:00
|
|
|
states, err := db.LoadStates()
|
2025-01-22 00:46:49 +01:00
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2025-02-22 00:35:17 +01:00
|
|
|
render(w, statesTemplates, http.StatusOK, StatesPage{
|
2025-01-30 00:19:16 +01:00
|
|
|
Page: makePage(r, &Page{Title: "States", Section: "states"}),
|
2025-01-22 00:46:49 +01:00
|
|
|
States: states,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2025-02-22 00:35:17 +01:00
|
|
|
|
|
|
|
func handleStatesPOST(db *database.DB) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// file upload limit of 20MB
|
|
|
|
if err := r.ParseMultipartForm(20 << 20); err != nil {
|
|
|
|
errorResponse(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file, _, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
statePath := r.FormValue("path")
|
|
|
|
parsedStatePath, err := url.Parse(statePath)
|
|
|
|
if err != nil || path.Clean(parsedStatePath.Path) != statePath || statePath[0] != '/' {
|
|
|
|
render(w, statesTemplates, http.StatusBadRequest, StatesPage{
|
|
|
|
ActiveTab: 1,
|
|
|
|
Page: makePage(r, &Page{Title: "New State", Section: "states"}),
|
|
|
|
Path: statePath,
|
|
|
|
PathError: true,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data, err := io.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusBadRequest, fmt.Errorf("failed to read uploaded file: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fileType := http.DetectContentType(data)
|
|
|
|
if fileType != "text/plain; charset=utf-8" {
|
|
|
|
errorResponse(w, http.StatusBadRequest, fmt.Errorf("invalid file type: expected \"text/plain; charset=utf-8\" but got \"%s\"", fileType))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
account := r.Context().Value(model.AccountContextKey{}).(*model.Account)
|
|
|
|
version, err := db.CreateState(statePath, account.Id, data)
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if version == nil {
|
|
|
|
render(w, statesTemplates, http.StatusBadRequest, StatesPage{
|
|
|
|
ActiveTab: 1,
|
|
|
|
Page: makePage(r, &Page{Title: "New State", Section: "states"}),
|
|
|
|
Path: statePath,
|
|
|
|
PathDuplicate: true,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2025-02-22 13:35:17 +01:00
|
|
|
destination := path.Join("/version", version.Id.String())
|
2025-02-22 00:35:17 +01:00
|
|
|
http.Redirect(w, r, destination, http.StatusFound)
|
|
|
|
})
|
|
|
|
}
|