/
main.go
75 lines (66 loc) · 1.45 KB
/
main.go
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
package main
import (
"context"
"flag"
"fmt"
"github.com/246859/river/assets"
"github.com/246859/river/cmd/river/server"
"log/slog"
"os"
"os/signal"
"syscall"
)
var (
Version string
ConfigFile string
)
func init() {
flag.StringVar(&ConfigFile, "f", "", "specified configuration file")
flag.Parse()
}
func main() {
// listen the os signals
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGKILL, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
defer cancel()
// log banner
_ = assets.OutFsFile(assets.Fs, "banner.txt", os.Stdout)
// red config file
var option server.Options
if len(ConfigFile) > 0 {
options, err := server.ReadOption(ConfigFile)
if err != nil {
slog.Error("", "err", err)
return
}
slog.Info("config file loaded √")
option = options
} else {
slog.Info("no config file specified")
}
option.Version = Version
// initialize the server
riverServer, err := server.NewServer(ctx, option)
if err != nil {
slog.Error(fmt.Sprintf("river grpc server boot failed %s", err.Error()))
}
defer func() {
if riverServer != nil {
riverServer.Close()
}
}()
// run the server in another goroutine
runCh := make(chan error)
go func() {
runCh <- riverServer.Run()
close(runCh)
}()
// close when recv os ignals or run failed
select {
case <-ctx.Done():
if riverServer != nil {
riverServer.Close()
}
case err := <-runCh:
slog.Error("river server run failed", "error", err)
}
}