-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
95 lines (77 loc) · 2.98 KB
/
server.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
package test
import (
"context"
"errors"
"fmt"
"github.com/alexfalkowski/go-service/otel"
"github.com/alexfalkowski/go-service/security/jwt/meta"
v1 "github.com/alexfalkowski/go-service/test/greet/v1"
"github.com/alexfalkowski/go-service/transport"
tgrpc "github.com/alexfalkowski/go-service/transport/grpc"
gprometheus "github.com/alexfalkowski/go-service/transport/grpc/metrics/prometheus"
gotel "github.com/alexfalkowski/go-service/transport/grpc/otel"
shttp "github.com/alexfalkowski/go-service/transport/http"
hprometheus "github.com/alexfalkowski/go-service/transport/http/metrics/prometheus"
hotel "github.com/alexfalkowski/go-service/transport/http/otel"
"go.uber.org/fx"
"go.uber.org/zap"
"google.golang.org/grpc"
)
// ErrInvalidToken ...
var ErrInvalidToken = errors.New("invalid token")
// NewServer ...
func NewServer(verifyAuth bool) *Server {
return &Server{verifyAuth: verifyAuth}
}
// Server ...
type Server struct {
verifyAuth bool
v1.UnimplementedGreeterServiceServer
}
// SayHello ...
func (s *Server) SayHello(ctx context.Context, req *v1.SayHelloRequest) (*v1.SayHelloResponse, error) {
c, _ := meta.RegisteredClaims(ctx)
if s.verifyAuth && c == nil {
return nil, ErrInvalidToken
}
return &v1.SayHelloResponse{Message: fmt.Sprintf("Hello %s", req.GetName())}, nil
}
// SayStreamHello ...
func (s *Server) SayStreamHello(stream v1.GreeterService_SayStreamHelloServer) error {
c, _ := meta.RegisteredClaims(stream.Context())
if s.verifyAuth && c == nil {
return ErrInvalidToken
}
req, err := stream.Recv()
if err != nil {
return err
}
return stream.Send(&v1.SayStreamHelloResponse{Message: fmt.Sprintf("Hello %s", req.GetName())})
}
// NewHTTPServer for test.
func NewHTTPServer(lc fx.Lifecycle, logger *zap.Logger, cfg *otel.Config, tcfg *transport.Config) *shttp.Server {
tracer, _ := hotel.NewTracer(hotel.TracerParams{Lifecycle: lc, Config: cfg, Version: Version})
server := shttp.NewServer(shttp.ServerParams{
Shutdowner: NewShutdowner(), Config: &tcfg.HTTP, Logger: logger,
Tracer: tracer, Metrics: hprometheus.NewServerMetrics(lc, Version),
})
return server
}
// NewGRPCServer for test.
func NewGRPCServer(
lc fx.Lifecycle, logger *zap.Logger, cfg *otel.Config, tcfg *transport.Config,
verifyAuth bool, unary []grpc.UnaryServerInterceptor, stream []grpc.StreamServerInterceptor,
) *tgrpc.Server {
tracer, _ := gotel.NewTracer(gotel.TracerParams{Lifecycle: lc, Config: cfg, Version: Version})
server := tgrpc.NewServer(tgrpc.ServerParams{
Shutdowner: NewShutdowner(), Config: &tcfg.GRPC, Logger: logger,
Tracer: tracer, Metrics: gprometheus.NewServerMetrics(lc, Version),
Unary: unary, Stream: stream,
})
v1.RegisterGreeterServiceServer(server.Server, NewServer(verifyAuth))
return server
}
// RegisterTransport for test.
func RegisterTransport(lc fx.Lifecycle, cfg *transport.Config, gs *tgrpc.Server, hs *shttp.Server) {
transport.Register(transport.RegisterParams{Lifecycle: lc, Shutdowner: NewShutdowner(), Config: cfg, HTTP: hs, GRPC: gs})
}