Set endpoint metadata in the http server

This commit is contained in:
Asim Aslam 2017-04-03 15:03:46 +01:00 committed by Vasiliy Tolstov
parent 31f1702ab2
commit 2327e1d4e8
2 changed files with 21 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import (
type httpHandler struct { type httpHandler struct {
opts server.HandlerOptions opts server.HandlerOptions
eps []*registry.Endpoint
hd interface{} hd interface{}
} }
@ -19,7 +20,7 @@ func (h *httpHandler) Handler() interface{} {
} }
func (h *httpHandler) Endpoints() []*registry.Endpoint { func (h *httpHandler) Endpoints() []*registry.Endpoint {
return []*registry.Endpoint{} return h.eps
} }
func (h *httpHandler) Options() server.HandlerOptions { func (h *httpHandler) Options() server.HandlerOptions {

21
http.go
View File

@ -50,14 +50,29 @@ func (h *httpServer) Handle(handler server.Handler) error {
} }
func (h *httpServer) NewHandler(handler interface{}, opts ...server.HandlerOption) server.Handler { func (h *httpServer) NewHandler(handler interface{}, opts ...server.HandlerOption) server.Handler {
var options server.HandlerOptions options := server.HandlerOptions{
Metadata: make(map[string]map[string]string),
}
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)
} }
var eps []*registry.Endpoint
if !options.Internal {
for name, metadata := range options.Metadata {
eps = append(eps, &registry.Endpoint{
Name: name,
Metadata: metadata,
})
}
}
return &httpHandler{ return &httpHandler{
opts: options, eps: eps,
hd: handler, hd: handler,
opts: options,
} }
} }
@ -81,9 +96,11 @@ func (h *httpServer) Subscribe(s server.Subscriber) error {
func (h *httpServer) Register() error { func (h *httpServer) Register() error {
h.Lock() h.Lock()
opts := h.opts opts := h.opts
eps := h.hd.Endpoints()
h.Unlock() h.Unlock()
service := serviceDef(opts) service := serviceDef(opts)
service.Endpoints = eps
rOpts := []registry.RegisterOption{ rOpts := []registry.RegisterOption{
registry.RegisterTTL(opts.RegisterTTL), registry.RegisterTTL(opts.RegisterTTL),