2019-06-03 20:44:43 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
2021-01-29 15:13:56 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2020-09-20 16:08:45 +03:00
|
|
|
"github.com/unistack-org/micro/v3/server"
|
2019-06-03 20:44:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type rpcHandler struct {
|
|
|
|
name string
|
|
|
|
handler interface{}
|
2021-01-29 15:13:56 +03:00
|
|
|
endpoints []*register.Endpoint
|
2019-06-03 20:44:43 +03:00
|
|
|
opts server.HandlerOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRpcHandler(handler interface{}, opts ...server.HandlerOption) server.Handler {
|
2020-11-03 02:15:31 +03:00
|
|
|
options := server.NewHandlerOptions(opts...)
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
typ := reflect.TypeOf(handler)
|
|
|
|
hdlr := reflect.ValueOf(handler)
|
|
|
|
name := reflect.Indirect(hdlr).Type().Name()
|
|
|
|
|
2021-01-29 15:13:56 +03:00
|
|
|
var endpoints []*register.Endpoint
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
for m := 0; m < typ.NumMethod(); m++ {
|
2021-01-29 15:13:56 +03:00
|
|
|
if e := register.ExtractEndpoint(typ.Method(m)); e != nil {
|
2019-06-03 20:44:43 +03:00
|
|
|
e.Name = name + "." + e.Name
|
|
|
|
|
|
|
|
for k, v := range options.Metadata[e.Name] {
|
|
|
|
e.Metadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints = append(endpoints, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rpcHandler{
|
|
|
|
name: name,
|
|
|
|
handler: handler,
|
|
|
|
endpoints: endpoints,
|
|
|
|
opts: options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcHandler) Name() string {
|
|
|
|
return r.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcHandler) Handler() interface{} {
|
|
|
|
return r.handler
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:13:56 +03:00
|
|
|
func (r *rpcHandler) Endpoints() []*register.Endpoint {
|
2019-06-03 20:44:43 +03:00
|
|
|
return r.endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcHandler) Options() server.HandlerOptions {
|
|
|
|
return r.opts
|
|
|
|
}
|