This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (53 loc) · 1.4 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"github.com/cble-platform/cble-backend/cble"
"github.com/cble-platform/cble-backend/internal/logo"
_ "github.com/lib/pq"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)
func main() {
ctx, closeContext := context.WithCancel(context.Background())
// Print the logo
fmt.Println(logo.Print())
// Parse the config path from command line args
var cfgFile string
pflag.StringVar(&cfgFile, "config", "", "the path to the config file")
pflag.Parse()
// Create the CBLEServer instance
cbleServer, err := cble.NewServer(ctx, cfgFile)
if err != nil {
logrus.Fatalf("failed to create CBLE server: %v", err)
}
// Do the CBLE lifetime
// Initialize
if err := cbleServer.Initialize(ctx); err != nil {
logrus.Fatalf("failed to initialize CBLE server: %v", err)
}
// Run all runtimes
var wg sync.WaitGroup
cbleServer.Run(ctx, &wg)
// Close global context on signal receive
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
wg.Add(1)
go func() {
s := <-sigCh
logrus.Warnf("Received signal %v, attempting graceful shutdown...", s)
closeContext()
wg.Done()
}()
// Wait for all runtimes to shutdown
wg.Wait()
// Shutdown
if err := cbleServer.Shutdown(); err != nil {
logrus.Fatalf("failed to initialize CBLE server: %v", err)
}
logrus.Infof("CBLE shutdown successful")
}