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
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2020-10-16 09:38:57 +03:00
|
|
|
)
|
2019-01-24 00:46:26 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
type rpcHandler struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
opts HandlerOptions
|
2020-10-16 09:38:57 +03:00
|
|
|
handler interface{}
|
2021-03-06 19:45:13 +03:00
|
|
|
name string
|
2021-01-29 13:17:32 +03:00
|
|
|
endpoints []*register.Endpoint
|
2016-01-08 14:02:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
func newRPCHandler(handler interface{}, opts ...HandlerOption) Handler {
|
2020-10-16 09:38:57 +03:00
|
|
|
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
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
var endpoints []*register.Endpoint
|
2020-09-05 02:11:29 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
for m := 0; m < typ.NumMethod(); m++ {
|
2021-01-29 13:17:32 +03:00
|
|
|
if e := register.ExtractEndpoint(typ.Method(m)); e != nil {
|
2020-10-16 09:38:57 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (r *rpcHandler) Endpoints() []*register.Endpoint {
|
2020-10-16 09:38:57 +03:00
|
|
|
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
|
|
|
}
|