Add search index

This commit is contained in:
2026-05-07 09:41:20 +02:00
parent 2787c15d40
commit 3d3a121fa6
6 changed files with 278 additions and 49 deletions
+35
View File
@@ -50,6 +50,7 @@ func main() {
cacheDir := flag.String("cache", "./cache", "thumbnail cache directory")
user := flag.String("user", "", "basic auth username (empty = no auth)")
pass := flag.String("pass", "", "basic auth password")
reindexInterval := flag.Duration("reindex-interval", 30*time.Minute, "periodic search index rebuild interval (0 disables)")
flag.Parse()
root, err := filepath.Abs(*wikiDir)
@@ -85,8 +86,33 @@ func main() {
static.ServeHTTP(w, r)
}))
http.HandleFunc("/_logout", h.handleLogout)
http.HandleFunc("/_reindex", h.handleReindex)
http.Handle("/", h)
// Build the folder index off the request path so the listener can start
// accepting connections immediately. searchWiki blocks on folderIndex.ready
// so the first search after a cold start still returns correct results.
go func() {
folderIndex.buildMu.Lock()
entries := buildFolderIndex(root)
folderIndex.Lock()
folderIndex.entries = entries
folderIndex.builtAt = time.Now()
folderIndex.Unlock()
folderIndex.buildMu.Unlock()
close(folderIndex.ready)
}()
if *reindexInterval > 0 {
go func(interval time.Duration) {
t := time.NewTicker(interval)
defer t.Stop()
for range t.C {
rebuildFolderIndex(root)
}
}(*reindexInterval)
}
log.Printf("datascape listening on %s, wiki at %s", *addr, root)
log.Fatal(http.ListenAndServe(*addr, nil))
}
@@ -312,6 +338,10 @@ func (h *handler) handlePost(w http.ResponseWriter, r *http.Request, urlPath, fs
return
}
} else {
// Stat first so we know whether MkdirAll actually created the folder
// — if it did, the search index needs a new entry.
_, statErr := os.Stat(fsPath)
newlyCreated := os.IsNotExist(statErr)
if err := os.MkdirAll(fsPath, 0755); err != nil {
http.Error(w, "mkdir failed: "+err.Error(), http.StatusInternalServerError)
return
@@ -320,6 +350,11 @@ func (h *handler) handlePost(w http.ResponseWriter, r *http.Request, urlPath, fs
http.Error(w, "write failed: "+err.Error(), http.StatusInternalServerError)
return
}
if newlyCreated {
if rel, err := filepath.Rel(h.root, fsPath); err == nil {
folderIndexAdd(filepath.ToSlash(rel))
}
}
}
http.Redirect(w, r, redirectTarget, http.StatusSeeOther)
}