-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (62 loc) · 2.01 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
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
"go.uber.org/zap"
"github.com/WaffleHacks/mailer/http"
"github.com/WaffleHacks/mailer/tracing"
"github.com/WaffleHacks/mailer/daemon"
"github.com/WaffleHacks/mailer/logging"
)
func main() {
config, err := ReadConfig()
if err != nil {
log.Fatalf("failed to read configuration: %v\n", err)
}
// Initialize tracing
ctx := context.Background()
provider, err := tracing.Initialize(ctx, config.Tracing)
if err != nil {
log.Fatalf("failed to initalize tracing: %v\n", err)
} else if provider != nil {
defer func() {
_ = provider.Shutdown(ctx)
}()
}
logger, err := logging.New(config.LogLevel, config.Development)
if err != nil {
log.Fatalf("failed to initialize logging: %v\n", err)
}
defer logger.Sync()
// Setup the mailer daemon
mailer := daemon.New(ctx, config.SendBacklog, config.Providers)
// Start the HTTP server
server := http.New(config.Address, mailer.Queue)
if err != nil {
logger.Named("http").Fatal("failed to create HTTP gateway", zap.String("address", config.Address), zap.Error(err))
}
go func() {
logger.Named("http").Info("listening and ready to handle requests", zap.String("address", config.Address))
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Named("http").Fatal("an error occurred while running the server", zap.Error(err))
}
}()
// Register signal handlers for shutdown
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-shutdown
// Set a 15s timeout for the shutdown
shutdownCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
logger.Named("http").Fatal("failed to shutdown gateway", zap.Error(err))
}
if err := mailer.Shutdown(shutdownCtx); err != nil {
logger.Named("daemon").Fatal("failed to shutdown daemon", zap.Error(err))
}
logger.Info("shutdown complete. goodbye!")
}