-
Notifications
You must be signed in to change notification settings - Fork 0
/
interceptor.go
39 lines (32 loc) · 955 Bytes
/
interceptor.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
package pkg
import (
"context"
"log"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func WithServerUnaryInterceptor(procfile string) grpc.ServerOption {
return grpc.UnaryInterceptor(func(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Errorf(codes.InvalidArgument, "Retrieving metadata is failed")
}
procfileHeader, ok := md["procfile"]
if !ok || len(procfileHeader) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "Procfile not supplied")
}
if procfile != procfileHeader[0] && info.FullMethod != "/Procgo/KillAll" {
log.Println(procfile, procfileHeader)
return nil, status.Errorf(codes.InvalidArgument, "Wrong Procfile")
}
h, err := handler(ctx, req)
return h, err
})
}