1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00
go-micro/examples/template/api/client/example.go
2020-12-26 15:21:29 +00:00

30 lines
843 B
Go

package client
import (
"context"
example "github.com/micro/go-micro/examples/template/srv/proto/example"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/server"
)
type exampleKey struct{}
// FromContext retrieves the client from the Context
func ExampleFromContext(ctx context.Context) (example.ExampleService, bool) {
c, ok := ctx.Value(exampleKey{}).(example.ExampleService)
return c, ok
}
// Client returns a wrapper for the ExampleClient
func ExampleWrapper(service micro.Service) server.HandlerWrapper {
client := example.NewExampleService("go.micro.srv.template", service.Client())
return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
ctx = context.WithValue(ctx, exampleKey{}, client)
return fn(ctx, req, rsp)
}
}
}