Device management

This commit is contained in:
Magnus Åhall 2026-02-26 21:37:26 +01:00
parent 5635c2af1a
commit b9a5437909
6 changed files with 294 additions and 9 deletions

View file

@ -33,7 +33,8 @@ func registerWebserverHandlers() { // {{{
}
http.HandleFunc("/", rootHandler)
http.HandleFunc("/devices", actionDevices)
http.HandleFunc("GET /devices", actionDevices)
http.HandleFunc("POST /device", actionDeviceUpdate)
http.HandleFunc("GET /device/{dev}/dns_records", actionDNSRecords)
http.HandleFunc("POST /device/{dev}/record", actionRecordSave)
http.HandleFunc("DELETE /device/{dev}/record/{id}", actionRecordDelete)
@ -96,6 +97,35 @@ func actionDevices(w http.ResponseWriter, r *http.Request) { // {{{
w.Write(j)
} // }}}
func actionDeviceUpdate(w http.ResponseWriter, r *http.Request) { // {{{
var req struct {
CurrentName string
Device Device
}
body, _ := io.ReadAll(r.Body)
err := json.Unmarshal(body, &req)
if err != nil {
httpError(w, err)
return
}
device, err := config.UpdateDevice(req.CurrentName, req.Device)
if err != nil {
httpError(w, err)
return
}
device.Password = "" // don't leak unnecessarily
j, _ := json.Marshal(struct {
OK bool
Device Device
}{
true,
device,
})
w.Write(j)
} // }}}
func actionDNSRecords(w http.ResponseWriter, r *http.Request) { // {{{
devname := r.PathValue("dev")