-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
75 lines (66 loc) · 1.78 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"
"errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"kartverket.no/smseagle-proxy/pkg/alerter"
"kartverket.no/smseagle-proxy/pkg/config"
"kartverket.no/smseagle-proxy/pkg/smseagle"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var (
ctx context.Context
stop context.CancelFunc
gracePeriod = 30 * time.Second
logLevel *slog.LevelVar
cfg *config.ProxyConfig
)
func init() {
ctx, stop = signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
cfg = config.Read()
logLevel = &slog.LevelVar{}
logLevel.Set(slog.LevelInfo)
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
}))
slog.SetDefault(logger)
if cfg.Debug {
logLevel.Set(slog.LevelDebug)
slog.Debug("Debug mode on")
}
}
func main() {
smseagle := smseagle.NewSMSEagle(cfg)
oncall := alerter.NewGrafanaOncall(smseagle, cfg)
sm := http.NewServeMux()
sm.HandleFunc("/webhook/sms", oncall.HandleSMS)
sm.HandleFunc("/webhook/call", oncall.HandleCall)
sm.Handle("/metrics", promhttp.Handler())
sm.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {})
server := http.Server{
Handler: sm,
Addr: cfg.Port,
}
defer stop()
go func() {
slog.Info("Starting smseagle-proxy", "port", cfg.Port)
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("error starting server", "error", err)
os.Exit(1)
}
}()
<-ctx.Done()
slog.Info("graceful shutdown")
shutdownCtx, cancel := context.WithDeadline(context.Background(), time.Now().Add(gracePeriod))
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
slog.Error("server shutdown error", "error", err)
os.Exit(1)
}
slog.Info("bye")
}