github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/edge/handlers.go (about) 1 package edge 2 3 import ( 4 "github.com/ronaksoft/rony" 5 ) 6 7 /* 8 Creation Time: 2021 - Jan - 02 9 Created by: (ehsan) 10 Maintainers: 11 1. Ehsan N. Moosa (E2) 12 Auditor: Ehsan N. Moosa (E2) 13 Copyright Ronak Software Group 2020 14 */ 15 16 type ( 17 Handler = func(ctx *RequestCtx, in *rony.MessageEnvelope) 18 ) 19 20 // HandlerOption is a structure holds all the information required for a handler. 21 type HandlerOption struct { 22 constructors map[uint64]struct{} 23 handlers []Handler 24 tunnel bool 25 gateway bool 26 serviceName string 27 methodName string 28 29 // internal use 30 builtin bool 31 } 32 33 func NewHandlerOptions() *HandlerOption { 34 return &HandlerOption{ 35 constructors: map[uint64]struct{}{}, 36 handlers: nil, 37 gateway: true, 38 tunnel: true, 39 } 40 } 41 42 func (ho *HandlerOption) setBuiltin() *HandlerOption { 43 ho.builtin = true 44 45 return ho 46 } 47 48 func (ho *HandlerOption) SetConstructor(constructors ...uint64) *HandlerOption { 49 for _, c := range constructors { 50 ho.constructors[c] = struct{}{} 51 } 52 53 return ho 54 } 55 56 // SetServiceName set the service name for this handler, this is mostly used for Tracing. 57 func (ho *HandlerOption) SetServiceName(serviceName string) *HandlerOption { 58 ho.serviceName = serviceName 59 60 return ho 61 } 62 63 // SetMethodName set the method name for this handler, this is mostly used for Tracing. 64 func (ho *HandlerOption) SetMethodName(methodName string) *HandlerOption { 65 ho.methodName = methodName 66 67 return ho 68 } 69 70 // SetHandler replaces the handlers for this constructor with h 71 func (ho *HandlerOption) SetHandler(h ...Handler) *HandlerOption { 72 ho.handlers = append(ho.handlers[:0], h...) 73 74 return ho 75 } 76 77 // GatewayOnly makes this method only available through gateway messages. 78 func (ho *HandlerOption) GatewayOnly() *HandlerOption { 79 ho.tunnel = false 80 ho.gateway = true 81 82 return ho 83 } 84 85 // TunnelOnly makes this method only available through tunnel messages. 86 func (ho *HandlerOption) TunnelOnly() *HandlerOption { 87 ho.tunnel = true 88 ho.gateway = false 89 90 return ho 91 } 92 93 // Prepend adds the h handlers before already set handlers 94 func (ho *HandlerOption) Prepend(h ...Handler) *HandlerOption { 95 nh := make([]Handler, 0, len(ho.handlers)+len(h)) 96 nh = append(nh, h...) 97 nh = append(nh, ho.handlers...) 98 ho.handlers = nh 99 100 return ho 101 } 102 103 // Append adds the h handlers after already set handlers 104 func (ho *HandlerOption) Append(h ...Handler) *HandlerOption { 105 ho.handlers = append(ho.handlers, h...) 106 107 return ho 108 }