-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
51 lines (41 loc) · 1.22 KB
/
main.go
File metadata and controls
51 lines (41 loc) · 1.22 KB
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
package main
import (
"context"
"github.com/Brain-Wave-Ecosystem/go-common/pkg/abstractions"
"github.com/Brain-Wave-Ecosystem/go-common/pkg/config"
users "github.com/Brain-Wave-Ecosystem/users-service/internal/config"
"github.com/Brain-Wave-Ecosystem/users-service/internal/server"
"golang.org/x/exp/slog"
"os"
"os/signal"
"syscall"
)
func main() {
cfg, err := config.Load[users.Config]()
if err != nil {
slog.Error("Error loading config: ", "error", err)
return
}
startCtx, startCancel := context.WithTimeout(context.Background(), cfg.StartTimeout)
defer startCancel()
var srv abstractions.Server
srv, err = server.NewServer(startCtx, cfg)
if err != nil {
slog.Error("Error starting server: ", "error", err)
return
}
go func() {
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
<-signalCh
slog.Info("Shutting down server...")
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), cfg.ShutdownTimeout)
defer shutdownCancel()
if err = srv.Shutdown(shutdownCtx); err != nil {
slog.Warn("Server forced to shutdown", "error", err)
}
}()
if err = srv.Start(); err != nil {
slog.Error("Server failed to start", "error", err)
}
}