2015-06-03 02:25:37 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
2021-10-12 13:55:53 +02:00
|
|
|
"go-micro.dev/v4/registry"
|
2015-06-03 02:25:37 +02:00
|
|
|
)
|
|
|
|
|
2022-10-20 13:00:50 +02:00
|
|
|
type RpcHandler struct {
|
2015-06-03 02:25:37 +02:00
|
|
|
handler interface{}
|
2016-01-08 16:02:32 +02:00
|
|
|
opts HandlerOptions
|
2023-04-26 02:16:34 +02:00
|
|
|
name string
|
|
|
|
endpoints []*registry.Endpoint
|
2015-06-03 02:25:37 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 13:00:50 +02:00
|
|
|
func NewRpcHandler(handler interface{}, opts ...HandlerOption) Handler {
|
2016-05-26 19:01:02 +02:00
|
|
|
options := HandlerOptions{
|
|
|
|
Metadata: make(map[string]map[string]string),
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:02:32 +02:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:25:37 +02: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 13:05:20 +02:00
|
|
|
e.Name = name + "." + e.Name
|
2016-05-26 19:01:02 +02:00
|
|
|
|
|
|
|
for k, v := range options.Metadata[e.Name] {
|
|
|
|
e.Metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
endpoints = append(endpoints, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:00:50 +02:00
|
|
|
return &RpcHandler{
|
2015-06-03 02:25:37 +02:00
|
|
|
name: name,
|
|
|
|
handler: handler,
|
|
|
|
endpoints: endpoints,
|
2016-01-08 16:02:32 +02:00
|
|
|
opts: options,
|
2015-06-03 02:25:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:00:50 +02:00
|
|
|
func (r *RpcHandler) Name() string {
|
2015-06-03 02:25:37 +02:00
|
|
|
return r.name
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:00:50 +02:00
|
|
|
func (r *RpcHandler) Handler() interface{} {
|
2015-06-03 02:25:37 +02:00
|
|
|
return r.handler
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:00:50 +02:00
|
|
|
func (r *RpcHandler) Endpoints() []*registry.Endpoint {
|
2015-06-03 02:25:37 +02:00
|
|
|
return r.endpoints
|
|
|
|
}
|
2016-01-08 16:02:32 +02:00
|
|
|
|
2022-10-20 13:00:50 +02:00
|
|
|
func (r *RpcHandler) Options() HandlerOptions {
|
2016-01-08 16:02:32 +02:00
|
|
|
return r.opts
|
|
|
|
}
|