Files
pages/internal/store/store.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

162 lines
3.3 KiB
Go

package store
import (
"crypto/rand"
"crypto/sha256"
"database/sql"
"os"
"path/filepath"
"strings"
"sync"
"time"
"pageswiki/internal/geduldh"
)
type Store struct {
// Standby is immutable for the lifetime of this process. Role changes require restart.
ReadOnly bool
StandbyReadUntil time.Time
BackgroundJobsDisabled bool
DB *sql.DB
DocumentRoot string
documentMaterializerOnce sync.Once
documentMaterializer *geduldh.Materializer
pathCacheMu sync.RWMutex
revisionWriteMu sync.Mutex
childCache map[string][]string
authKey []byte
}
func New(
db *sql.DB,
) *Store {
return NewWithDocumentRoot(
db,
"",
)
}
func NewWithDocumentRoot(
db *sql.DB,
documentRoot string,
) *Store {
st :=
&Store{
DB: db,
DocumentRoot: strings.TrimSpace(documentRoot),
childCache: map[string][]string{},
}
st.authKey = loadOrCreateAuthKey(st.DocumentRoot)
if err := st.EnsureDomainsSchema(); err != nil {
panic(err)
}
if err := st.EnsureCampusesSchema(); err != nil {
panic(err)
}
if err := st.EnsureDomainPathsSchema(); err != nil {
panic(err)
}
if err := st.EnsureObjectListingSchema(); err != nil {
panic(err)
}
if err := st.EnsureAssetsSchema(); err != nil {
panic(err)
}
if err := st.EnsureFederationSchema(); err != nil {
panic(err)
}
if err := st.EnsureDocumentStore(); err != nil {
panic(err)
}
if err := st.EnsureEventJobsSchema(); err != nil {
panic(err)
}
if err := st.EnsureActivityOutboxSchema(); err != nil {
panic(err)
}
if err := st.EnsureNotificationSchema(); err != nil {
panic(err)
}
if err := st.EnsurePublicationApprovalSchema(); err != nil {
panic(err)
}
if err := st.EnsureBroadcastSchema(); err != nil {
panic(err)
}
if err := st.EnsureProjectionMetadataSchema(); err != nil {
panic(err)
}
if err := st.EnsureResourceHistorySchema(); err != nil {
panic(err)
}
if err := st.EnsureAdapterSessionsSchema(); err != nil {
panic(err)
}
if err := st.EnsureAuthSchema(); err != nil {
panic(err)
}
if err := st.EnsureTasksSchema(); err != nil {
panic(err)
}
if err := st.EnsureBrowserNavigationSchema(); err != nil {
panic(err)
}
if err := st.EnsureCitySchema(); err != nil {
panic(err)
}
if err := st.EnsurePeriodicalSchema(); err != nil {
panic(err)
}
if err := st.EnsureUserSpaceSchema(); err != nil {
panic(err)
}
if err := st.ensureSchemaComponent("profile-contacts"); err != nil {
panic(err)
}
if err := st.EnsureBuiltinAdminGroups(); err != nil {
panic(err)
}
if err := st.EnsureSearchIndexSchema(); err != nil {
panic(err)
}
if err := st.EnsureImportSchema(); err != nil {
panic(err)
}
if err := st.EnsureZombieGCSchema(); err != nil {
panic(err)
}
return st
}
func loadOrCreateAuthKey(documentRoot string) []byte {
if encoded := strings.TrimSpace(os.Getenv("PAGESWIKI_AUTH_KEY")); encoded != "" {
sum := sha256.Sum256([]byte(encoded))
return sum[:]
}
path := ""
if documentRoot != "" {
path = filepath.Join(filepath.Dir(documentRoot), "auth.key")
if data, err := os.ReadFile(path); err == nil && len(data) == 32 {
return data
}
}
key := make([]byte, 32)
if _, err := rand.Read(key); err != nil {
panic(err)
}
if path != "" {
if err := os.WriteFile(path, key, 0600); err != nil {
panic(err)
}
}
return key
}