2020-07-27 15:22:00 +03:00
|
|
|
package mucp
|
2015-06-03 03:25:37 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/registry"
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
2015-06-03 03:25:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type rpcHandler struct {
|
|
|
|
name string
|
|
|
|
handler interface{}
|
|
|
|
endpoints []*registry.Endpoint
|
2020-07-27 15:22:00 +03:00
|
|
|
opts server.HandlerOptions
|
2015-06-03 03:25:37 +03:00
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
func newRpcHandler(handler interface{}, opts ...server.HandlerOption) server.Handler {
|
|
|
|
options := server.HandlerOptions{
|
2016-05-26 20:01:02 +03:00
|
|
|
Metadata: make(map[string]map[string]string),
|
|
|
|
}
|
|
|
|
|
2016-01-08 17:02:32 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:25:37 +03:00
|
|
|
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 {
|
2015-10-11 14:05:20 +03:00
|
|
|
e.Name = name + "." + e.Name
|
2016-05-26 20:01:02 +03:00
|
|
|
|
|
|
|
for k, v := range options.Metadata[e.Name] {
|
|
|
|
e.Metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2015-06-03 03:25:37 +03:00
|
|
|
endpoints = append(endpoints, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rpcHandler{
|
|
|
|
name: name,
|
|
|
|
handler: handler,
|
|
|
|
endpoints: endpoints,
|
2016-01-08 17:02:32 +03:00
|
|
|
opts: options,
|
2015-06-03 03:25:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2016-01-08 17:02:32 +03:00
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
func (r *rpcHandler) Options() server.HandlerOptions {
|
2016-01-08 17:02:32 +03:00
|
|
|
return r.opts
|
|
|
|
}
|