about summary refs log tree commit diff stats
path: root/internal/server/server.go
diff options
context:
space:
mode:
authorAlan Pearce2024-05-07 21:40:19 +0200
committerAlan Pearce2024-05-07 21:40:19 +0200
commitf459e84ecf7307fe2eeb7fbaa5b0c50613ec04f4 (patch)
tree235900bc6c11c0bbcdea6436461ed2a011ec5f58 /internal/server/server.go
parent212e5cf6621c99e46dbb37c860dab8938968bb19 (diff)
downloadsearchix-f459e84ecf7307fe2eeb7fbaa5b0c50613ec04f4.tar.lz
searchix-f459e84ecf7307fe2eeb7fbaa5b0c50613ec04f4.tar.zst
searchix-f459e84ecf7307fe2eeb7fbaa5b0c50613ec04f4.zip
perf: create search indexes in parallel with server startup
Diffstat (limited to 'internal/server/server.go')
-rw-r--r--internal/server/server.go19
1 files changed, 11 insertions, 8 deletions
diff --git a/internal/server/server.go b/internal/server/server.go
index 3b85061..9649ad2 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -80,15 +80,12 @@ var sourceFileName = map[string]string{
 	"nixos":        "nixos-options-nixos-unstable",
 }
 
-func init() {
+func makeIndex(source string, filename string) {
 	var err error
-
-	for source, filename := range sourceFileName {
-		index[source], err = search.New(filename)
-		if err != nil {
-			log.Fatalf("could not build search index, error: %#v", err)
-		}
-
+	log.Printf("loading %s index", source)
+	index[source], err = search.New(filename)
+	if err != nil {
+		log.Fatalf("could not build search index, error: %#v", err)
 	}
 }
 
@@ -193,6 +190,12 @@ func New(runtimeConfig *Config) (*Server, error) {
 
 	mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("frontend/static"))))
 
+	go func() {
+		for source, filename := range sourceFileName {
+			makeIndex(source, filename)
+		}
+	}()
+
 	if runtimeConfig.LiveReload {
 		applyDevModeOverrides(config)
 		liveReload := livereload.New()