-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (41 loc) · 1.22 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
package main
import (
"context"
"github.com/kaplan-michael/oops/internal/config"
"github.com/kaplan-michael/oops/internal/logger"
"github.com/kaplan-michael/oops/server"
"go.uber.org/zap"
"os"
"os/signal"
"syscall"
)
func main() {
// Load the configuration.
conf := config.MustGetConfig()
// Initialize the logger.
if err := logger.Init(conf.LogLevel); err != nil {
panic(err)
}
// Ensure logs are flushed on exit.
defer zap.L().Sync()
defer zap.S().Sync()
zap.S().Infof("Configuration loaded: TemplateFile=%s, ErrorsFile=%s, LogLevel=%s", conf.Template, conf.Errors, conf.LogLevel)
zap.S().Info("Starting...")
// Create a cancellable context.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Set up OS signal handling.
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
// Listen for termination signals in a separate goroutine.
go func() {
<-sigChan
zap.S().Info("Received termination signal, shutting down...")
cancel()
}()
// Call server run, which will block until the context is cancelled.
if err := server.Run(ctx, conf); err != nil {
zap.S().Fatalf("Server error: %v", err)
}
zap.S().Info("Server shutdown gracefully.")
}