2015-06-03 01:25:37 +01:00
|
|
|
package server
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
import (
|
|
|
|
"reflect"
|
2019-01-24 00:46:26 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
"github.com/unistack-org/micro/v3/registry"
|
|
|
|
)
|
2019-01-24 00:46:26 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
type rpcHandler struct {
|
|
|
|
name string
|
|
|
|
handler interface{}
|
|
|
|
endpoints []*registry.Endpoint
|
|
|
|
opts HandlerOptions
|
2016-01-08 14:02:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
func newRpcHandler(handler interface{}, opts ...HandlerOption) Handler {
|
|
|
|
options := NewHandlerOptions(opts...)
|
2020-09-20 16:57:54 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
typ := reflect.TypeOf(handler)
|
|
|
|
hdlr := reflect.ValueOf(handler)
|
|
|
|
name := reflect.Indirect(hdlr).Type().Name()
|
2020-09-20 16:57:54 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
var endpoints []*registry.Endpoint
|
2020-09-05 02:11:29 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
for m := 0; m < typ.NumMethod(); m++ {
|
|
|
|
if e := registry.ExtractEndpoint(typ.Method(m)); e != nil {
|
|
|
|
e.Name = name + "." + e.Name
|
2019-01-24 00:46:26 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
for k, v := range options.Metadata[e.Name] {
|
|
|
|
e.Metadata[k] = v
|
|
|
|
}
|
2016-01-08 14:02:32 +00:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
endpoints = append(endpoints, e)
|
|
|
|
}
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
return &rpcHandler{
|
|
|
|
name: name,
|
|
|
|
handler: handler,
|
|
|
|
endpoints: endpoints,
|
|
|
|
opts: options,
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
func (r *rpcHandler) Name() string {
|
|
|
|
return r.name
|
2016-05-26 20:43:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
func (r *rpcHandler) Handler() interface{} {
|
|
|
|
return r.handler
|
2016-01-08 14:02:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
func (r *rpcHandler) Endpoints() []*registry.Endpoint {
|
|
|
|
return r.endpoints
|
2015-06-12 19:52:27 +01:00
|
|
|
}
|
2016-01-22 21:48:43 +00:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
func (r *rpcHandler) Options() HandlerOptions {
|
|
|
|
return r.opts
|
2019-01-24 00:46:26 +03:00
|
|
|
}
|