github.com/erda-project/erda-infra@v1.0.9/examples/service/server/helloworld/provider.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package example 16 17 import ( 18 "context" 19 20 "github.com/erda-project/erda-infra/base/logs" 21 "github.com/erda-project/erda-infra/base/servicehub" 22 "github.com/erda-project/erda-infra/examples/service/protocol/pb" 23 "github.com/erda-project/erda-infra/pkg/transport" 24 "github.com/erda-project/erda-infra/pkg/transport/interceptor" 25 ) 26 27 type config struct { 28 } 29 30 // +provider 31 type provider struct { 32 Cfg *config 33 Log logs.Logger 34 Register transport.Register 35 greeterService *greeterService 36 userService *userService 37 } 38 39 func (p *provider) Init(ctx servicehub.Context) error { 40 // TODO initialize something ... 41 42 if p.Register != nil { 43 p.greeterService = &greeterService{p} 44 pb.RegisterGreeterServiceImp(p.Register, p.greeterService, 45 transport.WithInterceptors(func(h interceptor.Handler) interceptor.Handler { 46 p.Log.Info("wrap greeterService methods") 47 return func(ctx context.Context, req interface{}) (interface{}, error) { 48 info := transport.ContextServiceInfo(ctx) 49 p.Log.Infof("before %s/%s\n", info.Service(), info.Method()) 50 header := transport.ContextHeader(ctx) 51 p.Log.Info("header: ", header) 52 p.Log.Info(req) 53 out, err := h(ctx, req) 54 p.Log.Infof("after %s/%s\n", info.Service(), info.Method()) 55 return out, err 56 } 57 }), 58 ) 59 60 p.userService = &userService{p} 61 pb.RegisterUserServiceImp(p.Register, p.userService, 62 transport.WithInterceptors(func(h interceptor.Handler) interceptor.Handler { 63 p.Log.Info("wrap userService methods") 64 return func(ctx context.Context, req interface{}) (interface{}, error) { 65 info := transport.ContextServiceInfo(ctx) 66 p.Log.Infof("before %s/%s\n", info.Service(), info.Method()) 67 header := transport.ContextHeader(ctx) 68 p.Log.Info("header: ", header) 69 p.Log.Info(req) 70 out, err := h(ctx, req) 71 p.Log.Infof("after %s/%s\n", info.Service(), info.Method()) 72 return out, err 73 } 74 }), 75 ) 76 } 77 return nil 78 } 79 80 func (p *provider) Provide(ctx servicehub.DependencyContext, args ...interface{}) interface{} { 81 switch { 82 case ctx.Service() == "erda.infra.example.GreeterService" || ctx.Type() == pb.GreeterServiceServerType() || ctx.Type() == pb.GreeterServiceHandlerType(): 83 return p.greeterService 84 case ctx.Service() == "erda.infra.example.UserService" || ctx.Type() == pb.UserServiceServerType() || ctx.Type() == pb.UserServiceHandlerType(): 85 return p.userService 86 } 87 return p 88 } 89 90 func init() { 91 servicehub.Register("erda.infra.example", &servicehub.Spec{ 92 Services: pb.ServiceNames(), 93 Types: pb.Types(), 94 OptionalDependencies: []string{"service-register"}, 95 Description: "", 96 ConfigFunc: func() interface{} { 97 return &config{} 98 }, 99 Creator: func() servicehub.Provider { 100 return &provider{} 101 }, 102 }) 103 }