@@ -18,32 +18,77 @@ package helloworld
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
21
22
rawGRPC "google.golang.org/grpc"
22
23
pb "google.golang.org/grpc/examples/helloworld/helloworld"
24
+ "mosn.io/layotto/cmd/layotto_multiple_api/helloworld/component"
25
+ "mosn.io/layotto/components/lock"
23
26
"mosn.io/layotto/pkg/grpc"
24
27
grpc_api "mosn.io/layotto/pkg/grpc"
25
- mgrpc "mosn.io/mosn/ pkg/filter/network/grpc "
28
+ "mosn.io/pkg/log "
26
29
)
27
30
31
+ const componentType = "helloworld"
32
+
33
+ // This demo will always use this component name.
34
+ const componentName = "in-memory"
35
+
28
36
func NewHelloWorldAPI (ac * grpc_api.ApplicationContext ) grpc.GrpcAPI {
29
- return & server {}
37
+ // 1. convert custom components
38
+ name2component := make (map [string ]component.HelloWorld )
39
+ if len (ac .CustomComponent ) != 0 {
40
+ // we only care about those components of type "helloworld"
41
+ name2comp , ok := ac .CustomComponent [componentType ]
42
+ if ok && len (name2comp ) > 0 {
43
+ for name , v := range name2comp {
44
+ // convert them using type assertion
45
+ comp , ok := v .(component.HelloWorld )
46
+ if ! ok {
47
+ errMsg := fmt .Sprintf ("custom component %s does not implement HelloWorld interface" , name )
48
+ log .DefaultLogger .Errorf (errMsg )
49
+ }
50
+ name2component [name ] = comp
51
+ }
52
+ }
53
+ }
54
+ // 2. construct your API implementation
55
+ return & server {
56
+ appId : ac .AppId ,
57
+ // Your API plugin can store and use all the components.
58
+ // For example,this demo set all the LockStore components here.
59
+ name2LockStore : ac .LockStores ,
60
+ // Custom components of type "helloworld"
61
+ name2component : name2component ,
62
+ }
30
63
}
31
64
32
65
// server is used to implement helloworld.GreeterServer.
33
66
type server struct {
67
+ appId string
68
+ // custom components which implements the `HelloWorld` interface
69
+ name2component map [string ]component.HelloWorld
70
+ // LockStore components. They are not used in this demo, we put them here as a demo.
71
+ name2LockStore map [string ]lock.LockStore
34
72
pb.UnimplementedGreeterServer
35
73
}
36
74
37
- func (s * server ) Init (conn * rawGRPC.ClientConn ) error {
38
- return nil
75
+ // SayHello implements helloworld.GreeterServer.SayHello
76
+ func (s * server ) SayHello (ctx context.Context , in * pb.HelloRequest ) (* pb.HelloReply , error ) {
77
+ if _ , ok := s .name2component [componentName ]; ! ok {
78
+ return & pb.HelloReply {Message : "We don't want to talk with you!" }, nil
79
+ }
80
+ message , err := s .name2component [componentName ].SayHello (in .GetName ())
81
+ if err != nil {
82
+ return nil , err
83
+ }
84
+ return & pb.HelloReply {Message : message }, nil
39
85
}
40
86
41
- func (s * server ) Register (grpcServer * rawGRPC.Server , registeredServer mgrpc.RegisteredServer ) (mgrpc.RegisteredServer , error ) {
42
- pb .RegisterGreeterServer (grpcServer , s )
43
- return registeredServer , nil
87
+ func (s * server ) Init (conn * rawGRPC.ClientConn ) error {
88
+ return nil
44
89
}
45
90
46
- // SayHello implements helloworld.GreeterServer
47
- func ( s * server ) SayHello ( ctx context. Context , in * pb.HelloRequest ) ( * pb. HelloReply , error ) {
48
- return & pb. HelloReply { Message : "Hello " + in . GetName ()}, nil
91
+ func ( s * server ) Register ( rawGrpcServer * rawGRPC. Server ) error {
92
+ pb .RegisterGreeterServer ( rawGrpcServer , s )
93
+ return nil
49
94
}
0 commit comments