feat(storage): add native PostgreSQL runtime and verified offline application import
Build and publish container images / images (push) Failing after 12m29s
Build and publish container images / images (push) Failing after 12m29s
This commit is contained in:
1 parent
96065d1e89
commit
0ba1dcfef0
243 files changed
+21441
-3090
No files matched your search
@@ -202,6 +202,9 @@ func (s *Server) adapterActionsForPath(r *http.Request, semanticPath string) ([]
|
||||
return plugins[i].DefaultEditor && !plugins[j].DefaultEditor
|
||||
})
|
||||
for _, plugin := range plugins {
|
||||
if plugin.Mode == "editor" && !s.store.SupportsHostedEditors() {
|
||||
continue
|
||||
}
|
||||
if pluginMatches(plugin, strings.ToLower(mediaType), filename) {
|
||||
actions = append(actions, adapterAction{
|
||||
ID: plugin.ID, Name: plugin.Name, Mode: plugin.Mode,
|
||||
@@ -341,6 +344,10 @@ func (s *Server) handleOpenWith(w http.ResponseWriter, r *http.Request) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if selected.Mode == "editor" && !s.store.SupportsHostedEditors() {
|
||||
http.Error(w, "hosted editors are unavailable on this database backend; use the built-in editor", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
actions, err := s.adapterActionsForPath(r, semanticPath)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 404)
|
||||
|
||||
@@ -100,6 +100,10 @@ func (s *Server) authorizeAdapterSession(r *http.Request, session *store.Adapter
|
||||
}
|
||||
|
||||
func (s *Server) handleAdapterSessionAPI(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.store.SupportsHostedEditors() {
|
||||
writeEventJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "hosted editors disabled for this database backend; use the built-in editor"})
|
||||
return
|
||||
}
|
||||
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/adapter/session"), "/")
|
||||
if action == "exchange" && r.Method == http.MethodPost {
|
||||
var input struct {
|
||||
|
||||
@@ -89,6 +89,7 @@ func (s *Server) handleSaveRevisionAPI(
|
||||
return
|
||||
}
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
actorUUID := ""
|
||||
if req.EditorPrincipal != nil {
|
||||
principal, e := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
|
||||
id := ""
|
||||
@@ -106,10 +107,14 @@ func (s *Server) handleSaveRevisionAPI(
|
||||
api.WriteError(w, http.StatusUnauthorized, "authentication_required", "authentication required")
|
||||
return
|
||||
}
|
||||
var revisionCount int
|
||||
_ = s.store.DB.QueryRow(`SELECT COUNT(*) FROM revisions WHERE object_uuid=?`, req.ObjectUUID).Scan(&revisionCount)
|
||||
actorUUID = principal.UUID
|
||||
hasRevision, revisionErr := s.store.ObjectHasRevision(r.Context(), req.ObjectUUID)
|
||||
if revisionErr != nil {
|
||||
api.WriteError(w, 500, "storage_unavailable", "revision state unavailable")
|
||||
return
|
||||
}
|
||||
capability := "write"
|
||||
if revisionCount == 0 {
|
||||
if !hasRevision {
|
||||
capability = "create"
|
||||
}
|
||||
allowed := false
|
||||
@@ -129,12 +134,7 @@ func (s *Server) handleSaveRevisionAPI(
|
||||
}
|
||||
}
|
||||
|
||||
var revision *store.Revision
|
||||
if req.BaseRevision != nil {
|
||||
revision, err = s.store.SaveRevisionContentIfHead(req.ObjectUUID, []byte(req.Body), req.Message, req.Language, req.ContentType, req.Filename, *req.BaseRevision)
|
||||
} else {
|
||||
revision, err = s.store.SaveRevisionContent(req.ObjectUUID, []byte(req.Body), req.Message, req.Language, req.ContentType, req.Filename)
|
||||
}
|
||||
revision, err := s.store.SaveRevisionContentInDomain(r.Context(), domainUUID, actorUUID, req.ObjectUUID, []byte(req.Body), req.Message, req.Language, req.ContentType, req.Filename, req.BaseRevision)
|
||||
if errors.Is(err, store.ErrRevisionConflict) {
|
||||
api.WriteError(w, http.StatusConflict, "revision_conflict", "document changed; reload the language head before saving")
|
||||
return
|
||||
|
||||
@@ -285,8 +285,7 @@ func (s *Server) verifyMagicLink(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
config, configErr := s.store.AuthProviderConfig(domainUUID, "smtp")
|
||||
var valid bool
|
||||
stateErr := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? WHERE p.uuid=? AND p.state IN ('active','readonly') AND m.state='active' AND p.email=?)`, domainUUID, principalUUID, payload.Email).Scan(&valid)
|
||||
valid, stateErr := s.store.ValidateMagicLinkIdentity(r.Context(), domainUUID, principalUUID, payload.Email)
|
||||
if configErr != nil || !config.Enabled || stateErr != nil || !valid || payload.Email == "" {
|
||||
http.Error(w, "This sign-in link is invalid or expired", http.StatusBadRequest)
|
||||
return
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -36,14 +36,7 @@ func (s *Server) calendars() (*calendar.Repository, error) {
|
||||
s.calendarErr = errors.New("calendar storage unavailable")
|
||||
return
|
||||
}
|
||||
s.calendarRepo, s.calendarErr = calendar.New(filepath.Join(s.store.DocumentRoot, "calendars"), s.store.DB)
|
||||
if s.calendarErr == nil {
|
||||
s.calendarRepo.PublicAllowed = func(domain string) bool {
|
||||
var allowed bool
|
||||
err := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM locality_publication_policies p JOIN domains d ON d.uuid=p.domain_uuid AND d.enabled=1 JOIN config c ON c.domain_uuid=p.domain_uuid AND c.key='authorization_mode' AND c.value='enforcing' WHERE p.domain_uuid=? AND p.node_kind='domain' AND p.node_uuid=p.domain_uuid AND p.policy='public')`, domain).Scan(&allowed)
|
||||
return err == nil && allowed
|
||||
}
|
||||
}
|
||||
s.calendarRepo, s.calendarErr = s.store.NewCalendarRepository(context.Background())
|
||||
})
|
||||
return s.calendarRepo, s.calendarErr
|
||||
}
|
||||
@@ -316,9 +309,8 @@ func (s *Server) handleCalendarAPI(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
id = p.UUID
|
||||
}
|
||||
var count int
|
||||
e := s.store.DB.QueryRowContext(r.Context(), `SELECT COUNT(*) FROM domain_memberships WHERE domain_uuid=? AND principal_uuid=? AND state='active'`, domain, id).Scan(&count)
|
||||
if e != nil || count != 1 {
|
||||
active, e := s.store.ActiveDomainMember(r.Context(), domain, id)
|
||||
if e != nil || !active {
|
||||
http.Error(w, "unknown collaborator", 400)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -161,14 +161,7 @@ func fetchCalendarSource(ctx context.Context, client *http.Client, c calendar.Ca
|
||||
return events, response.Header.Get("ETag"), response.Header.Get("Last-Modified"), false, e
|
||||
}
|
||||
func (s *Server) ensureCalendarSweep() error {
|
||||
var exists bool
|
||||
if e := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM event_definitions WHERE uuid=?)`, calendarSweepType).Scan(&exists); e != nil {
|
||||
return e
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
return s.store.SaveEventDefinition(&store.EventDefinition{UUID: calendarSweepType, Name: "Calendar source synchronization", EventType: calendarSweepType, EventKey: calendarSweepType, Payload: "{}", ScheduleKind: "interval", IntervalSeconds: 60, Enabled: true, Priority: -10, MaxAttempts: 1})
|
||||
return s.store.EnsureBuiltinEventDefinition(store.EventDefinition{UUID: calendarSweepType, Name: "Calendar source synchronization", EventType: calendarSweepType, EventKey: calendarSweepType, Payload: "{}", ScheduleKind: "interval", IntervalSeconds: 60, Enabled: true, Priority: -10, MaxAttempts: 1})
|
||||
}
|
||||
func (s *Server) enqueueCalendarSource(domain, id string, force bool) (bool, error) {
|
||||
b, _ := json.Marshal(calendarSyncPayload{Domain: domain, Calendar: id, Force: force})
|
||||
@@ -220,8 +213,7 @@ func (s *Server) runCalendarSource(job *store.EventJob) error {
|
||||
if !c.Source.Enabled || (!p.Force && c.Source.Next > time.Now().UnixMilli()) {
|
||||
return nil
|
||||
}
|
||||
var active bool
|
||||
e = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM domain_memberships m JOIN principals p ON p.uuid=m.principal_uuid JOIN domains d ON d.uuid=m.domain_uuid WHERE m.domain_uuid=? AND m.principal_uuid=? AND m.state='active' AND p.state='active' AND d.enabled=1)`, p.Domain, c.Owner).Scan(&active)
|
||||
active, e := s.store.ActiveDomainMember(context.Background(), p.Domain, c.Owner)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
type cityAutoPlanItem struct {
|
||||
@@ -46,15 +48,7 @@ func (s *Server) availableCityAutoName(domain, kind, parent, base, boundary stri
|
||||
}
|
||||
name = stem + "-" + hex.EncodeToString(sum[:8])
|
||||
}
|
||||
var exists bool
|
||||
var err error
|
||||
if kind == "spot" {
|
||||
err = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM spots WHERE domain_uuid=? AND name=? AND state!='archived')`, domain, name).Scan(&exists)
|
||||
} else if kind == "quartier" {
|
||||
err = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM zones WHERE domain_uuid=? AND kind='quartier' AND name=? AND state!='archived')`, domain, name).Scan(&exists)
|
||||
} else {
|
||||
err = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM zones WHERE domain_uuid=? AND parent_uuid=? AND name=? AND state!='archived')`, domain, parent, name).Scan(&exists)
|
||||
}
|
||||
exists, err := s.store.CityNameExists(domain, kind, parent, name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -66,19 +60,29 @@ func (s *Server) availableCityAutoName(domain, kind, parent, base, boundary stri
|
||||
}
|
||||
|
||||
func (s *Server) buildCityAutoPlan(domainUUID string) ([]cityAutoPlanItem, error) {
|
||||
rows, err := s.store.DB.Query(`SELECT p.path,p.object_uuid FROM paths p WHERE p.domain_uuid=? AND p.path LIKE '/%' AND p.path!='/u' AND p.path NOT LIKE '/u/%'
|
||||
AND NOT EXISTS(SELECT 1 FROM city_page_overrides ov JOIN spots dest ON dest.uuid=ov.spot_uuid AND dest.domain_uuid=ov.domain_uuid AND dest.state!='archived' WHERE ov.domain_uuid=p.domain_uuid AND ov.page_uuid=p.object_uuid)
|
||||
ORDER BY p.path`, domainUUID)
|
||||
candidates, err := s.store.CityAutoCandidates(domainUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []cityAutoPlanItem
|
||||
for rows.Next() {
|
||||
var semanticPath, pageUUID string
|
||||
if err := rows.Scan(&semanticPath, &pageUUID); err != nil {
|
||||
return nil, err
|
||||
// Many Pages share one boundary. Resolve each boundary once, without
|
||||
// retaining a database cursor while making nested repository calls.
|
||||
type lookupResult struct {
|
||||
node store.CityNodeDetail
|
||||
err error
|
||||
}
|
||||
cache := map[string]lookupResult{}
|
||||
lookup := func(kind, parent, boundary string) (store.CityNodeDetail, error) {
|
||||
key := kind + "\x00" + parent + "\x00" + boundary
|
||||
if r, ok := cache[key]; ok {
|
||||
return r.node, r.err
|
||||
}
|
||||
node, err := s.store.CityNodeOverlappingPath(domainUUID, kind, parent, boundary)
|
||||
cache[key] = lookupResult{node, err}
|
||||
return node, err
|
||||
}
|
||||
for _, candidate := range candidates {
|
||||
semanticPath, pageUUID := candidate.Path, candidate.ObjectUUID
|
||||
parts := strings.Split(strings.Trim(semanticPath, "/"), "/")
|
||||
if len(parts) < 3 || parts[0] == "system" {
|
||||
continue
|
||||
@@ -93,19 +97,25 @@ func (s *Server) buildCityAutoPlan(domainUUID string) ([]cityAutoPlanItem, error
|
||||
item.SpotPath = item.StreetPath + "/_undefined"
|
||||
}
|
||||
item.Action = "create"
|
||||
if q, e := s.store.CityNodeOverlappingPath(domainUUID, "quartier", "", item.QuartierPath); e == nil {
|
||||
if q, e := lookup("quartier", "", item.QuartierPath); e == nil {
|
||||
item.Quartier = q.Name
|
||||
item.Action = "merge"
|
||||
if st, e := s.store.CityNodeOverlappingPath(domainUUID, "street", q.UUID, item.StreetPath); e == nil {
|
||||
if st, e := lookup("street", q.UUID, item.StreetPath); e == nil {
|
||||
item.Street = st.Name
|
||||
if spot, e := s.store.CityNodeOverlappingPath(domainUUID, "spot", st.UUID, item.SpotPath); e == nil {
|
||||
if spot, e := lookup("spot", st.UUID, item.SpotPath); e == nil {
|
||||
item.Spot = spot.Name
|
||||
} else if !errors.Is(e, sql.ErrNoRows) {
|
||||
return nil, e
|
||||
}
|
||||
} else if !errors.Is(e, sql.ErrNoRows) {
|
||||
return nil, e
|
||||
}
|
||||
} else if !errors.Is(e, sql.ErrNoRows) {
|
||||
return nil, e
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
return out, rows.Err()
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *Server) applyCityAutoPlan(domainUUID, actor string) (int, error) {
|
||||
|
||||
@@ -40,7 +40,7 @@ func (s *Server) handleCityGovernance(w http.ResponseWriter, r *http.Request) {
|
||||
kind := map[string]string{"spots": "spot", "domains": "domain"}[p[0]]
|
||||
var err error
|
||||
if p[0] == "zones" {
|
||||
err = s.store.DB.QueryRowContext(r.Context(), `SELECT kind FROM zones WHERE uuid=? AND domain_uuid=?`, p[1], domain).Scan(&kind)
|
||||
kind, err = s.store.CityZoneKind(r.Context(), domain, p[1])
|
||||
}
|
||||
var result any
|
||||
if err == nil && automatic {
|
||||
|
||||
@@ -131,14 +131,17 @@ func (s *Server) handleConfig(
|
||||
campusAdminRequired := strings.HasPrefix(section, "campus-") || strings.HasPrefix(section, "domain-") || section == "instance-status"
|
||||
if campusAdminRequired {
|
||||
targetCampusUUID := currentDomain.CampusUUID
|
||||
var targetErr error
|
||||
if section == "domain-create" {
|
||||
targetCampusUUID = r.FormValue("domain_campus_uuid")
|
||||
} else if section == "domain-update" || section == "domain-delete" || section == "domain-entry" || section == "domain-projection" {
|
||||
_ = s.store.DB.QueryRow(`SELECT campus_uuid FROM domains WHERE id=?`, r.FormValue("domain_id")).Scan(&targetCampusUUID)
|
||||
id, _ := strconv.ParseInt(r.FormValue("domain_id"), 10, 64)
|
||||
targetCampusUUID, targetErr = s.store.ConfigTargetCampus(r.Context(), true, id)
|
||||
} else if section != "campus-create" && r.FormValue("campus_id") != "" {
|
||||
_ = s.store.DB.QueryRow(`SELECT uuid FROM campuses WHERE id=?`, r.FormValue("campus_id")).Scan(&targetCampusUUID)
|
||||
id, _ := strconv.ParseInt(r.FormValue("campus_id"), 10, 64)
|
||||
targetCampusUUID, targetErr = s.store.ConfigTargetCampus(r.Context(), false, id)
|
||||
}
|
||||
if !s.store.IsCampusAdmin(targetCampusUUID, principal.UUID) {
|
||||
if targetErr != nil || !s.store.IsCampusAdmin(targetCampusUUID, principal.UUID) {
|
||||
http.Error(w, "campus admin required", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
@@ -824,6 +827,10 @@ func (s *Server) handleConfig(
|
||||
}
|
||||
|
||||
case "zombie-purge":
|
||||
if !s.store.SupportsZombieGC() {
|
||||
http.Error(w, store.ErrBackendFeatureUnavailable.Error(), http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = s.store.PurgeZombieObjectsInDomain(
|
||||
r.Context(), domainUUID,
|
||||
@@ -918,7 +925,7 @@ func (s *Server) handleConfig(
|
||||
s.getZombieGraceHours(s.domainUUIDForRequest(r))
|
||||
|
||||
var zombies []store.ZombieObject
|
||||
if r.URL.Query().Get("panel") == "objects" {
|
||||
if r.URL.Query().Get("panel") == "objects" && s.store.SupportsZombieGC() {
|
||||
zombies, err =
|
||||
s.store.ListZombieObjectsInDomain(
|
||||
r.Context(), s.domainUUIDForRequest(r),
|
||||
|
||||
@@ -88,12 +88,13 @@ func (s *Server) domainForRequestWithError(
|
||||
return s.currentDomain(), nil
|
||||
}
|
||||
if cached, ok := r.Context().Value(requestDomainKey{}).(*requestDomainLookup); ok && cached.host == r.Host {
|
||||
cached.once.Do(func() { cached.domain, cached.err = s.store.GetDomainByHost(r.Host) })
|
||||
cached.once.Do(func() { cached.domain, cached.err = s.store.GetDomainByHostContext(r.Context(), r.Host) })
|
||||
return cached.domain, cached.err
|
||||
}
|
||||
|
||||
sp, err :=
|
||||
s.store.GetDomainByHost(
|
||||
s.store.GetDomainByHostContext(
|
||||
r.Context(),
|
||||
r.Host,
|
||||
)
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ func (q schedulerQueue) Started(ctx context.Context, job *dregarnuhr.Job) error
|
||||
return q.store.AddEventLogContext(ctx, job.UUID, job.EventUUID, "info", "execution started")
|
||||
}
|
||||
func (q schedulerQueue) Finish(ctx context.Context, job *dregarnuhr.Job, err error, now time.Time) error {
|
||||
return q.store.FinishEventAt(ctx, job.UUID, err, now)
|
||||
return q.store.FinishClaimedEventAt(ctx, job, err, now)
|
||||
}
|
||||
|
||||
func (s *Server) eventRunner() *dregarnuhr.Runner {
|
||||
@@ -112,6 +112,9 @@ func (s *Server) schedulerLoop() dregarnuhr.Loop {
|
||||
{Name: "initial-preview-sweep", After: 5 * time.Second, Run: func(context.Context, time.Time) error { s.enqueueAllPreviewEvents(); return nil }},
|
||||
{Name: "preview-sweep", Every: 15 * time.Minute, Run: func(context.Context, time.Time) error { s.enqueueAllPreviewEvents(); return nil }},
|
||||
{Name: "history-cleanup", Every: time.Hour, Run: func(context.Context, time.Time) error {
|
||||
if !s.store.SupportsEventHistoryMaintenance() {
|
||||
return nil
|
||||
}
|
||||
settings, err := s.store.GetQueueSettings()
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -54,39 +54,56 @@ func (s *Server) initializePreviewScheduler() {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) startEventScheduler() {
|
||||
func (s *Server) startEventScheduler() error {
|
||||
if s.store == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
release, err := s.store.AcquireEventWorker(s.backgroundCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ready := false
|
||||
defer func() {
|
||||
if !ready {
|
||||
_ = release()
|
||||
}
|
||||
}()
|
||||
if err := s.store.EnsureFeedEvent(); err != nil {
|
||||
log.Printf("feed scheduler initialization failed")
|
||||
return fmt.Errorf("feed scheduler initialization: %w", err)
|
||||
}
|
||||
if err := s.store.EnsureBuiltinResourceEvent(); err != nil {
|
||||
log.Printf("resource monitor initialization failed: %v", err)
|
||||
return fmt.Errorf("resource monitor initialization: %w", err)
|
||||
}
|
||||
if err := s.ensureCalendarSweep(); err != nil {
|
||||
log.Printf("calendar scheduler initialization failed: %v", err)
|
||||
return fmt.Errorf("calendar scheduler initialization: %w", err)
|
||||
}
|
||||
if err := s.store.EnsureTaskActivityBridgeEvent(); err != nil {
|
||||
log.Printf("task journal bridge initialization failed: %v", err)
|
||||
return fmt.Errorf("task journal bridge initialization: %w", err)
|
||||
}
|
||||
if err := s.store.EnsureTaskReminderEvent(); err != nil {
|
||||
log.Printf("task reminder initialization failed: %v", err)
|
||||
return fmt.Errorf("task reminder initialization: %w", err)
|
||||
}
|
||||
if err := s.store.EnsureTaskFocusEvent(); err != nil {
|
||||
log.Printf("focus timer initialization failed: %v", err)
|
||||
return fmt.Errorf("focus timer initialization: %w", err)
|
||||
}
|
||||
if err := s.store.EnsureTaskDigestEvent(); err != nil {
|
||||
log.Printf("task digest initialization failed: %v", err)
|
||||
return fmt.Errorf("task digest initialization: %w", err)
|
||||
}
|
||||
if err := s.store.EnsureNotificationPushEvent(); err != nil {
|
||||
log.Printf("notification push initialization failed: %v", err)
|
||||
if s.store.SupportsNotificationPush() {
|
||||
if err := s.store.EnsureNotificationPushEvent(); err != nil {
|
||||
return fmt.Errorf("notification push initialization: %w", err)
|
||||
}
|
||||
}
|
||||
s.workerRelease = release
|
||||
ready = true
|
||||
s.backgroundWG.Add(1)
|
||||
go func() {
|
||||
if err := s.schedulerLoop().Run(context.Background()); err != nil {
|
||||
defer s.backgroundWG.Done()
|
||||
if err := s.schedulerLoop().Run(s.backgroundCtx); err != nil && !errors.Is(err, context.Canceled) {
|
||||
log.Printf("event scheduler loop stopped: %v", err)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) wakeEventScheduler() {
|
||||
|
||||
@@ -29,6 +29,10 @@ func zombieAPIOptions(r *http.Request) (int64, int, error) {
|
||||
}
|
||||
|
||||
func (s *Server) handleZombieListAPI(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.store.SupportsZombieGC() {
|
||||
http.Error(w, store.ErrBackendFeatureUnavailable.Error(), http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", http.MethodGet)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -50,6 +54,10 @@ func (s *Server) handleZombieListAPI(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (s *Server) handleZombiePurgeAPI(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.store.SupportsZombieGC() {
|
||||
http.Error(w, store.ErrBackendFeatureUnavailable.Error(), http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", http.MethodPost)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
|
||||
@@ -41,13 +41,18 @@ func (s *Server) zombieMaintenanceGrace() (int64, error) {
|
||||
}
|
||||
|
||||
func (s *Server) startGCWorker() {
|
||||
if !s.store.SupportsZombieGC() {
|
||||
return
|
||||
}
|
||||
grace, err := s.zombieMaintenanceGrace()
|
||||
if err != nil {
|
||||
log.Printf("zombie GC maintenance disabled: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
s.backgroundWG.Add(1)
|
||||
go func() {
|
||||
defer s.backgroundWG.Done()
|
||||
|
||||
ticker :=
|
||||
time.NewTicker(
|
||||
@@ -57,7 +62,11 @@ func (s *Server) startGCWorker() {
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
<-ticker.C
|
||||
select {
|
||||
case <-s.backgroundCtx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
}
|
||||
|
||||
deleted, err :=
|
||||
s.store.PurgeZombieObjects(
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"archive/tar"
|
||||
"archive/zip"
|
||||
"compress/gzip"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -597,10 +598,6 @@ func (s *Server) rollbackImport(domainUUID, sessionUUID string) error {
|
||||
if session.Status != "completed" && session.Status != "failed" {
|
||||
return errors.New("only a completed or failed import can be rolled back")
|
||||
}
|
||||
entries, err := s.store.ImportJournal(sessionUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
actor := session.ActorUUID
|
||||
if actor == "" {
|
||||
actor = "system:import"
|
||||
@@ -610,32 +607,20 @@ func (s *Server) rollbackImport(domainUUID, sessionUUID string) error {
|
||||
return err
|
||||
}
|
||||
for _, edge := range edges {
|
||||
_ = s.store.RemoveSpotPage(domainUUID, actor, edge.SpotUUID, edge.PageUUID, "primary")
|
||||
if err := s.store.RemoveSpotPage(domainUUID, actor, edge.SpotUUID, edge.PageUUID, "primary"); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
nodes, err := s.store.ImportCityJournal(sessionUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, node := range nodes {
|
||||
_ = s.store.DeleteCityNode(domainUUID, actor, node.Kind, node.UUID)
|
||||
}
|
||||
tx, err := s.store.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
for _, x := range entries {
|
||||
if _, err = tx.Exec(`DELETE FROM paths WHERE domain_uuid=? AND path=? AND object_uuid=?`, domainUUID, x.Path, x.ObjectUUID); err != nil {
|
||||
if err := s.store.DeleteCityNode(domainUUID, actor, node.Kind, node.UUID); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(`UPDATE import_sessions SET status='rolled_back',updated_at_ms=? WHERE uuid=?`, time.Now().UnixMilli(), sessionUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = tx.Commit(); err == nil {
|
||||
s.store.InvalidatePathCaches()
|
||||
}
|
||||
return err
|
||||
return s.store.RollbackImportPaths(domainUUID, sessionUUID)
|
||||
}
|
||||
|
||||
func readImportArchive(filename, name string, wanted map[string]bool) (map[string][]byte, error) {
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Close waits for the scheduler loop, maintenance and all running handlers
|
||||
// before releasing database worker ownership. Deadline expiry leaves ownership
|
||||
// held until the in-flight work actually exits; callers must not close the DB.
|
||||
func (s *Server) Close(ctx context.Context) error {
|
||||
s.closeOnce.Do(func() {
|
||||
s.backgroundCancel()
|
||||
go func() {
|
||||
_ = s.eventRunner().Stop(context.Background())
|
||||
s.backgroundWG.Wait()
|
||||
if s.workerRelease != nil {
|
||||
s.closeErr = s.workerRelease()
|
||||
}
|
||||
close(s.closeDone)
|
||||
}()
|
||||
})
|
||||
select {
|
||||
case <-s.closeDone:
|
||||
return s.closeErr
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// RunContext drains HTTP before releasing worker ownership. If a request or
|
||||
// worker cannot stop in time, return an error without pretending it has exited.
|
||||
func (s *Server) RunContext(ctx context.Context, addr string) error {
|
||||
server := &http.Server{Addr: addr, Handler: s.handler(), ReadHeaderTimeout: 10 * time.Second}
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- server.ListenAndServe() }()
|
||||
var serveErr error
|
||||
select {
|
||||
case serveErr = <-done:
|
||||
case <-ctx.Done():
|
||||
shutdown, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
serveErr = server.Shutdown(shutdown)
|
||||
cancel()
|
||||
if serveErr != nil {
|
||||
_ = server.Close()
|
||||
}
|
||||
}
|
||||
closeCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
closeErr := s.Close(closeCtx)
|
||||
if errors.Is(serveErr, http.ErrServerClosed) {
|
||||
serveErr = nil
|
||||
}
|
||||
return errors.Join(serveErr, closeErr)
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/dregarnuhr"
|
||||
"pageswiki/internal/store"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func lifecycleFixture(t *testing.T) *store.Store {
|
||||
t.Helper()
|
||||
t.Chdir("../..")
|
||||
database, err := db.Open(t.TempDir() + "/pages.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { database.Close() })
|
||||
if err = db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := store.NewWithDocumentRoot(database, t.TempDir()+"/documents")
|
||||
if err = s.EnsureConfigTable(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func TestServerCloseRetainsOwnershipUntilHandlersExit(t *testing.T) {
|
||||
st := lifecycleFixture(t)
|
||||
st.BackgroundJobsDisabled = true
|
||||
s, err := NewChecked(st)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Admission is driven manually: avoid unrelated timed triggers in this test.
|
||||
st.BackgroundJobsDisabled = false
|
||||
released := make(chan struct{})
|
||||
s.workerRelease = func() error { close(released); return nil }
|
||||
started := make(chan struct{})
|
||||
cancelled := make(chan struct{})
|
||||
exit := make(chan struct{})
|
||||
if err = s.registeredEventHandlers().Register("synthetic.stop", func(ctx context.Context, _ *dregarnuhr.Job) error {
|
||||
close(started)
|
||||
<-ctx.Done()
|
||||
close(cancelled)
|
||||
<-exit
|
||||
return ctx.Err()
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = st.EnqueueEvent("synthetic.stop", "one", "", time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.eventRunner().Dispatch(s.backgroundCtx)
|
||||
select {
|
||||
case <-started:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("handler not started")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if err = s.Close(ctx); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-cancelled:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("handler not cancelled")
|
||||
}
|
||||
select {
|
||||
case <-released:
|
||||
t.Fatal("ownership released while handler lives")
|
||||
default:
|
||||
}
|
||||
close(exit)
|
||||
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err = s.Close(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-released:
|
||||
default:
|
||||
t.Fatal("ownership retained after stop")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerRefusesSchedulerInitializationFailure(t *testing.T) {
|
||||
st := lifecycleFixture(t)
|
||||
if _, err := st.DB.Exec(`DROP TABLE event_definitions`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s, err := NewChecked(st)
|
||||
if err == nil || s != nil {
|
||||
t.Fatal("partial server accepted", err)
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package server
|
||||
|
||||
import "pageswiki/internal/mestionora"
|
||||
import "pageswiki/internal/store"
|
||||
|
||||
// Construction is read-only: schema/index maintenance remains an explicit
|
||||
// application startup operation, never a search request side effect.
|
||||
func (s *Server) searchEngine() *mestionora.SQLite {
|
||||
return mestionora.NewSQLite(s.store.DB)
|
||||
func (s *Server) searchEngine() *store.Store {
|
||||
return s.store
|
||||
}
|
||||
@@ -25,6 +25,9 @@ func validPushSubscription(sub webpush.Subscription) bool {
|
||||
}
|
||||
|
||||
func (s *Server) notificationPushKeys(ctx context.Context) (private, public string, err error) {
|
||||
if !s.store.SupportsNotificationPush() {
|
||||
return "", "", store.ErrBackendFeatureUnavailable
|
||||
}
|
||||
err = s.store.DB.QueryRowContext(ctx, `SELECT private_key,public_key FROM notification_push_keys WHERE id=1`).Scan(&private, &public)
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
return
|
||||
@@ -42,6 +45,10 @@ func (s *Server) notificationPushKeys(ctx context.Context) (private, public stri
|
||||
}
|
||||
|
||||
func (s *Server) handleNotificationPush(w http.ResponseWriter, r *http.Request, domain, owner string) {
|
||||
if !s.store.SupportsNotificationPush() {
|
||||
writeEventJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "push disabled for this database backend"})
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
switch r.Method {
|
||||
|
||||
@@ -34,6 +34,12 @@ func notificationDeliveryClient() *http.Client {
|
||||
}
|
||||
|
||||
func (s *Server) deliverNotificationPushContext(ctx context.Context, job *store.EventJob) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if !s.store.SupportsNotificationPush() {
|
||||
return store.ErrBackendFeatureUnavailable
|
||||
}
|
||||
dispatcher := ordoschnellic.Dispatcher{
|
||||
Queue: notificationDeliveryQueue{s.store},
|
||||
Sender: ordoschnellic.WebPush{Keys: s.notificationPushKeys, Client: notificationDeliveryClient()},
|
||||
|
||||
@@ -19,22 +19,11 @@ func (s *Server) periodicalShareTargets(r *http.Request, kind, path string) []pe
|
||||
defer cancel()
|
||||
r = r.WithContext(ctx)
|
||||
source := s.domainUUIDForRequest(r)
|
||||
rows, err := s.store.DB.QueryContext(r.Context(), `SELECT domain_uuid FROM periodical_projections WHERE source_uuid=? AND kind=? ORDER BY domain_uuid LIMIT 65`, source, kind)
|
||||
ids, err := s.store.PeriodicalProjectionTargets(r.Context(), source, kind)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
ids := []string{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err = rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return nil
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil || len(ids) > 64 {
|
||||
if len(ids) > 64 {
|
||||
return nil
|
||||
}
|
||||
out := []periodicalShareTarget{}
|
||||
|
||||
@@ -26,9 +26,7 @@ func (s *Server) appearanceForRequest(r *http.Request) personalAppearance {
|
||||
if principal == nil {
|
||||
return p
|
||||
}
|
||||
var raw string
|
||||
var version int64
|
||||
if s.store.DB.QueryRowContext(r.Context(), `SELECT version,data FROM personal_appearance WHERE domain_uuid=? AND owner_uuid=?`, domain, principal.UUID).Scan(&version, &raw) == nil {
|
||||
if version, raw, err := s.store.PersonalAppearance(r.Context(), domain, principal.UUID); err == nil {
|
||||
var saved personalAppearance
|
||||
if json.Unmarshal([]byte(raw), &saved) == nil {
|
||||
saved.Version = version
|
||||
@@ -93,20 +91,7 @@ func (s *Server) savePersonalAppearance(w http.ResponseWriter, r *http.Request,
|
||||
}
|
||||
p.Version = version + 1
|
||||
raw, _ := json.Marshal(p)
|
||||
result, err := s.store.DB.ExecContext(r.Context(), `INSERT INTO personal_appearance(domain_uuid,owner_uuid,version,data) SELECT ?,?,?,? WHERE ?=0 ON CONFLICT(domain_uuid,owner_uuid) DO NOTHING`, domain, actor, p.Version, string(raw), version)
|
||||
if err == nil {
|
||||
var n int64
|
||||
n, err = result.RowsAffected()
|
||||
if err == nil && n == 0 {
|
||||
result, err = s.store.DB.ExecContext(r.Context(), `UPDATE personal_appearance SET version=?,data=? WHERE domain_uuid=? AND owner_uuid=? AND version=?`, p.Version, string(raw), domain, actor, version)
|
||||
if err == nil {
|
||||
n, err = result.RowsAffected()
|
||||
if n == 0 {
|
||||
err = sql.ErrNoRows
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
err = s.store.SavePersonalAppearance(r.Context(), domain, actor, version, string(raw))
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
http.Error(w, "Settings changed elsewhere; reload before saving.", 409)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
const nativeTestDomain = "35ff2304-a401-49ba-b21a-6129d4be2af2"
|
||||
|
||||
func nativeRuntimeFixture(t *testing.T) *store.Store {
|
||||
t.Helper()
|
||||
dsn := os.Getenv("PAGESWIKI_TEST_POSTGRES_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("set PAGESWIKI_TEST_POSTGRES_DSN to a disposable PostgreSQL URL")
|
||||
}
|
||||
u, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
t.Fatal("invalid test DSN")
|
||||
}
|
||||
ctx := context.Background()
|
||||
admin, err := db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: dsn, AllowInsecure: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
schema := "pageswiki_http_test_" + strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
if _, err = admin.Exec(`CREATE SCHEMA "` + schema + `"`); err != nil {
|
||||
admin.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
defer admin.Close()
|
||||
if _, err := admin.Exec(`DROP SCHEMA "` + schema + `" CASCADE`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
q := u.Query()
|
||||
q.Set("search_path", schema)
|
||||
u.RawQuery = q.Encode()
|
||||
database, err := db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: u.String(), AllowInsecure: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { database.Close() })
|
||||
root := filepath.Join(t.TempDir(), "documents")
|
||||
if err = os.Mkdir(root, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PAGESWIKI_AUTH_KEY", "synthetic runtime key")
|
||||
// Initial bootstrap creates schema but refuses to invent namespace IDs.
|
||||
if _, err = store.NewPostgreSQLWithDocumentRoot(ctx, database, root); err == nil {
|
||||
t.Fatal("unprovisioned namespace accepted")
|
||||
}
|
||||
if _, err = database.Exec(`INSERT INTO campuses(uuid,name,title,created_at) VALUES('campus','default','Test Campus',1);
|
||||
INSERT INTO domains(uuid,name,title,created_at,campus_uuid) VALUES('35ff2304-a401-49ba-b21a-6129d4be2af2','default','Test Domain',1,'campus')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s, err := store.NewPostgreSQLWithDocumentRoot(ctx, database, root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Chdir("../..")
|
||||
return s
|
||||
}
|
||||
|
||||
func TestPostgreSQLRuntimeHTTPAndRestart(t *testing.T) {
|
||||
st := nativeRuntimeFixture(t)
|
||||
ctx := context.Background()
|
||||
owner, err := st.BootstrapOwner(nativeTestDomain, "owner", "synthetic runtime password", "Owner", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err := st.CreateAuthSession(nativeTestDomain, owner.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = st.SetConfigInDomain(nativeTestDomain, "authorization_mode", "enforcing"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
obj, err := st.CreatePageAtPathInDomain(nativeTestDomain, "/native-startup")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = st.SaveRevisionContentInDomain(ctx, nativeTestDomain, owner.UUID, obj, []byte("<h1>Native startup page</h1><p>Canonical document</p>"), "initial", "en", "text/html", "", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for iteration := 0; iteration < 2; iteration++ {
|
||||
if iteration > 0 {
|
||||
st, err = store.NewPostgreSQLWithDocumentRoot(ctx, st.DB, st.DocumentRoot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
s, err := NewChecked(st)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
closeServer := func() {
|
||||
closeCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
if err := s.Close(closeCtx); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
t.Cleanup(closeServer)
|
||||
listener := httptest.NewServer(s.handler())
|
||||
request, _ := http.NewRequest(http.MethodGet, listener.URL+"/native-startup", nil)
|
||||
request.Host = "localhost"
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
response, err := listener.Client().Do(request)
|
||||
if err != nil {
|
||||
listener.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := io.ReadAll(response.Body)
|
||||
response.Body.Close()
|
||||
listener.Close()
|
||||
if err != nil || response.StatusCode != 200 || !bytes.Contains(body, []byte("Canonical document")) {
|
||||
t.Fatalf("listener roundtrip: %d %v", response.StatusCode, err)
|
||||
}
|
||||
job := &store.EventJob{UUID: "synthetic-runtime-resource", Type: store.ResourceEventType}
|
||||
for range 2 {
|
||||
if err := s.runEvent(job); err != nil {
|
||||
t.Fatal("resource worker", err)
|
||||
}
|
||||
}
|
||||
var samples int
|
||||
if err := st.DB.QueryRow(`SELECT count(*) FROM resource_samples WHERE job_uuid='synthetic-runtime-resource'`).Scan(&samples); err != nil || samples != 1 {
|
||||
t.Fatal("resource deduplication", samples, err)
|
||||
}
|
||||
for _, target := range []string{"/health", "/", "/native-startup", "/e/-/native-startup", "/v/search?q=native-startup", "/_/api/v1/search/suggest?q=native", "/-/sys/config?panel=auth", "/v/tree", "/v/browser", "/v/plan/tasks", "/v/plan/cal/2026/11", "/v/plan/contacts", "/v/plan/dashboard", "/v/feeds"} {
|
||||
r := httptest.NewRequest(http.MethodGet, "http://localhost"+target, nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
s.handler().ServeHTTP(w, r)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("iteration %d %s: HTTP %d %.400s", iteration, target, w.Code, w.Body.String())
|
||||
}
|
||||
if strings.HasPrefix(target, "/v/search") && w.Header().Get("X-PagesWiki-Search-Profile") != "metadata-only" {
|
||||
t.Error("SQLite search selected")
|
||||
}
|
||||
if target == "/native-startup" && !strings.Contains(w.Body.String(), "Canonical document") {
|
||||
t.Error("page body missing")
|
||||
}
|
||||
}
|
||||
base, err := st.GetRevisionForExactLanguage(obj, "en")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload, _ := json.Marshal(map[string]any{"object_uuid": obj, "body": "<h1>Native startup page</h1><p>Canonical document updated</p>", "lang": "en", "content_type": "text/html", "base_revision_uuid": base.UUID, "editor_principal_uuid": owner.UUID})
|
||||
save := func(auth string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(http.MethodPost, "http://localhost/_/api/v1/revisions/save", bytes.NewReader(payload))
|
||||
r.Header.Set("Authorization", "Bearer "+auth)
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
s.handler().ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
if w := save(token); w.Code != 200 {
|
||||
t.Fatalf("save: %d %s", w.Code, w.Body)
|
||||
}
|
||||
if w := save(token); w.Code != 409 {
|
||||
t.Fatalf("stale save: %d %s", w.Code, w.Body)
|
||||
}
|
||||
if w := save("expired"); w.Code != 401 {
|
||||
t.Fatalf("expired save: %d %s", w.Code, w.Body)
|
||||
}
|
||||
for _, target := range []string{calendarDAV, taskDAVCollection, contactDAV} {
|
||||
r := httptest.NewRequest("PROPFIND", "http://localhost"+target, strings.NewReader(`<d:propfind xmlns:d="DAV:"><d:allprop/></d:propfind>`))
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
r.Header.Set("Depth", "1")
|
||||
r.Header.Set("Content-Type", "application/xml")
|
||||
w := httptest.NewRecorder()
|
||||
s.handler().ServeHTTP(w, r)
|
||||
if w.Code != 207 {
|
||||
t.Errorf("DAV %s: %d %.300s", target, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
for _, panel := range []string{"objects", "assets", "auth-providers", "auth-sessions", "groups", "campuses", "domains", "scheduler", "logs", "queue", "organizer"} {
|
||||
r := httptest.NewRequest("GET", "http://localhost/-/sys/config?panel="+panel, nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
s.handler().ServeHTTP(w, r)
|
||||
if w.Code != 200 {
|
||||
t.Errorf("panel %s: %d %.300s", panel, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
// Another Server must not take ownership while this worker is alive.
|
||||
if peer, e := NewChecked(st); e == nil {
|
||||
peer.Close(ctx)
|
||||
t.Error("duplicate worker accepted")
|
||||
}
|
||||
if iteration == 1 {
|
||||
if err := st.RevokeAuthSession(token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w := save(token); w.Code != 401 {
|
||||
t.Errorf("revoked session accepted: %d", w.Code)
|
||||
}
|
||||
r := httptest.NewRequest("GET", "http://localhost/native-startup", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
s.handler().ServeHTTP(w, r)
|
||||
if w.Code == 200 || strings.Contains(w.Body.String(), "Canonical document") {
|
||||
t.Error("revoked session exposed private page")
|
||||
}
|
||||
}
|
||||
closeServer()
|
||||
}
|
||||
}
|
||||
@@ -38,16 +38,11 @@ func (s *Server) handleRestoreRevisionAPI(w http.ResponseWriter, r *http.Request
|
||||
http.Error(w, "forbidden", 403)
|
||||
return
|
||||
}
|
||||
revision, err := s.store.GetRevision(input.Revision)
|
||||
if err != nil {
|
||||
http.Error(w, "revision unavailable", 500)
|
||||
return
|
||||
}
|
||||
message := strings.TrimSpace(input.Message)
|
||||
if message == "" {
|
||||
message = "Restore revision " + revision.UUID
|
||||
message = "Restore revision " + metadata.UUID
|
||||
}
|
||||
saved, err := s.store.SaveRevisionContentIfHead(revision.ObjectUUID, []byte(revision.Body), message, revision.Language, revision.ContentType, revision.Filename, input.Expected)
|
||||
saved, err := s.store.RestoreRevisionInDomain(r.Context(), s.domainUUIDForRequest(r), "", input.Revision, input.Expected, message)
|
||||
if errors.Is(err, store.ErrRevisionConflict) {
|
||||
http.Error(w, "revision changed; reload history", 412)
|
||||
return
|
||||
@@ -57,5 +52,5 @@ func (s *Server) handleRestoreRevisionAPI(w http.ResponseWriter, r *http.Request
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
writeEventJSON(w, 200, map[string]string{"revision_uuid": saved.UUID, "object_uuid": revision.ObjectUUID})
|
||||
writeEventJSON(w, 200, map[string]string{"revision_uuid": saved.UUID, "object_uuid": saved.ObjectUUID})
|
||||
}
|
||||
@@ -121,6 +121,7 @@ func searchExcerpt(raw, contentType, query string) string {
|
||||
}
|
||||
|
||||
func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-PagesWiki-Search-Profile", s.store.SearchProfile())
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
w.Header().Set("Allow", "GET, HEAD")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
|
||||
@@ -86,6 +86,7 @@ func citySuggestion(result store.CitySearchResult, query string) searchSuggestio
|
||||
}
|
||||
|
||||
func (s *Server) handleSearchSuggestAPI(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-PagesWiki-Search-Profile", s.store.SearchProfile())
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", http.MethodGet)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -44,13 +45,32 @@ type Server struct {
|
||||
schedulerRunner *dregarnuhr.Runner
|
||||
eventHandlersOnce sync.Once
|
||||
eventHandlers *dregarnuhr.Registry
|
||||
backgroundCtx context.Context
|
||||
backgroundCancel context.CancelFunc
|
||||
backgroundWG sync.WaitGroup
|
||||
workerRelease func() error
|
||||
closeOnce sync.Once
|
||||
closeDone chan struct{}
|
||||
closeErr error
|
||||
}
|
||||
|
||||
func New(
|
||||
st *store.Store,
|
||||
) *Server {
|
||||
s, err := NewChecked(st)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// NewChecked refuses to serve when mandatory scheduler initialization fails.
|
||||
// New remains a compatibility wrapper for existing constructors and tests.
|
||||
func NewChecked(st *store.Store) (*Server, error) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
s := &Server{
|
||||
backgroundCtx: ctx, backgroundCancel: cancel, closeDone: make(chan struct{}),
|
||||
mux: http.NewServeMux(),
|
||||
store: st,
|
||||
previewSlots: make(chan struct{}, 1),
|
||||
@@ -111,11 +131,14 @@ func New(
|
||||
s.registerRoutes()
|
||||
|
||||
if !st.ReadOnly && !st.BackgroundJobsDisabled {
|
||||
if err := s.startEventScheduler(); err != nil {
|
||||
cancel()
|
||||
return nil, err
|
||||
}
|
||||
s.startGCWorker()
|
||||
s.startEventScheduler()
|
||||
}
|
||||
|
||||
return s
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Server) registerStaticRoutes() {
|
||||
@@ -378,8 +401,5 @@ func (s *Server) Run(addr string) error {
|
||||
addr,
|
||||
)
|
||||
|
||||
return http.ListenAndServe(
|
||||
addr,
|
||||
s.handler(),
|
||||
)
|
||||
return s.RunContext(context.Background(), addr)
|
||||
}
|
||||
@@ -193,18 +193,10 @@ func (s *Server) taskPagePath(r *http.Request, id string) string {
|
||||
if id == "" {
|
||||
return ""
|
||||
}
|
||||
rows, err := s.store.DB.QueryContext(r.Context(), `SELECT path FROM paths WHERE domain_uuid=? AND object_uuid=? ORDER BY length(path),path LIMIT 100`, s.domainUUIDForRequest(r), id)
|
||||
paths, err := s.store.ObjectAliasPaths(r.Context(), s.domainUUIDForRequest(r), id)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
paths := []string{}
|
||||
for rows.Next() {
|
||||
var p string
|
||||
if rows.Scan(&p) == nil {
|
||||
paths = append(paths, p)
|
||||
}
|
||||
}
|
||||
rows.Close()
|
||||
allows := s.pathReadFilter(r)
|
||||
for _, p := range paths {
|
||||
if allows(p) {
|
||||
|
||||
Reference in new issue
Block a user