From 169e2d0d9d253c316fa322d3200230704b57fb76 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 22 Feb 2025 00:35:17 +0100 Subject: chore(webui): refactored state creation page and route --- pkg/database/db.go | 5 ++- pkg/webui/html/login.html | 2 +- pkg/webui/html/states.html | 99 ++++++++++++++++++++++++++++-------------- pkg/webui/html/states_new.html | 33 -------------- pkg/webui/routes.go | 3 +- pkg/webui/states.go | 74 ++++++++++++++++++++++++++++--- pkg/webui/states_new.go | 84 ----------------------------------- 7 files changed, 141 insertions(+), 159 deletions(-) delete mode 100644 pkg/webui/html/states_new.html delete mode 100644 pkg/webui/states_new.go diff --git a/pkg/database/db.go b/pkg/database/db.go index eac31eb..c155ecc 100644 --- a/pkg/database/db.go +++ b/pkg/database/db.go @@ -104,7 +104,10 @@ func (db *DB) Close() error { _ = db.writeDB.Close() return fmt.Errorf("failed to close read database connection: %w", err) } - return db.writeDB.Close() + if err := db.writeDB.Close(); err != nil { + return fmt.Errorf("failed to close write database connection: %w", err) + } + return nil } func (db *DB) Exec(query string, args ...any) (sql.Result, error) { diff --git a/pkg/webui/html/login.html b/pkg/webui/html/login.html index deb6d4a..bdc8fa7 100644 --- a/pkg/webui/html/login.html +++ b/pkg/webui/html/login.html @@ -2,7 +2,7 @@ - + - - - add - New - - - - - - Path - Updated - Locked - - - - {{ range .States }} - - {{ .Path }} - {{ .Updated }} - - - {{ if eq .Lock nil }}no{{ else }} - yes - - Lock - {{ .Lock }} - + + + States + Create New State + + + + + + Path + Updated + Locked + + + + {{ range .States }} + + {{ .Path }} + {{ .Updated }} + + + {{ if eq .Lock nil }}no{{ else }} + yes + + Lock + {{ .Lock }} + + {{ end }} + + + + {{ end }} + + + + + + + + + Path + {{ if .PathDuplicate }} + This path already exist + {{ else if .PathError }} + Invalid path + {{ else }} + Valid URL path beginning with a / {{ end }} - - - - {{ end }} - - + + + + + File + JSON state file + + New + + + + {{ end }} diff --git a/pkg/webui/html/states_new.html b/pkg/webui/html/states_new.html deleted file mode 100644 index 68facc7..0000000 --- a/pkg/webui/html/states_new.html +++ /dev/null @@ -1,33 +0,0 @@ -{{ define "main" }} - - - - - - Path - {{ if .PathDuplicate }} - This path already exist - {{ else if .PathError }} - Invalid path - {{ else }} - Valid URL path beginning with a / - {{ end }} - - - - - File - JSON state file - - New - - - -{{ end }} diff --git a/pkg/webui/routes.go b/pkg/webui/routes.go index 7ee7841..5c500d2 100644 --- a/pkg/webui/routes.go +++ b/pkg/webui/routes.go @@ -19,8 +19,7 @@ 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("POST /states", requireLogin(handleStatesPOST(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.go b/pkg/webui/states.go index a0d16ca..85303fe 100644 --- a/pkg/webui/states.go +++ b/pkg/webui/states.go @@ -1,29 +1,93 @@ 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 StatesPage struct { + ActiveTab int + Page *Page + Path string + PathError bool + PathDuplicate bool + States []model.State +} + var statesTemplates = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/states.html")) func handleStatesGET(db *database.DB) http.Handler { - type StatesData struct { - Page *Page - States []model.State - } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { states, err := db.LoadStates() if err != nil { errorResponse(w, http.StatusInternalServerError, err) return } - render(w, statesTemplates, http.StatusOK, StatesData{ + render(w, statesTemplates, http.StatusOK, StatesPage{ Page: makePage(r, &Page{Title: "States", Section: "states"}), States: states, }) }) } + +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 + } + destination := path.Join("/version", strconv.Itoa(version.Id)) + http.Redirect(w, r, destination, http.StatusFound) + }) +} diff --git a/pkg/webui/states_new.go b/pkg/webui/states_new.go deleted file mode 100644 index 8551191..0000000 --- a/pkg/webui/states_new.go +++ /dev/null @@ -1,84 +0,0 @@ -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
{{ .Lock }}