170 lines
2.6 KiB
Go
170 lines
2.6 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",
|
|
strings.TrimSpace(r.FormValue("home_path")),
|
|
)
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = s.store.SetConfig(
|
|
"wiki_name",
|
|
strings.TrimSpace(r.FormValue("wiki_name")),
|
|
)
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = s.store.SetConfig(
|
|
"locale",
|
|
normalizeLocale(
|
|
strings.TrimSpace(r.FormValue("locale")),
|
|
),
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
wikiName, err := s.store.GetConfig("wiki_name")
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if wikiName == "" {
|
|
wikiName = "Pages Wiki"
|
|
}
|
|
|
|
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,
|
|
"WikiName": wikiName,
|
|
"Locale": s.getLocale(),
|
|
"Objects": recent,
|
|
"NavItemsConfig": s.getNavItems(),
|
|
},
|
|
)
|
|
}
|