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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
package admin
import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"thehouseoficarus/internal/behavior"
)
func (s *AdminServer) handleDrops(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
ids, err := listYAMLFiles(s.dataDir, "drops")
if err != nil {
writeJSON(w, map[string]any{"error": err.Error()})
return
}
if ids == nil {
ids = []string{}
}
writeJSON(w, ids)
case http.MethodPost:
s.createDrop(w, r)
default:
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
}
}
func (s *AdminServer) handleDropByID(w http.ResponseWriter, r *http.Request) {
id := strings.TrimPrefix(r.URL.Path, "/api/drops/")
if id == "" {
http.Error(w, `{"error":"missing drop id"}`, http.StatusBadRequest)
return
}
switch r.Method {
case http.MethodGet:
s.getDrop(w, r, id)
case http.MethodPut:
s.updateDrop(w, r, id)
case http.MethodDelete:
s.deleteDrop(w, r, id)
default:
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
}
}
func (s *AdminServer) getDrop(w http.ResponseWriter, r *http.Request, id string) {
data, path, err := readYAMLFile(s.dataDir, "drops", id)
if err != nil {
http.Error(w, `{"error":"drop not found"}`, http.StatusNotFound)
return
}
m, err := yamlToMap(data)
if err != nil {
http.Error(w, `{"error":"failed to parse drop yaml"}`, http.StatusInternalServerError)
return
}
m["_path"] = path
m["_raw"] = string(data)
writeJSON(w, m)
}
func (s *AdminServer) updateDrop(w http.ResponseWriter, r *http.Request, id string) {
_, path, err := readYAMLFile(s.dataDir, "drops", id)
if err != nil {
http.Error(w, `{"error":"drop not found"}`, http.StatusNotFound)
return
}
var m map[string]any
if err := readJSON(r, &m); err != nil {
http.Error(w, `{"error":"invalid json body"}`, http.StatusBadRequest)
return
}
m["id"] = id
oldContent, err := snapshotFile(path)
if err != nil {
http.Error(w, `{"error":"failed to read existing drop"}`, http.StatusInternalServerError)
return
}
newContent, err := writeMapAsYAML(path, m)
if err != nil {
http.Error(w, `{"error":"failed to write drop"}`, http.StatusInternalServerError)
return
}
behavior.ClearDropIndex()
s.undoStack.Push(ChangeDesc{
Description: fmt.Sprintf("Update drop %s", id),
FilePath: path,
OldContent: oldContent,
NewContent: newContent,
})
writeJSON(w, m)
}
func (s *AdminServer) deleteDrop(w http.ResponseWriter, r *http.Request, id string) {
_, path, err := readYAMLFile(s.dataDir, "drops", id)
if err != nil {
http.Error(w, `{"error":"drop not found"}`, http.StatusNotFound)
return
}
oldContent := backupFile(path)
if err := os.Remove(path); err != nil {
http.Error(w, `{"error":"failed to delete drop"}`, http.StatusInternalServerError)
return
}
behavior.ClearDropIndex()
s.undoStack.Push(ChangeDesc{
Description: fmt.Sprintf("Delete drop %s", id),
FilePath: path,
OldContent: oldContent,
IsDelete: true,
})
writeJSON(w, map[string]any{"deleted": id})
}
func (s *AdminServer) createDrop(w http.ResponseWriter, r *http.Request) {
var m map[string]any
if err := readJSON(r, &m); err != nil {
http.Error(w, `{"error":"invalid json body"}`, http.StatusBadRequest)
return
}
id, ok := m["id"].(string)
if !ok || strings.TrimSpace(id) == "" {
http.Error(w, `{"error":"missing drop id"}`, http.StatusBadRequest)
return
}
path := filepath.Join(s.dataDir, "drops", id+".yaml")
if _, err := os.Stat(path); err == nil {
http.Error(w, `{"error":"drop already exists"}`, http.StatusConflict)
return
}
newContent, err := writeMapAsYAML(path, m)
if err != nil {
http.Error(w, `{"error":"failed to write drop"}`, http.StatusInternalServerError)
return
}
behavior.ClearDropIndex()
s.undoStack.Push(ChangeDesc{
Description: fmt.Sprintf("Create drop %s", id),
FilePath: path,
NewContent: newContent,
IsCreate: true,
})
writeJSON(w, m)
}
|