-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (59 loc) · 1.61 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/teilomillet/hapax/errors"
"github.com/teilomillet/hapax/server"
"go.uber.org/zap"
)
func main() {
// Create logger with explicit error handling
logger, err := zap.NewProduction()
if err != nil {
// Fail fast if logger creation fails
fmt.Printf("Critical error: Failed to create logger: %v\n", err)
os.Exit(1)
}
// Ensure logger is synced, with robust error handling
defer func() {
if syncErr := logger.Sync(); syncErr != nil {
// Log sync failure, but don't mask the original error
fmt.Printf("Warning: Failed to sync logger: %v\n", syncErr)
}
}()
// Set global logger
errors.SetLogger(logger)
// Configuration and server setup with comprehensive error handling
configPath := "config.yaml"
server, err := server.NewServer(configPath, logger)
if err != nil {
logger.Fatal("Server initialization failed",
zap.Error(err),
zap.String("config_path", configPath),
)
}
// Graceful shutdown infrastructure
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Signal handling with detailed logging
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigChan
logger.Info("Shutdown signal received",
zap.String("signal", sig.String()),
zap.String("action", "initiating graceful shutdown"),
)
cancel()
}()
// Server start with comprehensive error tracking
if err := server.Start(ctx); err != nil {
logger.Fatal("Server startup or runtime error",
zap.Error(err),
zap.String("action", "server_start_failed"),
)
}
}