micro/server/rpc_handler.go

46 lines
807 B
Go

package server
import (
"reflect"
"github.com/myodc/go-micro/registry"
)
type rpcHandler struct {
name string
handler interface{}
endpoints []*registry.Endpoint
}
func newRpcHandler(handler interface{}) Handler {
typ := reflect.TypeOf(handler)
hdlr := reflect.ValueOf(handler)
name := reflect.Indirect(hdlr).Type().Name()
var endpoints []*registry.Endpoint
for m := 0; m < typ.NumMethod(); m++ {
if e := extractEndpoint(typ.Method(m)); e != nil {
endpoints = append(endpoints, e)
}
}
return &rpcHandler{
name: name,
handler: handler,
endpoints: endpoints,
}
}
func (r *rpcHandler) Name() string {
return r.name
}
func (r *rpcHandler) Handler() interface{} {
return r.handler
}
func (r *rpcHandler) Endpoints() []*registry.Endpoint {
return r.endpoints
}