From fcc220612495a21067f9043396ba63062537ad63 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 4 Feb 2025 23:34:26 +0100 Subject: feat(webui): add state creation page --- pkg/webui/html/states.html | 6 +++ pkg/webui/html/states_new.html | 33 +++++++++++++++++ pkg/webui/routes.go | 2 + pkg/webui/states_new.go | 84 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 pkg/webui/html/states_new.html create mode 100644 pkg/webui/states_new.go (limited to 'pkg/webui') diff --git a/pkg/webui/html/states.html b/pkg/webui/html/states.html index 787ac73..37d80cf 100644 --- a/pkg/webui/html/states.html +++ b/pkg/webui/html/states.html @@ -1,5 +1,11 @@ {{ define "main" }}
+ + + diff --git a/pkg/webui/html/states_new.html b/pkg/webui/html/states_new.html new file mode 100644 index 0000000..68facc7 --- /dev/null +++ b/pkg/webui/html/states_new.html @@ -0,0 +1,33 @@ +{{ define "main" }} +
+
+
+
+ + + {{ if .PathDuplicate }} + This path already exist + {{ else if .PathError }} + Invalid path + {{ else }} + Valid URL path beginning with a / + {{ end }} +
+
+ + + + JSON state file +
+ +
+ +
+{{ end }} diff --git a/pkg/webui/routes.go b/pkg/webui/routes.go index e84f33a..7ee7841 100644 --- a/pkg/webui/routes.go +++ b/pkg/webui/routes.go @@ -19,6 +19,8 @@ func addRoutes( mux.Handle("GET /settings", requireLogin(handleSettingsGET(db))) mux.Handle("POST /settings", requireLogin(handleSettingsPOST(db))) mux.Handle("GET /states", requireLogin(handleStatesGET(db))) + mux.Handle("GET /states/new", requireLogin(handleStatesNewGET(db))) + mux.Handle("POST /states/new", requireLogin(handleStatesNewPOST(db))) mux.Handle("GET /state/{id}", requireLogin(handleStateGET(db))) mux.Handle("GET /static/", cache(http.FileServer(http.FS(staticFS)))) mux.Handle("GET /version/{id}", requireLogin(handleVersionGET(db))) diff --git a/pkg/webui/states_new.go b/pkg/webui/states_new.go new file mode 100644 index 0000000..8551191 --- /dev/null +++ b/pkg/webui/states_new.go @@ -0,0 +1,84 @@ +package webui + +import ( + "fmt" + "html/template" + "io" + "net/http" + "net/url" + "path" + "strconv" + + "git.adyxax.org/adyxax/tfstated/pkg/database" + "git.adyxax.org/adyxax/tfstated/pkg/model" +) + +type StatesNewPage struct { + Page *Page + fileError bool + Path string + PathDuplicate bool + PathError bool +} + +var statesNewTemplates = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/states_new.html")) + +func handleStatesNewGET(db *database.DB) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + render(w, statesNewTemplates, http.StatusOK, StatesNewPage{ + Page: makePage(r, &Page{Title: "New State", Section: "states"}), + }) + }) +} + +func handleStatesNewPOST(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, statesNewTemplates, http.StatusBadRequest, StatesNewPage{ + 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, statesNewTemplates, http.StatusBadRequest, StatesNewPage{ + Page: makePage(r, &Page{Title: "New State", Section: "states"}), + Path: statePath, + PathDuplicate: true, + }) + return + } + destination := path.Join("/version", strconv.Itoa(version.Id)) + http.Redirect(w, r, destination, http.StatusFound) + }) +} -- cgit v1.2.3