all repos — searchix @ e9eed3ddc4229db707cccb30beddde15044eff16

Search engine for NixOS, nix-darwin, home-manager and NUR users

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

Alan Pearce
commit

e9eed3ddc4229db707cccb30beddde15044eff16

parent

2c1491de56d0c3e2f4cb0b0c1e33035510f72fc5

1 file changed, 82 insertions(+), 0 deletions(-)

changed files
A 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") +}