1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-01-05 10:20:53 +02:00
go-micro/examples/wrapper/cli/main.go
2020-12-26 15:17:20 +00:00

47 lines
992 B
Go

package main
import (
"fmt"
"context"
proto "github.com/micro/examples/service/proto"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/client"
)
// log wrapper logs every time a request is made
type logWrapper struct {
client.Client
}
func (l *logWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
fmt.Printf("[wrapper] client request service: %s method: %s\n", req.Service(), req.Endpoint())
return l.Client.Call(ctx, req, rsp)
}
// Implements client.Wrapper as logWrapper
func logWrap(c client.Client) client.Client {
return &logWrapper{c}
}
func main() {
service := micro.NewService(
micro.Name("greeter.client"),
// wrap the client
micro.WrapClient(logWrap),
)
service.Init()
greeter := proto.NewGreeterService("greeter", service.Client())
rsp, err := greeter.Hello(context.TODO(), &proto.Request{Name: "John"})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(rsp.Greeting)
}