-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (63 loc) · 1.64 KB
/
main.go
File metadata and controls
79 lines (63 loc) · 1.64 KB
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"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"reverse-watch/config"
"reverse-watch/domain/models"
"reverse-watch/ingestors"
"reverse-watch/logging"
"reverse-watch/repository/factory"
"reverse-watch/secret"
"reverse-watch/server"
)
func main() {
logging.Initialize()
cfg := config.Load()
models.InitSnowflakeGenerator(0 /* workerID */, 0 /* processID */)
keygen := secret.NewKeyGenerator(cfg.Environment)
f, err := factory.NewFactory(cfg, keygen)
if err != nil {
logging.Log.Errorf("failed to create factory: %v", err)
panic(err)
}
logging.Log.Info("starting reverse watch")
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGTERM)
srv, err := server.New(cfg, f)
if err != nil {
panic(err)
}
ingestorManager := ingestors.New(f, &cfg, logging.Log)
ingestorManager.StartIngestors()
httpSrv := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%s", cfg.HTTP.Port),
Handler: srv,
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
go func() {
logging.Log.Infof("starting HTTP Server on %v", httpSrv.Addr)
if err := httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}()
<-done
logging.Log.Info("shutting down server connections gracefully")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := httpSrv.Shutdown(ctx); err != nil {
panic(err)
}
ingestorManager.StopIngestors()
if err := f.Close(); err != nil {
panic(err)
}
}