about summary refs log tree commit diff stats
path: root/cmd
diff options
context:
space:
mode:
authorAlan Pearce2024-05-24 18:31:56 +0200
committerAlan Pearce2024-05-24 18:31:56 +0200
commite9eed3ddc4229db707cccb30beddde15044eff16 (patch)
treec586eae45a4aa99fd1a971c2bd29ad2e74d14975 /cmd
parent2c1491de56d0c3e2f4cb0b0c1e33035510f72fc5 (diff)
downloadsearchix-e9eed3ddc4229db707cccb30beddde15044eff16.tar.lz
searchix-e9eed3ddc4229db707cccb30beddde15044eff16.tar.zst
searchix-e9eed3ddc4229db707cccb30beddde15044eff16.zip
refactor: split server cmd and module
It should now be possible to run the server from inside another go
application by importing the main module and running its Start() function
Diffstat (limited to 'cmd')
-rw-r--r--cmd/searchix-web/main.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/cmd/searchix-web/main.go b/cmd/searchix-web/main.go
new file mode 100644
index 0000000..91ecc7a
--- /dev/null
+++ b/cmd/searchix-web/main.go
@@ -0,0 +1,82 @@
+package main
+
+import (
+	"context"
+	"flag"
+	"fmt"
+	"log"
+	"log/slog"
+	"os"
+	"os/signal"
+
+	"searchix"
+	"searchix/internal/config"
+)
+
+var buildVersion string
+
+var (
+	configFile         = flag.String("config", "config.toml", "config `file` to use")
+	printDefaultConfig = flag.Bool(
+		"print-default-config",
+		false,
+		"print default configuration and exit",
+	)
+	liveReload = flag.Bool("live", false, "whether to enable live reloading (development)")
+	replace    = flag.Bool("replace", false, "replace existing index and exit")
+	update     = flag.Bool("update", false, "update index and exit")
+	version    = flag.Bool("version", false, "print version information")
+)
+
+func main() {
+	flag.Parse()
+	if *version {
+		fmt.Fprintf(os.Stderr, "searchix %s", buildVersion)
+		if buildVersion != config.CommitSHA && buildVersion != config.ShortSHA {
+			fmt.Fprintf(os.Stderr, " %s", config.CommitSHA)
+		}
+		_, err := fmt.Fprint(os.Stderr, "\n")
+		if err != nil {
+			panic("can't write to standard error?!")
+		}
+		os.Exit(0)
+	}
+	if *printDefaultConfig {
+		_, err := fmt.Print(config.GetDefaultConfig())
+		if err != nil {
+			panic("can't write to standard output?!")
+		}
+		os.Exit(0)
+	}
+
+	cfg, err := config.GetConfig(*configFile)
+	if err != nil {
+		// only use log functions after the config file has been read successfully
+		log.Fatalf("Failed to parse config file: %v", err)
+	}
+	s, err := searchix.New(cfg)
+	if err != nil {
+		log.Fatalf("Failed to initialise searchix: %v", err)
+	}
+
+	err = s.SetupIndex(*replace, *update)
+	if err != nil {
+		log.Fatalf("Failed to setup index: %v", err)
+	}
+
+	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
+	defer cancel()
+
+	go func() {
+		err = s.Start(ctx, *liveReload)
+		if err != nil {
+			// Error starting or closing listener:
+			log.Fatalf("error: %v", err)
+		}
+	}()
+
+	<-ctx.Done()
+	slog.Debug("calling stop")
+	s.Stop()
+	slog.Debug("done")
+}