forked from DiceDB/dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (59 loc) · 1.52 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
package main
import (
"context"
"errors"
"flag"
"os"
"os/signal"
"sync"
"syscall"
"github.com/dicedb/dice/internal/server"
"github.com/charmbracelet/log"
"github.com/dicedb/dice/config"
)
func setupFlags() {
flag.StringVar(&config.Host, "host", "0.0.0.0", "host for the dice server")
flag.IntVar(&config.Port, "port", 7379, "port for the dice server")
flag.StringVar(&config.RequirePass, "requirepass", config.RequirePass, "enable authentication for the default user")
flag.Parse()
}
func main() {
setupFlags()
ctx, cancel := context.WithCancel(context.Background())
// Handle SIGTERM and SIGINT
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
// Initialize the AsyncServer
asyncServer := server.NewAsyncServer()
// Find a port and bind it
if err := asyncServer.FindPortAndBind(); err != nil {
cancel()
log.Fatal("Error finding and binding port:", err)
}
wg := sync.WaitGroup{}
// Goroutine to handle shutdown signals
wg.Add(1)
go func() {
defer wg.Done()
<-sigs
asyncServer.InitiateShutdown()
cancel()
}()
// Run the server
err := asyncServer.Run(ctx)
// Handling different server errors
if err != nil {
if errors.Is(err, context.Canceled) {
log.Info("Server was canceled")
} else if errors.Is(err, server.ErrAborted) {
log.Info("Server received abort command")
} else {
log.Error("Server error", "error", err)
}
} else {
log.Info("Server stopped without error")
}
close(sigs)
wg.Wait()
log.Info("Server has shut down gracefully")
}