about summary refs log tree commit diff stats
path: root/cmd/searchix-web/main.go
blob: f065a84354e65614efd4dd530267ae36a46537eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main

import (
	"context"
	"flag"
	"fmt"
	"log"
	"log/slog"
	"os"
	"os/signal"

	"searchix"
	"searchix/internal/config"
)

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 {
		_, err := fmt.Fprintf(os.Stderr, "searchix %s\n", config.Version)
		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")
}