-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_server.go
39 lines (33 loc) · 1.07 KB
/
start_server.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
package server
import (
"fmt"
"github.com/kotalco/core-api/config"
"os"
"os/signal"
"syscall"
"github.com/gofiber/fiber/v2"
"github.com/kotalco/core-api/pkg/logger"
)
// StartServerWithGracefulShutdown function for starting server with a graceful shutdown.
// Create channel for idle connections.
// check if Received an interrupt signal, shutdown.
// Error if closing listeners, or context timeout
// Run server.
// Error if Run server with reason
func StartServerWithGracefulShutdown(a *fiber.App) {
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, syscall.SIGINT) // Catch OS signals.
<-sigint
if err := a.Shutdown(); err != nil {
go logger.Info("StartServerWithGracefulShutdown", fmt.Sprintf("Oops... Server is not shutting down! Reason: %v", err))
}
close(idleConnsClosed)
}()
port := ":" + config.Environment.ServerPort
if err := a.Listen(port); err != nil {
go logger.Info("StartServerWithGracefulShutdown", fmt.Sprintf("Oops... Server is not running! Reason: %v", err))
}
<-idleConnsClosed
}