Files
pages/internal/server/config_handler.go
T

144 lines
1.9 KiB
Go

package server
import (
"encoding/json"
"net/http"
"strings"
)
func (s *Server) handleConfig(
w http.ResponseWriter,
r *http.Request,
) {
if r.Method == http.MethodPost {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
section :=
r.FormValue("section")
switch section {
case "home":
err = s.store.SetConfig(
"home_path",
r.FormValue("home_path"),
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
case "nav":
labels :=
r.Form["nav_label"]
hrefs :=
r.Form["nav_href"]
var items []NavItem
for i := range labels {
label :=
strings.TrimSpace(labels[i])
if label == "" {
continue
}
if i >= len(hrefs) {
continue
}
href :=
strings.TrimSpace(hrefs[i])
if href == "" {
continue
}
items = append(
items,
NavItem{
Label: label,
Href: href,
},
)
}
raw, err :=
json.Marshal(items)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = s.store.SetConfig(
"nav_items",
string(raw),
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
default:
http.Error(
w,
"unknown config section",
http.StatusBadRequest,
)
return
}
http.Redirect(
w,
r,
"/-/sys/config?panel="+section,
http.StatusFound,
)
return
}
homePath, err :=
s.store.GetConfig("home_path")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
recent, err :=
s.store.ListRecentObjects(50)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
s.render(
w,
"config.gohtml",
map[string]any{
"HomePath": homePath,
"Objects": recent,
"NavItemsConfig": s.getNavItems(),
},
)
}