-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_lobbies.go
77 lines (57 loc) · 1.72 KB
/
list_lobbies.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
package main
import (
"errors"
"github.com/gogo/status"
"github.com/hashicorp/go-multierror"
"github.com/rs/zerolog/log"
"google.golang.org/grpc/codes"
"github.com/elojah/game_02/cmd/auth/grpc"
"github.com/elojah/game_02/pkg/account/dto"
gerrors "github.com/elojah/game_02/pkg/errors"
"github.com/elojah/game_02/pkg/lobby"
)
func (h handler) ListLobbies(request *dto.Auth, stream grpc.Auth_ListLobbiesServer) error {
ctx := stream.Context()
logger := log.With().Str("method", "list_lobbies").Logger()
// #Request processing
if request == nil {
err := gerrors.ErrNullRequest{}
logger.Error().Err(err).Msg("null request")
return status.New(codes.InvalidArgument, err.Error()).Err()
}
// #Check credentials
if _, err := h.account.Authorize(ctx, request.ID, request.Token); err != nil {
if errors.As(err, &gerrors.ErrInvalidCredentials{}) {
logger.Error().Err(err).Msg("invalid credentials")
return status.New(codes.Unauthenticated, err.Error()).Err()
}
logger.Error().Err(err).Msg("failed to authenticate")
return status.New(codes.Internal, err.Error()).Err()
}
// #Fetch lobbies
c := make(chan lobby.L, h.c.BufferLobbies)
done := make(chan struct{})
var result error
go func() {
// #Send lobbies through stream
for l := range c {
local := l
if err := stream.Send(&local); err != nil {
result = multierror.Append(result, err)
}
}
done <- struct{}{}
}()
if err := h.lobby.FetchAll(ctx, c); err != nil {
close(c)
logger.Error().Err(err).Msg("failed to fetch lobbies")
return status.New(codes.Internal, err.Error()).Err()
}
close(c)
_ = <-done
if result != nil {
return status.New(codes.Internal, result.Error()).Err()
}
logger.Info().Msg("success")
return nil
}