-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
46 lines (36 loc) · 1.03 KB
/
service.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
package test
import (
"context"
"errors"
"github.com/alexfalkowski/go-service/meta"
v1 "github.com/alexfalkowski/go-service/test/greet/v1"
)
// ErrInvalidToken ...
var ErrInvalidToken = errors.New("invalid token")
// NewService ...
func NewService(verifyAuth bool) *Service {
return &Service{verifyAuth: verifyAuth}
}
// Service ...
type Service struct {
v1.UnimplementedGreeterServiceServer
verifyAuth bool
}
// SayHello ...
func (s *Service) SayHello(ctx context.Context, req *v1.SayHelloRequest) (*v1.SayHelloResponse, error) {
if s.verifyAuth && !meta.IsEqual(Test(ctx), "auth") {
return nil, ErrInvalidToken
}
return &v1.SayHelloResponse{Message: "Hello " + req.GetName()}, nil
}
// SayStreamHello ...
func (s *Service) SayStreamHello(stream v1.GreeterService_SayStreamHelloServer) error {
if s.verifyAuth && !meta.IsEqual(Test(stream.Context()), "auth") {
return ErrInvalidToken
}
req, err := stream.Recv()
if err != nil {
return err
}
return stream.Send(&v1.SayStreamHelloResponse{Message: "Hello " + req.GetName()})
}