-
Notifications
You must be signed in to change notification settings - Fork 12
/
customer_os_api_main.go
59 lines (51 loc) · 1.44 KB
/
customer_os_api_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
package main
import (
"context"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/config"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/constants"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/logger"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/server"
"log"
"os"
"os/signal"
"syscall"
)
func main() {
// Initialize configuration
cfg, err := config.InitConfig()
if err != nil {
log.Fatal(err)
}
// Initialize logger
appLogger := logger.NewExtendedAppLogger(&cfg.Logger)
appLogger.InitLogger()
appLogger.WithName(constants.ServiceName)
// Create a context with a cancel function
ctx, cancel := context.WithCancel(context.Background())
// Set up signal handler to cancel context on interrupt
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
select {
case <-sigChan:
appLogger.Warn("Interrupt signal received. Shutting down...")
cancel()
case <-ctx.Done():
appLogger.Warn("Context canceled. Shutting down...")
}
}()
// Init server
errChan := make(chan error, 1)
go func() {
errChan <- server.NewServer(cfg, appLogger).Run(ctx)
}()
// Wait for server to exit or context to be canceled
select {
case err := <-errChan:
appLogger.Fatalf("Server error: %v", err)
case <-ctx.Done():
// Do nothing
}
// Flush logs and exit
appLogger.Sync()
}