-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
124 lines (109 loc) · 3.1 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
_ "embed"
"github.com/cloudknit-io/cloudknit/event-service/internal/db"
"github.com/cloudknit-io/cloudknit/event-service/internal/services"
"github.com/cloudknit-io/cloudknit/event-service/internal/web"
"github.com/cloudknit-io/cloudknit/event-service/internal/zlog"
_ "github.com/go-sql-driver/mysql"
_ "github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
var (
//go:embed .version
Version string
baseLogger = zlog.NewPlainEntry()
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
baseLogger = baseLogger.WithField("version", Version)
if len(os.Args) > 1 {
if err := migrate(baseLogger); err != nil {
panic(err)
}
return
}
errWg, errCtx := errgroup.WithContext(ctx)
l := baseLogger.WithContext(ctx)
l.Info("Initializing event-service...")
svcs, err := services.NewServices(l)
if err != nil {
l.Fatalf("error instantiating services: %v", err)
}
ss, err := web.NewStreamingServer(svcs, l)
if err != nil {
l.Fatalf("error creating streaming server: %v", err)
}
errWg.Go(func() error {
if err := ss.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return errors.Wrap(err, "streaming server error")
}
return nil
})
rs, err := web.NewServer(svcs, l)
if err != nil {
l.Fatalf("error creating rest server: %v", err)
}
errWg.Go(func() error {
if err := rs.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return errors.Wrap(err, "rest server error")
}
return nil
})
errWg.Go(func() error {
<-errCtx.Done()
stop()
l.Info("Shutting down streaming server")
if err := ss.Shutdown(errCtx); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
l.Info("Shutting down REST server")
if err := rs.Shutdown(errCtx); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
})
err = errWg.Wait()
if err == nil || errors.Is(err, context.Canceled) {
l.Info("Streaming and REST server gracefully shutdown")
} else if err != nil {
l.Fatalf("unknown error occurred: %v", err)
}
}
func migrate(log *logrus.Entry) error {
if len(os.Args) != 3 {
return errors.Errorf("invalid argument count for migration mode, expected 3, got %d", len(os.Args))
}
if os.Args[1] == "--migrate" || os.Args[1] == "-m" {
action := os.Args[2]
log.Infof("Executing migrate %s command", action)
change, err := db.Migrate(db.MigrationCommand(action))
if err != nil {
return err
}
if change {
log.Infof("Finished executing migrate %s command", action)
} else {
log.Info("Latest migrations already applied, no change needed")
}
return nil
}
return errors.Errorf("invalid argument provided, expected --migrate or -m, got %s", os.Args[1])
}
func setVersion() error {
v, err := os.ReadFile(".version")
if err != nil {
return errors.Wrap(err, "error reading .version file")
}
Version = string(v)
return nil
}