-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (87 loc) · 2.34 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
package main
import (
_ "net/http/pprof"
"context"
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"github.com/meilisearch/meilisearch-go"
"github.com/redis/go-redis/v9"
zLog "github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
"gorm.io/gorm"
)
func main() {
ctx := context.Background()
cfg, err := GetConfig()
if err != nil {
zLog.Fatal().Err(err).Msg("Failed to get config")
}
zLog.Info().Msgf("Config: %+v", cfg)
if cfg.PPROF.IsEnabled {
zLog.Info().Msg("Starting pprof...")
go func() {
zLog.Info().Msgf("pprof is now running on %s", cfg.PPROF.Address())
err := http.ListenAndServe(cfg.PPROF.Address(), nil)
if err != nil {
zLog.Fatal().Err(err).Caller().Msg("error starting pprof")
}
}()
}
var redis *redis.Client
var meilisearch meilisearch.ServiceManager
var db *gorm.DB
eg, mCtx := errgroup.WithContext(ctx)
eg.Go(func() (err error) {
db, err = NewDatabaseInstance(mCtx, cfg.DatabaseConfig)
if err != nil {
err = fmt.Errorf("Failed to create database instance: %w", err)
}
return
})
eg.Go(func() (err error) {
redis, err = NewRedisInstance(mCtx, cfg.RedisConfig)
if err != nil {
err = fmt.Errorf("Failed to create redis client: %w", err)
}
return
})
eg.Go(func() (err error) {
meilisearch, err = NewMeilisearchInstance(mCtx, cfg.MeilisearchConfig)
if err != nil {
err = fmt.Errorf("Failed to create meilisearch client: %w", err)
}
return
})
if err := eg.Wait(); err != nil {
zLog.Fatal().Err(err).Msg("Failed to create clients")
}
zLog.Info().Msgf("Database client created %+v", db)
zLog.Info().Msgf("Redis client created %+v", redis)
zLog.Info().Msgf("Meilisearch client created %+v", meilisearch)
repo := NewRepository(db, meilisearch)
useCase := NewUseCase(repo)
roomUpdate := NewPubSub(redis, cfg.GoroutineConfig.Channel, cfg.GoroutineConfig.Workers, useCase)
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
zLog.Info().Msg("Starting application")
var wg sync.WaitGroup
delta := 1
wg.Add(delta)
go func() {
defer wg.Done()
roomUpdate.ListenRedisPubSub(ctx)
}()
wg.Add(delta)
go func() {
defer wg.Done()
roomUpdate.ProcessMessage(ctx)
}()
<-ctx.Done()
zLog.Info().Msg("Shutting down application")
stop()
wg.Wait()
zLog.Info().Msg("Application shut down cleanly")
}