1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00
go-micro/server/rpc_handler.go

66 lines
1.1 KiB
Go
Raw Normal View History

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