-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
104 lines (82 loc) · 2.68 KB
/
main.go
File metadata and controls
104 lines (82 loc) · 2.68 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
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
package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
"github.com/arthurdotwork/chat/internal/adapters/primary/grpc"
ppubsub "github.com/arthurdotwork/chat/internal/adapters/primary/pubsub"
"github.com/arthurdotwork/chat/internal/adapters/secondary/broadcaster"
"github.com/arthurdotwork/chat/internal/adapters/secondary/store"
"github.com/arthurdotwork/chat/internal/domain"
"github.com/arthurdotwork/chat/internal/infrastructure/log"
ps "github.com/arthurdotwork/chat/internal/infrastructure/pubsub"
"golang.org/x/sync/errgroup"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.Config(ctx)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sig
slog.DebugContext(ctx, "shutdown initiated")
cancel()
}()
if err := run(ctx); err != nil {
slog.ErrorContext(ctx, "error running server", "error", err)
}
}
func run(ctx context.Context) error {
grpcAddress := fmt.Sprintf(":%s", env("GRPC_PORT", "56000"))
pubsubClient, err := ps.NewClient(ctx, env("GCP_PROJECT_ID", "arthur-private"), env("GCP_CREDENTIALS_FILE_PATH", ".pubsub_service_account.json"))
if err != nil {
slog.ErrorContext(ctx, "error creating pubsub client", "error", err)
return fmt.Errorf("ps.NewClient: %w", err)
}
memoryRoomStore := store.NewMemoryRoomStore()
b := broadcaster.NewBroadcaster(pubsubClient)
chatService := domain.NewChatService(memoryRoomStore, b)
chatServer := grpc.NewChatServer(chatService, grpcAddress)
chatSubscriber := ppubsub.NewSubscriber(pubsubClient, chatService)
g, _ := errgroup.WithContext(ctx)
g.Go(func() error {
if err := chatSubscriber.Subscribe(ctx, "chat-events"); err != nil {
slog.ErrorContext(ctx, "error subscribing to chat", "error", err)
return fmt.Errorf("chatSubscriber.Subscribe: %w", err)
}
return nil
})
g.Go(func() error {
slog.DebugContext(ctx, "starting grpc server", "address", grpcAddress)
if err := chatServer.Run(ctx); err != nil {
slog.ErrorContext(ctx, "error running grpc server", "error", err)
return fmt.Errorf("chatServer.Run: %w", err)
}
return nil
})
g.Go(func() error {
<-ctx.Done()
if err := chatServer.Close(context.WithoutCancel(ctx)); err != nil {
slog.ErrorContext(ctx, "error closing chat service", "error", err)
}
return nil
})
if err := g.Wait(); err != nil {
slog.ErrorContext(ctx, "error running server", "error", err)
return fmt.Errorf("errGroup.Wait: %w", err)
}
slog.DebugContext(ctx, "processing shutdown")
time.Sleep(time.Second * 10)
return nil
}
func env(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}