1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-01-05 10:20:53 +02:00
go-micro/plugins/wrapper/service
2021-11-11 14:03:34 +00:00
..
go.mod upgrade to go 1.17 (#2346) 2021-11-11 14:03:34 +00:00
go.sum upgrade to go 1.17 (#2346) 2021-11-11 14:03:34 +00:00
README.md add all the plugins 2020-12-26 15:32:45 +00:00
service.go go-micro.dev/v4 (#2305) 2021-10-12 12:55:53 +01:00

Service Wrapper

A lot of the time you need access to the service from within the handler. The service wrapper provides a way to do that. It embeds the service within the context so that it can then be retrieved within the handler.

Usage

When creating your service, add the wrapper like so.

srv := micro.NewService(
	micro.Name("com.example.srv.foo"),
)

srv.Init(
	micro.WrapClient(service.NewClientWrapper(srv))
	micro.WrapHandler(service.NewHandlerWrapper(srv)),
)

Then within your handler it can be accessed like this.

func (e *Example) Handler(ctx context.Context, req *example.Request, rsp *example.Response) error {
	service, ok := micro.FromContext(ctx)
	if !ok {
		return errors.InternalServerError("com.example.srv.foo", "Could not retrieve service")
	}

	// do something with the service
	fmt.Println("Got service", service)
	return nil
}

And if you decide to wrap the client with some other wrapper it becomes accessible there too.

type myWrapper struct {
	client.Client
}

func (m *myWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
	service, ok = micro.FromContext(ctx)
	if !ok {
		return errors.InternalServerError("com.example.srv.foo.mywrapper", "Could not retrieve service")
	}

	// do something with the service
	fmt.Println("Got service", service)

	// now do some call
	return c.Client.Call(ctx, req, rsp, opts...)
}