diff options
Diffstat (limited to '')
-rw-r--r-- | pkg/webui/html/login.html | 2 | ||||
-rw-r--r-- | pkg/webui/html/states.html | 99 | ||||
-rw-r--r-- | pkg/webui/html/states_new.html | 33 | ||||
-rw-r--r-- | pkg/webui/routes.go | 3 | ||||
-rw-r--r-- | pkg/webui/state.go | 7 | ||||
-rw-r--r-- | pkg/webui/states.go | 73 | ||||
-rw-r--r-- | pkg/webui/states_new.go | 84 | ||||
-rw-r--r-- | pkg/webui/version.go | 11 |
8 files changed, 144 insertions, 168 deletions
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 @@ <main class="responsive"> <form action="/login" method="post"> <fieldset> - <div class="field border label{{ if .Forbidden }} invalid{{ end}}"> + <div class="field border label{{ if .Forbidden }} invalid{{ end }}"> <input autofocus id="username" name="username" diff --git a/pkg/webui/html/states.html b/pkg/webui/html/states.html index 37d80cf..be8329a 100644 --- a/pkg/webui/html/states.html +++ b/pkg/webui/html/states.html @@ -1,38 +1,71 @@ {{ define "main" }} <main class="responsive" id="main"> - <a href="/states/new"> - <button class="small-round"> - <i>add</i> - <span>New</span> - </button> - </a> - <table class="clickable-rows no-space"> - <thead> - <tr> - <th>Path</th> - <th>Updated</th> - <th>Locked</th> - </tr> - </thead> - <tbody> - {{ range .States }} - <tr> - <td><a href="/state/{{ .Id }}">{{ .Path }}</a></td> - <td><a href="/state/{{ .Id }}">{{ .Updated }}</a></td> - <td> - <a href="/state/{{ .Id }}"> - {{ if eq .Lock nil }}no{{ else }} - <span>yes</span> - <div class="tooltip left max"> - <b>Lock</b> - <p>{{ .Lock }}</p> - </div> + <div> + <div class="tabs"> + <a data-ui="#explorer"{{ if eq .ActiveTab 0 }} class="active"{{ end }}>States</a> + <a data-ui="#new"{{ if eq .ActiveTab 1 }} class="active"{{ end }}>Create New State</a> + </div> + <div id="explorer" class="page padding{{ if eq .ActiveTab 0 }} active{{ end }}"> + <table class="clickable-rows no-space"> + <thead> + <tr> + <th>Path</th> + <th>Updated</th> + <th>Locked</th> + </tr> + </thead> + <tbody> + {{ range .States }} + <tr> + <td><a href="/state/{{ .Id }}">{{ .Path }}</a></td> + <td><a href="/state/{{ .Id }}">{{ .Updated }}</a></td> + <td> + <a href="/state/{{ .Id }}"> + {{ if eq .Lock nil }}no{{ else }} + <span>yes</span> + <div class="tooltip left max"> + <b>Lock</b> + <p>{{ .Lock }}</p> + </div> + {{ end }} + </a> + </td> + </tr> + {{ end }} + </tbody> + </table> + </div> + <div id="new" class="page padding{{ if eq .ActiveTab 1 }} active{{ end }}"> + <form action="/states" enctype="multipart/form-data" method="post"> + <fieldset> + <div class="field border label{{ if .PathError }} invalid{{ end }}"> + <input autofocus + id="path" + name="path" + required + type="text" + value="{{ .Path }}"> + <label for="path">Path</label> + {{ if .PathDuplicate }} + <span class="error">This path already exist</span> + {{ else if .PathError }} + <span class="error">Invalid path</span> + {{ else }} + <span class="helper">Valid URL path beginning with a /</span> {{ end }} - </a> - </td> - </tr> - {{ end }} - </tbody> - </table> + </div> + <div class="field label border"> + <input name="file" + required + type="file"> + <input type="text"> + <label>File</label> + <span class="helper">JSON state file</span> + </div> + <button class="small-round" type="submit" value="submit">New</button> + </fieldset> + </form> + </div> + </div> </main> {{ 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" }} -<main class="responsive"> - <form action="/states/new" enctype="multipart/form-data" method="post"> - <fieldset> - <div class="field border label{{ if .PathError }} invalid{{ end }}"> - <input autofocus - id="path" - name="path" - required - type="text" - value="{{ .Path }}"> - <label for="path">Path</label> - {{ if .PathDuplicate }} - <span class="error">This path already exist</span> - {{ else if .PathError }} - <span class="error">Invalid path</span> - {{ else }} - <span class="helper">Valid URL path beginning with a /</span> - {{ end }} - </div> - <div class="field label border"> - <input name="file" - required - type="file"> - <input type="text"> - <label>File</label> - <span class="helper">JSON state file</span> - </div> - <button class="small-round" type="submit" value="submit">New</button> - </fieldset> - </form> -</main> -{{ 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/state.go b/pkg/webui/state.go index 2ad1597..c12ee03 100644 --- a/pkg/webui/state.go +++ b/pkg/webui/state.go @@ -3,10 +3,10 @@ package webui import ( "html/template" "net/http" - "strconv" "git.adyxax.org/adyxax/tfstated/pkg/database" "git.adyxax.org/adyxax/tfstated/pkg/model" + "go.n16f.net/uuid" ) var stateTemplate = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/state.html")) @@ -19,9 +19,8 @@ func handleStateGET(db *database.DB) http.Handler { 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 { + var stateId uuid.UUID + if err := stateId.Parse(r.PathValue("id")); err != nil { errorResponse(w, http.StatusBadRequest, err) return } diff --git a/pkg/webui/states.go b/pkg/webui/states.go index a0d16ca..c68b2c9 100644 --- a/pkg/webui/states.go +++ b/pkg/webui/states.go @@ -1,29 +1,92 @@ package webui import ( + "fmt" "html/template" + "io" "net/http" + "net/url" + "path" "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", version.Id.String()) + 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) - }) -} diff --git a/pkg/webui/version.go b/pkg/webui/version.go index a577d5f..62a0e22 100644 --- a/pkg/webui/version.go +++ b/pkg/webui/version.go @@ -1,13 +1,13 @@ package webui import ( - "fmt" "html/template" "net/http" - "strconv" + "path" "git.adyxax.org/adyxax/tfstated/pkg/database" "git.adyxax.org/adyxax/tfstated/pkg/model" + "go.n16f.net/uuid" ) var versionTemplate = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/version.html")) @@ -21,9 +21,8 @@ func handleVersionGET(db *database.DB) http.Handler { VersionData string } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - versionIdStr := r.PathValue("id") - versionId, err := strconv.Atoi(versionIdStr) - if err != nil { + var versionId uuid.UUID + if err := versionId.Parse(r.PathValue("id")); err != nil { errorResponse(w, http.StatusBadRequest, err) return } @@ -49,7 +48,7 @@ func handleVersionGET(db *database.DB) http.Handler { versionData := string(version.Data[:]) render(w, versionTemplate, http.StatusOK, VersionsData{ Page: makePage(r, &Page{ - Precedent: fmt.Sprintf("/state/%d", state.Id), + Precedent: path.Join("/state/", state.Id.String()), Section: "states", Title: state.Path, }), |