gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/server/options.go (about) 1 package server 2 3 import ( 4 "context" 5 "sync" 6 "time" 7 8 "gitee.com/liuxuezhan/go-micro-v1.18.0/broker" 9 "gitee.com/liuxuezhan/go-micro-v1.18.0/codec" 10 "gitee.com/liuxuezhan/go-micro-v1.18.0/registry" 11 "gitee.com/liuxuezhan/go-micro-v1.18.0/transport" 12 ) 13 14 type Options struct { 15 Codecs map[string]codec.NewCodec 16 Broker broker.Broker 17 Registry registry.Registry 18 Transport transport.Transport 19 Metadata map[string]string 20 Name string 21 Address string 22 Advertise string 23 Id string 24 Version string 25 HdlrWrappers []HandlerWrapper 26 SubWrappers []SubscriberWrapper 27 28 // RegisterCheck runs a check function before registering the service 29 RegisterCheck func(context.Context) error 30 // The register expiry time 31 RegisterTTL time.Duration 32 // The interval on which to register 33 RegisterInterval time.Duration 34 35 // The router for requests 36 Router Router 37 38 // Other options for implementations of the interface 39 // can be stored in a context 40 Context context.Context 41 } 42 43 func newOptions(opt ...Option) Options { 44 opts := Options{ 45 Codecs: make(map[string]codec.NewCodec), 46 Metadata: map[string]string{}, 47 RegisterInterval: DefaultRegisterInterval, 48 RegisterTTL: DefaultRegisterTTL, 49 } 50 51 for _, o := range opt { 52 o(&opts) 53 } 54 55 if opts.Broker == nil { 56 opts.Broker = broker.DefaultBroker 57 } 58 59 if opts.Registry == nil { 60 opts.Registry = registry.DefaultRegistry 61 } 62 63 if opts.Transport == nil { 64 opts.Transport = transport.DefaultTransport 65 } 66 67 if opts.RegisterCheck == nil { 68 opts.RegisterCheck = DefaultRegisterCheck 69 } 70 71 if len(opts.Address) == 0 { 72 opts.Address = DefaultAddress 73 } 74 75 if len(opts.Name) == 0 { 76 opts.Name = DefaultName 77 } 78 79 if len(opts.Id) == 0 { 80 opts.Id = DefaultId 81 } 82 83 if len(opts.Version) == 0 { 84 opts.Version = DefaultVersion 85 } 86 87 return opts 88 } 89 90 // Server name 91 func Name(n string) Option { 92 return func(o *Options) { 93 o.Name = n 94 } 95 } 96 97 // Unique server id 98 func Id(id string) Option { 99 return func(o *Options) { 100 o.Id = id 101 } 102 } 103 104 // Version of the service 105 func Version(v string) Option { 106 return func(o *Options) { 107 o.Version = v 108 } 109 } 110 111 // Address to bind to - host:port 112 func Address(a string) Option { 113 return func(o *Options) { 114 o.Address = a 115 } 116 } 117 118 // The address to advertise for discovery - host:port 119 func Advertise(a string) Option { 120 return func(o *Options) { 121 o.Advertise = a 122 } 123 } 124 125 // Broker to use for pub/sub 126 func Broker(b broker.Broker) Option { 127 return func(o *Options) { 128 o.Broker = b 129 } 130 } 131 132 // Codec to use to encode/decode requests for a given content type 133 func Codec(contentType string, c codec.NewCodec) Option { 134 return func(o *Options) { 135 o.Codecs[contentType] = c 136 } 137 } 138 139 // Registry used for discovery 140 func Registry(r registry.Registry) Option { 141 return func(o *Options) { 142 o.Registry = r 143 } 144 } 145 146 // Transport mechanism for communication e.g http, rabbitmq, etc 147 func Transport(t transport.Transport) Option { 148 return func(o *Options) { 149 o.Transport = t 150 } 151 } 152 153 // Metadata associated with the server 154 func Metadata(md map[string]string) Option { 155 return func(o *Options) { 156 o.Metadata = md 157 } 158 } 159 160 // RegisterCheck run func before registry service 161 func RegisterCheck(fn func(context.Context) error) Option { 162 return func(o *Options) { 163 o.RegisterCheck = fn 164 } 165 } 166 167 // Register the service with a TTL 168 func RegisterTTL(t time.Duration) Option { 169 return func(o *Options) { 170 o.RegisterTTL = t 171 } 172 } 173 174 // Register the service with at interval 175 func RegisterInterval(t time.Duration) Option { 176 return func(o *Options) { 177 o.RegisterInterval = t 178 } 179 } 180 181 // WithRouter sets the request router 182 func WithRouter(r Router) Option { 183 return func(o *Options) { 184 o.Router = r 185 } 186 } 187 188 // Wait tells the server to wait for requests to finish before exiting 189 // If `wg` is nil, server only wait for completion of rpc handler. 190 // For user need finer grained control, pass a concrete `wg` here, server will 191 // wait against it on stop. 192 func Wait(wg *sync.WaitGroup) Option { 193 return func(o *Options) { 194 if o.Context == nil { 195 o.Context = context.Background() 196 } 197 if wg == nil { 198 wg = new(sync.WaitGroup) 199 } 200 o.Context = context.WithValue(o.Context, "wait", wg) 201 } 202 } 203 204 // Adds a handler Wrapper to a list of options passed into the server 205 func WrapHandler(w HandlerWrapper) Option { 206 return func(o *Options) { 207 o.HdlrWrappers = append(o.HdlrWrappers, w) 208 } 209 } 210 211 // Adds a subscriber Wrapper to a list of options passed into the server 212 func WrapSubscriber(w SubscriberWrapper) Option { 213 return func(o *Options) { 214 o.SubWrappers = append(o.SubWrappers, w) 215 } 216 }