service

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

README

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...)
}

Documentation

Overview

Package wrapper injects a go-micro.Service into the context

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClientWrapper

func NewClientWrapper(service micro.Service) client.Wrapper

NewClientWrapper wraps a service within a client so it can be accessed by subsequent client wrappers.

func NewHandlerWrapper

func NewHandlerWrapper(service micro.Service) server.HandlerWrapper

NewHandlerWrapper wraps a service within the handler so it can be accessed by the handler itself.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL