-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (64 loc) · 1.84 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
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"context"
"errors"
"github.com/Zapharaos/fihub-backend/internal/app"
"github.com/Zapharaos/fihub-backend/pkg/env"
"go.uber.org/zap"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
import "github.com/Zapharaos/fihub-backend/internal/router"
// @version 1.0
// @title Fihub API Swagger
// @description Fihub API Swagger
// @termsOfService http://swagger.io/terms/
// @contact.name Zapharaos
// @contact.url https://matthieu-freitag.com/
// @contact.email [email protected]
// @host localhost:8080
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
func main() {
// Setup application
app.Init()
// Create router
r := router.New()
// Create server
srv := &http.Server{
Addr: ":" + env.GetString("GO_PORT", "8080"),
Handler: r,
WriteTimeout: 30 * time.Second,
ReadTimeout: 10 * time.Second,
IdleTimeout: time.Minute,
}
// Create a channel to receive termination signals (SIGINT, SIGTERM)
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// Start the server in a separate goroutine
go func() {
// Listen for and handle incoming requests
err := srv.ListenAndServe()
// Check if the error is due to a graceful shutdown
if err != nil && !errors.Is(err, http.ErrServerClosed) {
zap.L().Fatal("server listen", zap.Error(err))
}
}()
zap.L().Info("Server started", zap.String("addr", srv.Addr))
// Wait for a termination signal
<-done
// Create a context with a 5-second timeout for graceful shutdown
ctxShutDown, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer func() {
cancel()
}()
// Gracefully shut down the server
if err := srv.Shutdown(ctxShutDown); err != nil {
zap.L().Fatal("Server shutdown failed", zap.Error(err))
}
zap.L().Info("Server shutdown")
}