Records update

This commit is contained in:
Magnus Åhall 2026-02-25 07:49:28 +01:00
parent 97058d036d
commit 2ae93b6fd4
4 changed files with 136 additions and 191 deletions

View file

@ -6,12 +6,11 @@ import (
// Standard
"embed"
_ "encoding/json"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"slices"
_ "html/template"
)
var (
@ -33,6 +32,7 @@ func registerWebserverHandlers() {
}
http.HandleFunc("/", rootHandler)
http.HandleFunc("/record/save", actionRecordSave)
}
func startWebserver() {
@ -56,31 +56,14 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
return
}
var entries []DNSRecord
var entries []*DNSRecord
entries, err = device.StaticDNSEntries()
if err != nil {
w.Write([]byte(err.Error()))
return
}
slices.SortFunc(entries, SortDNSRecord)
data["DNSRecords"] = entries
/*
tree := BuildRecordsTree(entries)
htmlElements := tree.ToHTMLElements([]string{})
data["TreeData"] = tree
var html string
for _, el := range htmlElements {
html += el.HTML
}
data["Tree"] = template.HTML(html)
j, _ := json.Marshal(tree)
os.WriteFile("/tmp/tree.json", j, 0644)
*/
data["VERSION"] = VERSION
page.Data = data
@ -95,3 +78,36 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
htmlEngine.StaticResource(w, r)
}
func httpError(w http.ResponseWriter, err error) {
resp := struct {
OK bool
Error string
}{
false,
err.Error(),
}
j, _ := json.Marshal(resp)
w.Write(j)
}
func actionRecordSave(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
var record DNSRecord
err := json.Unmarshal(body, &record)
if err != nil {
httpError(w, err)
return
}
err = device.UpdateDNSEntry(record.toDNSEntry())
if err != nil {
httpError(w, err)
logger.Error("webserver", "op", "record_save", "error", err)
return
}
j, _ := json.Marshal(struct{ OK bool }{true})
w.Write(j)
}