1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
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"
"go.n16f.net/uuid"
)
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"))
var statesIdTemplate = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/statesId.html"))
func handleStatesGET(db *database.DB) http.Handler {
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, 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("/versions", version.Id.String())
http.Redirect(w, r, destination, http.StatusFound)
})
}
func handleStatesIdGET(db *database.DB) http.Handler {
type StatesData struct {
Page *Page
State *model.State
Usernames map[string]string
Versions []model.Version
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var stateId uuid.UUID
if err := stateId.Parse(r.PathValue("id")); err != nil {
errorResponse(w, http.StatusBadRequest, err)
return
}
state, err := db.LoadStateById(stateId)
if err != nil {
errorResponse(w, http.StatusInternalServerError, err)
return
}
versions, err := db.LoadVersionsByState(state)
if err != nil {
errorResponse(w, http.StatusInternalServerError, err)
return
}
usernames, err := db.LoadAccountUsernames()
if err != nil {
errorResponse(w, http.StatusInternalServerError, err)
return
}
render(w, statesIdTemplate, http.StatusOK, StatesData{
Page: makePage(r, &Page{
Precedent: "/states",
Section: "states",
Title: state.Path,
}),
State: state,
Usernames: usernames,
Versions: versions,
})
})
}
|