Build out the admin panel
- Overview: users, organizations, workspaces, spaces, pages, files and storage (org admins see their organization), plus pages open right now, the search index size, and version, uptime and database health. - Users: invite people by email (with resend, revoke and a copyable link when mail fails) and send a password-reset email. - Authentication: sign-up on or off, the email-domain allow-list, and sign-in links by email. - Branding: instance name, accent colour and logo. The web app shows them on the sign-in pages and applies the accent colour everywhere. - Email and Storage: SMTP and S3 settings with test buttons. - Maintenance: create, download and delete database backups, and a view of the background jobs with their last run, duration and errors. On the server, a small jobs registry records each search-indexing pass, and "search.rebuild" replaces the separate reindex endpoint. A new instance package serves the dashboard numbers, health and backups. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
92cd81fd96
commit
ffc6cc4fbc
31 files changed
+2326
-134
No files matched your search
@@ -0,0 +1,250 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// Package instance serves the admin panel's view of the whole server: the
|
||||
// dashboard numbers, system health, background jobs and database backups.
|
||||
package instance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"docwell/server/internal/access"
|
||||
"docwell/server/internal/apperr"
|
||||
"docwell/server/internal/collab"
|
||||
"docwell/server/internal/jobs"
|
||||
"docwell/server/internal/store/pocketbase"
|
||||
)
|
||||
|
||||
// Live is what the collaboration hub reports.
|
||||
type Live interface{ Stats() collab.Stats }
|
||||
|
||||
// Indexed is what the search index reports.
|
||||
type Indexed interface{ Count() (uint64, error) }
|
||||
|
||||
type Service struct {
|
||||
pb *pocketbase.Client
|
||||
version string
|
||||
started time.Time
|
||||
live Live
|
||||
index Indexed
|
||||
jobs *jobs.Registry
|
||||
}
|
||||
|
||||
func New(pb *pocketbase.Client, version string, live Live, index Indexed, reg *jobs.Registry) *Service {
|
||||
return &Service{pb: pb, version: version, started: time.Now(), live: live, index: index, jobs: reg}
|
||||
}
|
||||
|
||||
// Stats are the dashboard numbers; an org admin sees their organization's.
|
||||
type Stats struct {
|
||||
// Scope is "instance" for superadmins, "organization" for org admins.
|
||||
Scope string `json:"scope"`
|
||||
Users int `json:"users"`
|
||||
Organizations int `json:"organizations"`
|
||||
Workspaces int `json:"workspaces"`
|
||||
Spaces int `json:"spaces"`
|
||||
Pages int `json:"pages"`
|
||||
Attachments int `json:"attachments"`
|
||||
StorageBytes int64 `json:"storageBytes"`
|
||||
// Rooms and Editors are pages open right now and the people in them
|
||||
// (instance-wide only).
|
||||
Rooms int `json:"rooms"`
|
||||
Editors int `json:"editors"`
|
||||
// Indexed is the number of pages in the search index (instance-wide only).
|
||||
Indexed int `json:"indexed"`
|
||||
}
|
||||
|
||||
func (s *Service) count(ctx context.Context, collection, filter string) (int, error) {
|
||||
res, err := pocketbase.List[struct{}](ctx, s.pb, collection, pocketbase.ListOptions{PerPage: 1, Filter: filter, Fields: "id"})
|
||||
return res.TotalItems, err
|
||||
}
|
||||
|
||||
// Stats counts what c manages.
|
||||
func (s *Service) Stats(ctx context.Context, c access.Caller) (Stats, error) {
|
||||
all, org, err := access.UserScope(c)
|
||||
if err != nil {
|
||||
return Stats{}, err
|
||||
}
|
||||
st := Stats{Scope: "instance"}
|
||||
f := map[string]string{"users": "", "organizations": "", "workspaces": "", "spaces": "", "pages": "deleted_at = ''", "attachments": ""}
|
||||
if !all {
|
||||
st.Scope = "organization"
|
||||
if org == "" {
|
||||
return st, nil
|
||||
}
|
||||
f = map[string]string{
|
||||
"users": fmt.Sprintf("organization = '%s'", org),
|
||||
"organizations": fmt.Sprintf("id = '%s'", org),
|
||||
"workspaces": fmt.Sprintf("organization = '%s'", org),
|
||||
"spaces": fmt.Sprintf("workspace.organization = '%s'", org),
|
||||
"pages": fmt.Sprintf("deleted_at = '' && workspace.organization = '%s'", org),
|
||||
"attachments": fmt.Sprintf("workspace.organization = '%s'", org),
|
||||
}
|
||||
}
|
||||
for name, dst := range map[string]*int{
|
||||
"users": &st.Users, "organizations": &st.Organizations, "workspaces": &st.Workspaces,
|
||||
"spaces": &st.Spaces, "pages": &st.Pages, "attachments": &st.Attachments,
|
||||
} {
|
||||
if *dst, err = s.count(ctx, name, f[name]); err != nil {
|
||||
return Stats{}, err
|
||||
}
|
||||
}
|
||||
// Storage is the sum of attachment sizes; PocketBase has no aggregate
|
||||
// query over REST, so it pages through the size column.
|
||||
for page := 1; ; page++ {
|
||||
res, err := pocketbase.List[struct {
|
||||
Size int64 `json:"size"`
|
||||
}](ctx, s.pb, "attachments", pocketbase.ListOptions{Page: page, PerPage: 1000, Fields: "size", Filter: f["attachments"]})
|
||||
if err != nil {
|
||||
return Stats{}, err
|
||||
}
|
||||
for _, a := range res.Items {
|
||||
st.StorageBytes += a.Size
|
||||
}
|
||||
if page >= res.TotalPages || len(res.Items) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if all {
|
||||
if s.live != nil {
|
||||
live := s.live.Stats()
|
||||
st.Rooms, st.Editors = live.Rooms, live.Peers
|
||||
}
|
||||
if s.index != nil {
|
||||
if n, err := s.index.Count(); err == nil {
|
||||
st.Indexed = int(min(n, 1<<31-1))
|
||||
}
|
||||
}
|
||||
}
|
||||
return st, nil
|
||||
}
|
||||
|
||||
// Health describes the running server (superadmin).
|
||||
type Health struct {
|
||||
Version string `json:"version"`
|
||||
GoVersion string `json:"goVersion"`
|
||||
Started string `json:"started"`
|
||||
UptimeSec int64 `json:"uptimeSeconds"`
|
||||
Database struct {
|
||||
OK bool `json:"ok"`
|
||||
LatencyMs int64 `json:"latencyMs"`
|
||||
Error string `json:"error,omitempty"`
|
||||
} `json:"database"`
|
||||
Goroutines int `json:"goroutines"`
|
||||
MemoryMB uint64 `json:"memoryMb"`
|
||||
}
|
||||
|
||||
func (s *Service) Health(ctx context.Context, c access.Caller) (Health, error) {
|
||||
if err := access.ManageInstance(c); err != nil {
|
||||
return Health{}, err
|
||||
}
|
||||
h := Health{
|
||||
Version: s.version, GoVersion: runtime.Version(), Started: s.started.UTC().Format(time.RFC3339),
|
||||
UptimeSec: int64(time.Since(s.started).Seconds()), Goroutines: runtime.NumGoroutine(),
|
||||
}
|
||||
var mem runtime.MemStats
|
||||
runtime.ReadMemStats(&mem)
|
||||
h.MemoryMB = mem.Sys >> 20
|
||||
start := time.Now()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
err := s.pb.Health(ctx)
|
||||
h.Database.LatencyMs = time.Since(start).Milliseconds()
|
||||
h.Database.OK = err == nil
|
||||
if err != nil {
|
||||
h.Database.Error = err.Error()
|
||||
}
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// Jobs lists the background jobs (superadmin).
|
||||
func (s *Service) Jobs(c access.Caller) ([]jobs.Job, error) {
|
||||
if err := access.ManageInstance(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.jobs.List(), nil
|
||||
}
|
||||
|
||||
// RunJob starts a job by hand (superadmin).
|
||||
func (s *Service) RunJob(c access.Caller, name string) error {
|
||||
if err := access.ManageInstance(c); err != nil {
|
||||
return err
|
||||
}
|
||||
switch err := s.jobs.Run(name); {
|
||||
case errors.Is(err, jobs.ErrUnknown):
|
||||
return apperr.NotFound("no job can be started with that name")
|
||||
case errors.Is(err, jobs.ErrBusy):
|
||||
return apperr.Conflict("the job is already running")
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Backups ---------------------------------------------------------------
|
||||
|
||||
// Backup is one PocketBase backup archive (the whole database and its files).
|
||||
type Backup struct {
|
||||
Key string `json:"key"`
|
||||
Size int64 `json:"size"`
|
||||
Modified string `json:"modified"`
|
||||
}
|
||||
|
||||
var backupKey = regexp.MustCompile(`^[a-z0-9_.-]{1,150}\.zip$`)
|
||||
|
||||
func backupError(err error) error {
|
||||
var pbErr *pocketbase.Error
|
||||
if errors.As(err, &pbErr) {
|
||||
switch pbErr.Status {
|
||||
case http.StatusNotFound:
|
||||
return apperr.NotFound("backup not found")
|
||||
case http.StatusBadRequest:
|
||||
return apperr.Invalid(pbErr.Message)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Service) ListBackups(ctx context.Context, c access.Caller) ([]Backup, error) {
|
||||
if err := access.ManageInstance(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := []Backup{}
|
||||
err := s.pb.Do(ctx, http.MethodGet, "/api/backups", nil, &out)
|
||||
return out, backupError(err)
|
||||
}
|
||||
|
||||
// CreateBackup asks PocketBase to write a new archive. It can take a while
|
||||
// on large instances; PocketBase answers when the archive is complete.
|
||||
func (s *Service) CreateBackup(ctx context.Context, c access.Caller) error {
|
||||
if err := access.ManageInstance(c); err != nil {
|
||||
return err
|
||||
}
|
||||
name := "docwell-" + time.Now().UTC().Format("20060102-150405") + ".zip"
|
||||
return backupError(s.pb.Do(ctx, http.MethodPost, "/api/backups", map[string]string{"name": name}, nil))
|
||||
}
|
||||
|
||||
func (s *Service) DeleteBackup(ctx context.Context, c access.Caller, key string) error {
|
||||
if err := access.ManageInstance(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if !backupKey.MatchString(key) {
|
||||
return apperr.NotFound("backup not found")
|
||||
}
|
||||
return backupError(s.pb.Do(ctx, http.MethodDelete, "/api/backups/"+key, nil, nil))
|
||||
}
|
||||
|
||||
// OpenBackup streams an archive; the caller closes the body.
|
||||
func (s *Service) OpenBackup(ctx context.Context, c access.Caller, key string) (*http.Response, error) {
|
||||
if err := access.ManageInstance(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !backupKey.MatchString(key) {
|
||||
return nil, apperr.NotFound("backup not found")
|
||||
}
|
||||
resp, err := s.pb.OpenBackup(ctx, key)
|
||||
return resp, backupError(err)
|
||||
}
|
||||
Reference in new issue
Block a user