github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/server/mucp/rpc_handler.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-micro/v3/server/mucp/rpc_handler.go 16 17 package mucp 18 19 import ( 20 "reflect" 21 22 "github.com/tickoalcantara12/micro/v3/service/registry" 23 "github.com/tickoalcantara12/micro/v3/service/server" 24 ) 25 26 type rpcHandler struct { 27 name string 28 handler interface{} 29 endpoints []*registry.Endpoint 30 opts server.HandlerOptions 31 } 32 33 func newRpcHandler(handler interface{}, opts ...server.HandlerOption) server.Handler { 34 options := server.HandlerOptions{ 35 Metadata: make(map[string]map[string]string), 36 } 37 38 for _, o := range opts { 39 o(&options) 40 } 41 42 typ := reflect.TypeOf(handler) 43 hdlr := reflect.ValueOf(handler) 44 name := reflect.Indirect(hdlr).Type().Name() 45 46 var endpoints []*registry.Endpoint 47 48 for m := 0; m < typ.NumMethod(); m++ { 49 if e := extractEndpoint(typ.Method(m)); e != nil { 50 e.Name = name + "." + e.Name 51 52 for k, v := range options.Metadata[e.Name] { 53 e.Metadata[k] = v 54 } 55 56 endpoints = append(endpoints, e) 57 } 58 } 59 60 return &rpcHandler{ 61 name: name, 62 handler: handler, 63 endpoints: endpoints, 64 opts: options, 65 } 66 } 67 68 func (r *rpcHandler) Name() string { 69 return r.name 70 } 71 72 func (r *rpcHandler) Handler() interface{} { 73 return r.handler 74 } 75 76 func (r *rpcHandler) Endpoints() []*registry.Endpoint { 77 return r.endpoints 78 } 79 80 func (r *rpcHandler) Options() server.HandlerOptions { 81 return r.opts 82 }