/
server.go
97 lines (80 loc) · 2.68 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
96
97
package main
import (
"context"
"log"
"net"
"github.com/maxnilz/grpc-istio-demo/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/reflection"
emoji "gopkg.in/kyokomi/emoji.v1"
)
type server struct{}
type AttributeCondition struct {
key string
desc string
}
func (ac *AttributeCondition) String() {
log.Printf("%v: %v", ac.key, ac.desc)
}
var ac1 = AttributeCondition{key: "c1", desc: "Can only perform the ops(update, delete, read) on the jobs he created"}
var ac2 = AttributeCondition{key: "c2", desc: "can only update the salary field of the jobs that he has been assigned to"}
var ac3 = AttributeCondition{key: "c3", desc: "can only update the requirements field of the jobs that he has been assigned to"}
func IsAttributeConditionEnabled(ctx context.Context, ac AttributeCondition) (meta string, enabled bool) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return
}
res := md.Get(ac.key)
if len(res) == 0 {
return
}
return res[0], true
}
func (s *server) InsertEmojis(ctx context.Context, req *proto.EmojiRequest) (*proto.EmojiResponse, error) {
log.Printf("Client says: %s", req.InputText)
if meta, ok := IsAttributeConditionEnabled(ctx, ac1); ok {
log.Printf("Hi, we got ac in InsertEmojis: %v, with meta:%v\n", ac1, meta)
}
if _, ok := IsAttributeConditionEnabled(ctx, ac2); ok {
log.Printf("Hi, we got ac in InsertEmojis: %v\n", ac2)
}
if _, ok := IsAttributeConditionEnabled(ctx, ac3); ok {
log.Printf("Hi, we got ac in InsertEmojis: %v\n", ac3)
}
outputText := emoji.Sprint(req.InputText)
log.Printf("Response: %s", outputText)
return &proto.EmojiResponse{OutputText: outputText}, nil
}
func (s *server) SayHello(ctx context.Context, req *proto.HelloRequest) (*proto.HelloResponse, error) {
log.Printf("Client: %s greeting!", req.Name)
md, ok := metadata.FromIncomingContext(ctx)
if ok {
log.Println(md)
}
if meta, ok := IsAttributeConditionEnabled(ctx, ac1); ok {
log.Printf("Hi, we got ac in SayHello: %v, with meta: %v\n", ac1, meta)
}
if _, ok := IsAttributeConditionEnabled(ctx, ac2); ok {
log.Printf("Hi, we got ac in SayHello: %v\n", ac2)
}
if _, ok := IsAttributeConditionEnabled(ctx, ac3); ok {
log.Printf("Hi, we got ac in SayHello: %v\n", ac3)
}
name := "service emojis"
log.Printf("Service: %s greeting!", name)
return &proto.HelloResponse{Name: name}, nil
}
func main() {
lis, err := net.Listen("tcp", ":9000")
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}
log.Printf("Listening on %s", lis.Addr())
s := grpc.NewServer()
proto.RegisterEmojiServiceServer(s, &server{})
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("Failed to serve: %v", err)
}
}