Merge pull request #93 from micro/endpoint_metadata

Support endpoint metadata
This commit is contained in:
Asim Aslam 2016-05-26 20:43:29 +01:00
commit 257333a0a3
2 changed files with 18 additions and 1 deletions

View File

@ -34,6 +34,7 @@ type Subscriber interface {
type HandlerOptions struct { type HandlerOptions struct {
Internal bool Internal bool
Metadata map[string]map[string]string
} }
type SubscriberOptions struct { type SubscriberOptions struct {
@ -41,6 +42,14 @@ type SubscriberOptions struct {
Internal bool Internal bool
} }
// EndpointMetadata is a Handler option that allows metadata to be added to
// individual endpoints.
func EndpointMetadata(name string, md map[string]string) HandlerOption {
return func(o *HandlerOptions) {
o.Metadata[name] = md
}
}
// Internal Handler options specifies that a handler is not advertised // Internal Handler options specifies that a handler is not advertised
// to the discovery system. In the future this may also limit request // to the discovery system. In the future this may also limit request
// to the internal network or authorised user. // to the internal network or authorised user.

View File

@ -14,7 +14,10 @@ type rpcHandler struct {
} }
func newRpcHandler(handler interface{}, opts ...HandlerOption) Handler { func newRpcHandler(handler interface{}, opts ...HandlerOption) Handler {
var options HandlerOptions options := HandlerOptions{
Metadata: make(map[string]map[string]string),
}
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)
} }
@ -28,6 +31,11 @@ func newRpcHandler(handler interface{}, opts ...HandlerOption) Handler {
for m := 0; m < typ.NumMethod(); m++ { for m := 0; m < typ.NumMethod(); m++ {
if e := extractEndpoint(typ.Method(m)); e != nil { if e := extractEndpoint(typ.Method(m)); e != nil {
e.Name = name + "." + e.Name e.Name = name + "." + e.Name
for k, v := range options.Metadata[e.Name] {
e.Metadata[k] = v
}
endpoints = append(endpoints, e) endpoints = append(endpoints, e)
} }
} }