about summary refs log tree commit diff stats
path: root/serve/main.go
blob: 709d340793f14d311c7ad682ab4ea265b9c42eb5 (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
package main

import (
	"flag"
	"log"
	"log/slog"
	"os"
	"os/signal"
	"sync"

	"searchix/internal/config"
	"searchix/internal/server"
)

var (
	liveReload = flag.Bool("live", false, "whether to enable live reloading (development)")
	configFile = flag.String("config", "config.toml", "config file to use")
)

func main() {
	flag.Parse()

	conf, err := config.GetConfig(*configFile)
	if err != nil {
		log.Panicf("error parsing configuration file: %v", err)
	}
	slog.SetLogLoggerLevel(conf.LogLevel)
	log.SetFlags(log.LstdFlags | log.Lmsgprefix)
	log.SetPrefix("searchix: ")

	c := make(chan os.Signal, 2)
	signal.Notify(c, os.Interrupt)
	sv, err := server.New(conf, *liveReload)
	if err != nil {
		log.Fatalf("error setting up server: %v", err)
	}
	wg := &sync.WaitGroup{}
	wg.Add(1)
	go func() {
		defer wg.Done()
		sig := <-c
		log.Printf("signal captured: %v", sig)
		<-sv.Stop()
		slog.Debug("server stopped")
	}()

	sErr := make(chan error)
	wg.Add(1)
	go func() {
		defer wg.Done()
		sErr <- sv.Start()
	}()

	err = <-sErr
	if err != nil {
		// Error starting or closing listener:
		log.Fatalf("error: %v", err)
	}
	wg.Wait()
}