Implement Knowledge City platform, organizer and DAV integration

Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
This commit is contained in:
Anya Tokugawa committed 2026-09-27 14:44:43 +09:00
1 parent 288e6e94ac
commit c1175d389f
699 files changed
+110197 -4117

No files matched your search

+166 -37
View File
@@ -1,23 +1,42 @@
package server
import (
"database/sql"
"errors"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"sync"
"sync/atomic"
"time"
mw "pageswiki/internal/server/middleware"
routespkg "pageswiki/internal/server/routes"
"pageswiki/internal/calendar"
"pageswiki/internal/store"
)
type Server struct {
mux *http.ServeMux
templates *template.Template
store *store.Store
calendarOnce sync.Once
calendarRepo *calendar.Repository
calendarErr error
navigationCache navigationDataCache
mux *http.ServeMux
templates *template.Template
store *store.Store
previewSlots chan struct{}
smtpSlots chan struct{}
ldapSlots chan struct{}
ldapDial ldapDialFunc
previewRoot string
diagramRoot string
importRoot string
schedulerWake chan struct{}
foregroundRequests atomic.Int64
schedulerRunning atomic.Int64
}
func New(
@@ -25,9 +44,16 @@ func New(
) *Server {
s := &Server{
mux: http.NewServeMux(),
store: st,
mux: http.NewServeMux(),
store: st,
previewSlots: make(chan struct{}, 1),
smtpSlots: make(chan struct{}, 2),
ldapSlots: make(chan struct{}, 4),
ldapDial: dialLDAPDirectory,
schedulerWake: make(chan struct{}, 1),
}
s.initializePreviewScheduler()
s.initializeImportRoot()
s.mustLoadTemplates()
@@ -74,11 +100,13 @@ func New(
s.registerRoutes()
s.startGCWorker()
s.startEventScheduler()
return s
}
func (s *Server) registerStaticRoutes() {
s.mux.HandleFunc(socialImagePath, handleSocialImage)
staticFS := http.FileServer(
http.Dir("./static"),
@@ -108,6 +136,7 @@ func (s *Server) registerStaticRoutes() {
func (s *Server) mustLoadTemplates() {
funcs := template.FuncMap{
"cspNonce": func() string { return "" },
"parentPath": parentPath,
@@ -121,6 +150,10 @@ func (s *Server) mustLoadTemplates() {
return a + b
},
"mulFloat": func(a, b float64) float64 {
return a * b
},
"until": func(n int) []int {
out := make([]int, n)
@@ -156,13 +189,17 @@ func (s *Server) mustLoadTemplates() {
In(s.getLocation()).
Format("2006-01-02 15:04")
},
"formatUnixMS": func(ts int64) string {
if ts <= 0 {
return "-"
}
return time.UnixMilli(ts).In(s.getLocation()).Format("2006-01-02 15:04")
},
}
tmpl := template.New("").Funcs(funcs)
parsed, err := tmpl.ParseGlob(
"templates/**/*.gohtml",
)
parsed, err := parseTrustedTemplates(tmpl)
if err != nil {
panic(err)
@@ -172,45 +209,89 @@ func (s *Server) mustLoadTemplates() {
}
func (s *Server) registerRoutes() {
s.registerCalendarRoutes()
s.registerTaskRoutes()
s.mux.HandleFunc("/_/auth/", s.handleAuth)
s.mux.HandleFunc("/_/auth/profile", s.handleProfile)
s.mux.HandleFunc("/_/obj/", s.handleImmutableObject)
s.mux.HandleFunc("/v/json/", s.handleJSONStructure)
s.mux.HandleFunc("/_/api/v1/assets", s.handleAssetsAPI)
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
s.mux.HandleFunc("/_/api/v1/browser/navigation", s.handleBrowserNavigationAPI)
s.mux.HandleFunc("/u/", s.handleUserProfile)
s.mux.HandleFunc("/g", s.handleGroups)
s.mux.HandleFunc("/g/", s.handleGroups)
s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI)
s.mux.HandleFunc("/v/import", s.handleImport)
s.mux.HandleFunc("/v/import/", s.handleImport)
s.mux.HandleFunc("/_/api/v1/import/", s.handleImportAPI)
s.mux.HandleFunc("/-/sys/config/auth", s.handleAuthAdmin)
s.mux.Handle(adapterGatewayPrefix+"/", newAdapterGatewayProxy())
routespkg.RegisterUIRoutes(
s.mux,
routespkg.UIHandlers{
Home: s.handleHome,
NewPage: s.handleNewPage,
SemanticEditor: s.handleEditorRoute,
SemanticPage: s.handleSemanticRoute,
RawView: s.handleRawView,
LinkManager: s.handleLinkManager,
Health: s.handleHealth,
Tree: s.handleTree,
Browser: s.handleBrowser,
Preview: s.handlePreview,
PlainView: s.handlePlainView,
MarkdownView: s.handleMarkdownView,
CompileMarkdown: s.handleCompileMarkdown,
CompileDiagram: s.handleCompileDiagram,
Config: s.handleConfig,
Home: s.handleHome,
NewPage: s.handleNewPage,
SemanticEditor: s.handleEditorRoute,
SemanticPage: s.handleSemanticRoute,
RawView: s.handleRawView,
TextWindow: s.handleTextWindow,
LinkManager: s.handleLinkManager,
Health: s.handleHealth,
Tree: s.handleTree,
Browser: s.handleBrowser,
Preview: s.handlePreview,
PlainView: s.handlePlainView,
MarkdownView: s.handleMarkdownView,
CompileMarkdown: s.handleCompileMarkdown,
CompileDiagram: s.handleCompileDiagram,
Config: s.handleConfig,
AdapterConfig: s.handleAdapterPluginConfig,
VimConfig: s.handleVimSettings,
SystemExtensions: s.handleSystemExtensions,
Periodical: s.handlePeriodical,
Upload: s.handleUpload,
Children: s.handleChildrenProjection,
Bookmarks: s.handleBookmarkChildren,
PreviewCache: s.handlePreviewCache,
Search: s.handleSearch,
Archive: s.handleArchiveEntry,
EmailHTML: s.handleEmailHTML,
PDFPage: s.handlePDFPage,
OpenWith: s.handleOpenWith,
City: s.handleCityMap,
Print: s.handlePrint,
Notifications: s.handleNotificationsPage,
},
)
routespkg.RegisterAPIRoutes(
s.mux,
routespkg.APIHandlers{
CreateObject: s.handleCreateObjectAPI,
ListObjects: s.handleListObjectsAPI,
DeleteObject: s.handleDeleteObjectAPI,
SaveRevision: s.handleSaveRevisionAPI,
ListRevisions: s.handleRevisionListAPI,
RestoreRevision: s.handleRestoreRevisionAPI,
RenderMarkdown: s.handleMarkdownRenderAPI,
AddTag: s.handleTagAddAPI,
BindPath: s.handleBindPathAPI,
RenamePath: s.handleRenamePath,
RenamePreview: s.handleRenamePreview,
RemovePath: s.handleRemovePathAPI,
ZombieList: s.handleZombieListAPI,
ZombiePurge: s.handleZombiePurgeAPI,
CreateObject: s.handleCreateObjectAPI,
ListObjects: s.handleListObjectsAPI,
DeleteObject: s.handleDeleteObjectAPI,
SaveRevision: s.handleSaveRevisionAPI,
ListRevisions: s.handleRevisionListAPI,
RestoreRevision: s.handleRestoreRevisionAPI,
RenderMarkdown: s.handleMarkdownRenderAPI,
AddTag: s.handleTagAddAPI,
BindPath: s.handleBindPathAPI,
RenamePath: s.handleRenamePath,
RenamePreview: s.handleRenamePreview,
RemovePath: s.handleRemovePathAPI,
CopyPath: s.handleCopyPathAPI,
ZombieList: s.handleZombieListAPI,
ZombiePurge: s.handleZombiePurgeAPI,
FederationEnroll: s.handleFederationEnrollAPI,
Event: s.handleEventAPI,
Queue: s.handleQueueAPI,
Adapter: s.handleAdapterAPI,
AdapterSession: s.handleAdapterSessionAPI,
Notifications: s.handleNotificationsAPI,
Publication: s.handlePublicationApprovalAPI,
SearchSuggest: s.handleSearchSuggestAPI,
},
)
}
@@ -218,6 +299,54 @@ func (s *Server) registerRoutes() {
func (s *Server) handler() http.Handler {
var h http.Handler = s.mux
h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/_/api/v1/federation/enroll" ||
strings.HasPrefix(r.URL.Path, "/_/api/v1/adapter/session/") ||
strings.HasPrefix(r.URL.Path, adapterGatewayPrefix+"/") ||
r.URL.Path == "/health" {
s.mux.ServeHTTP(w, r)
return
}
domain, err := s.domainForRequestWithError(r)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
http.NotFound(w, r)
return
}
logDomainLookupError(r, err)
http.Error(
w,
"domain lookup temporarily unavailable",
http.StatusServiceUnavailable,
)
return
}
if domain == nil {
http.NotFound(w, r)
return
}
if domain.Placement == "remote" {
http.Error(
w,
"remote domain is not served by this instance",
http.StatusMisdirectedRequest,
)
return
}
s.mux.ServeHTTP(w, r)
})
domainHandler := h
h = s.withAuthorization(h)
domainHandler = h
h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
background := r.URL.Query().Get("preview") == "1" ||
strings.HasPrefix(r.URL.Path, "/v/preview-cache/")
if !background {
s.foregroundRequests.Add(1)
defer s.foregroundRequests.Add(-1)
}
domainHandler.ServeHTTP(w, r)
})
h = mw.NormalizePath(h)
h = mw.RequestID(h)