-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
45 lines (38 loc) · 1010 Bytes
/
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
package srv
import (
"log"
"github.com/buckhx/safari-zone/srv/auth"
"github.com/mwitkow/go-grpc-middleware"
"google.golang.org/grpc"
)
type Service interface {
Listen() error
Name() string
Version() string
}
type Opts struct {
Address string
Auth auth.Opts
}
// ConfigureGRPC configures a GRPC Server w/ the given opts
func ConfigureGRPC(opts Opts) (*grpc.Server, error) {
a, err := auth.NewAuthorizer(opts.Auth)
if err != nil {
return nil, err
}
return NewGRPC(a), nil
}
// NewGRPC builds a grpc.Server w/ the given inceptors registered in order
func NewGRPC(incpts ...Interceptor) *grpc.Server {
stm := make([]grpc.StreamServerInterceptor, len(incpts))
uni := make([]grpc.UnaryServerInterceptor, len(incpts))
for i, incpt := range incpts {
log.Printf("Registering Interceptor - %T", incpt)
stm[i] = incpt.HandleStream
uni[i] = incpt.HandleUnary
}
return grpc.NewServer(
grpc_middleware.WithStreamServerChain(stm...),
grpc_middleware.WithUnaryServerChain(uni...),
)
}