Add district settings management
This commit is contained in:
1 parent
8a2ac56844
commit
b37427535c
5 files changed
+627
-2
No files matched your search
@@ -283,6 +283,70 @@ func (s *Server) handleConfig(
|
||||
return
|
||||
}
|
||||
|
||||
case "district-create":
|
||||
|
||||
err = s.store.CreateDistrict(
|
||||
r.FormValue("district_name"),
|
||||
r.FormValue("district_title"),
|
||||
r.FormValue("district_domain"),
|
||||
r.FormValue("district_enabled") == "1",
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
case "district-update":
|
||||
|
||||
id, parseErr :=
|
||||
strconv.ParseInt(
|
||||
r.FormValue("district_id"),
|
||||
10,
|
||||
64,
|
||||
)
|
||||
|
||||
if parseErr != nil {
|
||||
http.Error(w, "invalid district id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = s.store.UpdateDistrict(
|
||||
id,
|
||||
r.FormValue("district_name"),
|
||||
r.FormValue("district_title"),
|
||||
r.FormValue("district_domain"),
|
||||
r.FormValue("district_enabled") == "1",
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
case "district-delete":
|
||||
|
||||
id, parseErr :=
|
||||
strconv.ParseInt(
|
||||
r.FormValue("district_id"),
|
||||
10,
|
||||
64,
|
||||
)
|
||||
|
||||
if parseErr != nil {
|
||||
http.Error(w, "invalid district id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = s.store.DeleteDistrict(
|
||||
id,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
case "zombie-purge":
|
||||
|
||||
_, err = s.store.PurgeZombieObjects(
|
||||
@@ -312,6 +376,10 @@ func (s *Server) handleConfig(
|
||||
panel = "objects"
|
||||
}
|
||||
|
||||
if strings.HasPrefix(section, "district-") {
|
||||
panel = "districts"
|
||||
}
|
||||
|
||||
http.Redirect(
|
||||
w,
|
||||
r,
|
||||
@@ -369,6 +437,14 @@ func (s *Server) handleConfig(
|
||||
return
|
||||
}
|
||||
|
||||
districts, err :=
|
||||
s.store.ListDistricts()
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
s.renderWithContext(
|
||||
w,
|
||||
r,
|
||||
@@ -390,6 +466,7 @@ func (s *Server) handleConfig(
|
||||
"ZombieGraceHours": zombieGraceHours,
|
||||
"Objects": recent,
|
||||
"ZombieObjects": zombies,
|
||||
"Districts": districts,
|
||||
"HomeNavItemsConfig": s.getHomeNavItems(),
|
||||
"SystemNavItemsConfig": s.getSystemNavItems(),
|
||||
},
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package server
|
||||
|
||||
import "pageswiki/internal/store"
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) currentDistrict() *store.District {
|
||||
|
||||
@@ -18,3 +22,23 @@ func (s *Server) currentDistrict() *store.District {
|
||||
|
||||
return sp
|
||||
}
|
||||
|
||||
func (s *Server) districtForRequest(
|
||||
r *http.Request,
|
||||
) *store.District {
|
||||
|
||||
if r == nil {
|
||||
return s.currentDistrict()
|
||||
}
|
||||
|
||||
sp, err :=
|
||||
s.store.GetDistrictByHost(
|
||||
r.Host,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return s.currentDistrict()
|
||||
}
|
||||
|
||||
return sp
|
||||
}
|
||||
@@ -15,7 +15,9 @@ func (s *Server) BuildRequestContext(
|
||||
) *RequestContext {
|
||||
|
||||
return &RequestContext{
|
||||
District: s.currentDistrict(),
|
||||
District: s.districtForRequest(
|
||||
r,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -95,6 +97,236 @@ func (s *Store) GetDefaultDistrict() (*District, error) {
|
||||
return scanDistrict(row)
|
||||
}
|
||||
|
||||
func (s *Store) GetDistrictByHost(
|
||||
host string,
|
||||
) (*District, error) {
|
||||
|
||||
host =
|
||||
normalizeDistrictHost(
|
||||
host,
|
||||
)
|
||||
|
||||
if host == "" {
|
||||
return s.GetDefaultDistrict()
|
||||
}
|
||||
|
||||
subdomain :=
|
||||
host
|
||||
|
||||
if i := strings.IndexByte(host, '.'); i > 0 {
|
||||
subdomain = host[:i]
|
||||
}
|
||||
|
||||
row := s.DB.QueryRow(`
|
||||
SELECT
|
||||
id,
|
||||
uuid,
|
||||
name,
|
||||
title,
|
||||
domain,
|
||||
enabled,
|
||||
created_at
|
||||
FROM districts
|
||||
WHERE enabled = 1
|
||||
AND (
|
||||
lower(domain) = ?
|
||||
OR lower(domain) = ?
|
||||
OR lower(name) = ?
|
||||
)
|
||||
ORDER BY
|
||||
CASE WHEN lower(domain) = ? THEN 0 ELSE 1 END,
|
||||
id
|
||||
LIMIT 1
|
||||
`,
|
||||
host,
|
||||
subdomain,
|
||||
subdomain,
|
||||
host,
|
||||
)
|
||||
|
||||
d, err :=
|
||||
scanDistrict(
|
||||
row,
|
||||
)
|
||||
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return s.GetDefaultDistrict()
|
||||
}
|
||||
|
||||
return d, err
|
||||
}
|
||||
|
||||
func (s *Store) ListDistricts() ([]District, error) {
|
||||
|
||||
rows, err := s.DB.Query(`
|
||||
SELECT
|
||||
id,
|
||||
uuid,
|
||||
name,
|
||||
title,
|
||||
domain,
|
||||
enabled,
|
||||
created_at
|
||||
FROM districts
|
||||
ORDER BY
|
||||
CASE WHEN name = 'default' THEN 0 ELSE 1 END,
|
||||
name,
|
||||
id
|
||||
`)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
var out []District
|
||||
|
||||
for rows.Next() {
|
||||
d, err :=
|
||||
scanDistrict(
|
||||
rows,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out = append(
|
||||
out,
|
||||
*d,
|
||||
)
|
||||
}
|
||||
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) CreateDistrict(
|
||||
name string,
|
||||
title string,
|
||||
domain string,
|
||||
enabled bool,
|
||||
) error {
|
||||
|
||||
name =
|
||||
normalizeDistrictName(
|
||||
name,
|
||||
)
|
||||
|
||||
title =
|
||||
strings.TrimSpace(
|
||||
title,
|
||||
)
|
||||
|
||||
domain =
|
||||
normalizeDistrictHost(
|
||||
domain,
|
||||
)
|
||||
|
||||
if name == "" {
|
||||
return errors.New("district name is required")
|
||||
}
|
||||
|
||||
if title == "" {
|
||||
title = name
|
||||
}
|
||||
|
||||
if err := s.ensureDistrictUnique(0, name, domain); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enabledValue := 0
|
||||
|
||||
if enabled {
|
||||
enabledValue = 1
|
||||
}
|
||||
|
||||
_, err := s.DB.Exec(`
|
||||
INSERT INTO districts (
|
||||
uuid,
|
||||
name,
|
||||
title,
|
||||
domain,
|
||||
enabled,
|
||||
created_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`,
|
||||
uuid.NewString(),
|
||||
name,
|
||||
title,
|
||||
domain,
|
||||
enabledValue,
|
||||
time.Now().Unix(),
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) UpdateDistrict(
|
||||
id int64,
|
||||
name string,
|
||||
title string,
|
||||
domain string,
|
||||
enabled bool,
|
||||
) error {
|
||||
|
||||
name =
|
||||
normalizeDistrictName(
|
||||
name,
|
||||
)
|
||||
|
||||
title =
|
||||
strings.TrimSpace(
|
||||
title,
|
||||
)
|
||||
|
||||
domain =
|
||||
normalizeDistrictHost(
|
||||
domain,
|
||||
)
|
||||
|
||||
if id <= 0 {
|
||||
return errors.New("district id is required")
|
||||
}
|
||||
|
||||
if name == "" {
|
||||
return errors.New("district name is required")
|
||||
}
|
||||
|
||||
if title == "" {
|
||||
title = name
|
||||
}
|
||||
|
||||
if err := s.ensureDistrictUnique(id, name, domain); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enabledValue := 0
|
||||
|
||||
if enabled {
|
||||
enabledValue = 1
|
||||
}
|
||||
|
||||
_, err := s.DB.Exec(`
|
||||
UPDATE districts
|
||||
SET
|
||||
name = ?,
|
||||
title = ?,
|
||||
domain = ?,
|
||||
enabled = ?
|
||||
WHERE id = ?
|
||||
`,
|
||||
name,
|
||||
title,
|
||||
domain,
|
||||
enabledValue,
|
||||
id,
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) CountDistricts() (int, error) {
|
||||
|
||||
var count int
|
||||
@@ -155,3 +387,83 @@ func scanDistrict(row interface {
|
||||
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
func normalizeDistrictName(
|
||||
name string,
|
||||
) string {
|
||||
|
||||
name =
|
||||
strings.TrimSpace(
|
||||
strings.ToLower(
|
||||
name,
|
||||
),
|
||||
)
|
||||
|
||||
name =
|
||||
strings.ReplaceAll(
|
||||
name,
|
||||
" ",
|
||||
"-",
|
||||
)
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
func normalizeDistrictHost(
|
||||
host string,
|
||||
) string {
|
||||
|
||||
host =
|
||||
strings.TrimSpace(
|
||||
strings.ToLower(
|
||||
host,
|
||||
),
|
||||
)
|
||||
|
||||
if host == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
if i := strings.IndexByte(host, ':'); i > 0 {
|
||||
host = host[:i]
|
||||
}
|
||||
|
||||
return strings.Trim(
|
||||
host,
|
||||
".",
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Store) ensureDistrictUnique(
|
||||
excludeID int64,
|
||||
name string,
|
||||
domain string,
|
||||
) error {
|
||||
|
||||
var count int
|
||||
|
||||
err := s.DB.QueryRow(`
|
||||
SELECT count(*)
|
||||
FROM districts
|
||||
WHERE id != ?
|
||||
AND (
|
||||
lower(name) = ?
|
||||
OR (? != '' AND lower(domain) = ?)
|
||||
)
|
||||
`,
|
||||
excludeID,
|
||||
name,
|
||||
domain,
|
||||
domain,
|
||||
).Scan(&count)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return errors.New("district name or domain already exists")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user