-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
55 lines (45 loc) · 1.3 KB
/
main.go
File metadata and controls
55 lines (45 loc) · 1.3 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
52
53
54
55
package main
import (
"context"
"errors"
gateway "github.com/Brain-Wave-Ecosystem/gateway/internal/config"
"github.com/Brain-Wave-Ecosystem/gateway/internal/server"
"github.com/Brain-Wave-Ecosystem/go-common/pkg/abstractions"
"github.com/Brain-Wave-Ecosystem/go-common/pkg/config"
"golang.org/x/exp/slog"
"io"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
cfg, err := config.Load[gateway.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
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), cfg.ShutdownTimeout)
defer shutdownCancel()
if err = srv.Shutdown(shutdownCtx); err != nil {
if err != io.EOF || !errors.Is(context.Canceled, err) {
slog.Warn("Server forced to shutdown", "error", err)
}
}
}()
if err = srv.Start(); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("Server failed to start", "error", err)
}
}