2019-06-03 18:44:43 +01:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
2021-10-27 01:00:27 +03:00
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type rpcHandler struct {
|
2021-04-26 19:04:27 +03:00
|
|
|
opts server.HandlerOptions
|
2019-06-03 18:44:43 +01:00
|
|
|
handler interface{}
|
2021-04-26 19:04:27 +03:00
|
|
|
name string
|
2021-01-29 15:13:56 +03:00
|
|
|
endpoints []*register.Endpoint
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
2021-04-26 19:04:27 +03:00
|
|
|
func newRPCHandler(handler interface{}, opts ...server.HandlerOption) server.Handler {
|
2020-11-03 02:15:31 +03:00
|
|
|
options := server.NewHandlerOptions(opts...)
|
2019-06-03 18:44:43 +01: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 18:44:43 +01: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 18:44:43 +01: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 18:44:43 +01:00
|
|
|
return r.endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcHandler) Options() server.HandlerOptions {
|
|
|
|
return r.opts
|
|
|
|
}
|