Files
pages/internal/store/store.go
T
Anya Tokugawa b4792bb175
Build and publish container images / images (push) Successful in 16m37s
Add task capture, work reports, daily digests and private contacts
2026-11-01 22:28:16 +09:00

168 lines
3.5 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.ensureSchemaComponent("cli-auth"); err != nil {
panic(err)
}
if err := st.EnsureTasksSchema(); err != nil {
panic(err)
}
if err := st.ensureSchemaComponent("contacts"); 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
}