forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
37 lines (31 loc) · 1.1 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Package wrapper injects a go-micro.Service into the context
package service
import (
"github.com/micro/go-micro"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/server"
"context"
)
type clientWrapper struct {
service micro.Service
client.Client
}
func (c *clientWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
ctx = micro.NewContext(ctx, c.service)
return c.Client.Call(ctx, req, rsp, opts...)
}
// NewClientWrapper wraps a service within a client so it can be accessed by subsequent client wrappers.
func NewClientWrapper(service micro.Service) client.Wrapper {
return func(c client.Client) client.Client {
return &clientWrapper{service, c}
}
}
// NewHandlerWrapper wraps a service within the handler so it can be accessed by the handler itself.
func NewHandlerWrapper(service micro.Service) server.HandlerWrapper {
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
ctx = micro.NewContext(ctx, service)
return h(ctx, req, rsp)
}
}
}