merge mk_district #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
|
||||
}
|
||||
@@ -54,6 +54,10 @@
|
||||
Editor
|
||||
</a>
|
||||
|
||||
<a href="#" data-panel="districts" onclick="showConfigPanel('districts'); return false;">
|
||||
Districts
|
||||
</a>
|
||||
|
||||
<a href="#" data-panel="objects" onclick="showConfigPanel('objects'); return false;">
|
||||
{{ T "config.objects" }}
|
||||
</a>
|
||||
@@ -197,6 +201,212 @@ Save
|
||||
|
||||
</section>
|
||||
|
||||
<section
|
||||
id="panel-districts"
|
||||
class="config-section config-panel"
|
||||
>
|
||||
|
||||
<h2>Districts</h2>
|
||||
|
||||
<p class="muted">
|
||||
Districts are selected from the request host. The domain may be a full host or a subdomain label.
|
||||
</p>
|
||||
|
||||
<table class="recent-table">
|
||||
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Name</th>
|
||||
<th>Title</th>
|
||||
<th>Domain</th>
|
||||
<th>Enabled</th>
|
||||
<th>Created</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
|
||||
{{ range .Districts }}
|
||||
|
||||
<tr>
|
||||
<td class="mono">
|
||||
{{ .ID }}
|
||||
<form
|
||||
id="district-update-{{ .ID }}"
|
||||
method="post"
|
||||
action="/-/sys/config"
|
||||
></form>
|
||||
<form
|
||||
id="district-delete-{{ .ID }}"
|
||||
method="post"
|
||||
action="/-/sys/config"
|
||||
onsubmit="return confirm('Delete this district?')"
|
||||
></form>
|
||||
<input
|
||||
form="district-update-{{ .ID }}"
|
||||
type="hidden"
|
||||
name="section"
|
||||
value="district-update"
|
||||
>
|
||||
<input
|
||||
form="district-update-{{ .ID }}"
|
||||
type="hidden"
|
||||
name="district_id"
|
||||
value="{{ .ID }}"
|
||||
>
|
||||
<input
|
||||
form="district-delete-{{ .ID }}"
|
||||
type="hidden"
|
||||
name="section"
|
||||
value="district-delete"
|
||||
>
|
||||
<input
|
||||
form="district-delete-{{ .ID }}"
|
||||
type="hidden"
|
||||
name="district_id"
|
||||
value="{{ .ID }}"
|
||||
>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<input
|
||||
form="district-update-{{ .ID }}"
|
||||
name="district_name"
|
||||
type="text"
|
||||
value="{{ .Name }}"
|
||||
>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<input
|
||||
form="district-update-{{ .ID }}"
|
||||
name="district_title"
|
||||
type="text"
|
||||
value="{{ .Title }}"
|
||||
>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<input
|
||||
form="district-update-{{ .ID }}"
|
||||
name="district_domain"
|
||||
type="text"
|
||||
value="{{ .Domain }}"
|
||||
placeholder="docs or docs.example.test"
|
||||
>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<label>
|
||||
<input
|
||||
form="district-update-{{ .ID }}"
|
||||
type="checkbox"
|
||||
name="district_enabled"
|
||||
value="1"
|
||||
{{ if .Enabled }}checked{{ end }}
|
||||
>
|
||||
Enabled
|
||||
</label>
|
||||
</td>
|
||||
|
||||
<td class="mono">
|
||||
{{ formatUnix .CreatedAt }}
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<button
|
||||
form="district-update-{{ .ID }}"
|
||||
type="submit"
|
||||
>
|
||||
Save
|
||||
</button>
|
||||
<button
|
||||
form="district-delete-{{ .ID }}"
|
||||
type="submit"
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
{{ end }}
|
||||
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
|
||||
<h3>Create District</h3>
|
||||
|
||||
<form
|
||||
method="post"
|
||||
action="/-/sys/config"
|
||||
>
|
||||
|
||||
<input
|
||||
type="hidden"
|
||||
name="section"
|
||||
value="district-create"
|
||||
>
|
||||
|
||||
<label>
|
||||
Name
|
||||
</label>
|
||||
<input
|
||||
name="district_name"
|
||||
type="text"
|
||||
placeholder="docs"
|
||||
>
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
<label>
|
||||
Title
|
||||
</label>
|
||||
<input
|
||||
name="district_title"
|
||||
type="text"
|
||||
placeholder="Documentation"
|
||||
>
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
<label>
|
||||
Domain
|
||||
</label>
|
||||
<input
|
||||
name="district_domain"
|
||||
type="text"
|
||||
placeholder="docs or docs.example.test"
|
||||
>
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
<label>
|
||||
<input
|
||||
type="checkbox"
|
||||
name="district_enabled"
|
||||
value="1"
|
||||
checked
|
||||
>
|
||||
Enabled
|
||||
</label>
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
<button type="submit">
|
||||
Create
|
||||
</button>
|
||||
|
||||
</form>
|
||||
|
||||
</section>
|
||||
|
||||
<section
|
||||
id="panel-objects"
|
||||
class="config-section config-panel"
|
||||
|
||||
Reference in new issue
Block a user