github.com/annwntech/go-micro/v2@v2.9.5/options.go (about) 1 package micro 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/annwntech/go-micro/v2/auth" 8 "github.com/annwntech/go-micro/v2/broker" 9 "github.com/annwntech/go-micro/v2/client" 10 "github.com/annwntech/go-micro/v2/client/selector" 11 "github.com/annwntech/go-micro/v2/config" 12 "github.com/annwntech/go-micro/v2/config/cmd" 13 "github.com/annwntech/go-micro/v2/debug/profile" 14 "github.com/annwntech/go-micro/v2/debug/trace" 15 "github.com/annwntech/go-micro/v2/registry" 16 "github.com/annwntech/go-micro/v2/runtime" 17 "github.com/annwntech/go-micro/v2/server" 18 "github.com/annwntech/go-micro/v2/store" 19 "github.com/annwntech/go-micro/v2/transport" 20 "github.com/micro/cli/v2" 21 ) 22 23 // Options for micro service 24 type Options struct { 25 Auth auth.Auth 26 Broker broker.Broker 27 Cmd cmd.Cmd 28 Config config.Config 29 Client client.Client 30 Server server.Server 31 Store store.Store 32 Registry registry.Registry 33 Runtime runtime.Runtime 34 Transport transport.Transport 35 Profile profile.Profile 36 37 // Before and After funcs 38 BeforeStart []func() error 39 BeforeStop []func() error 40 AfterStart []func() error 41 AfterStop []func() error 42 43 // Other options for implementations of the interface 44 // can be stored in a context 45 Context context.Context 46 47 Signal bool 48 } 49 50 func newOptions(opts ...Option) Options { 51 opt := Options{ 52 Auth: auth.DefaultAuth, 53 Broker: broker.DefaultBroker, 54 Cmd: cmd.DefaultCmd, 55 Config: config.DefaultConfig, 56 Client: client.DefaultClient, 57 Server: server.DefaultServer, 58 Store: store.DefaultStore, 59 Registry: registry.DefaultRegistry, 60 Runtime: runtime.DefaultRuntime, 61 Transport: transport.DefaultTransport, 62 Context: context.Background(), 63 Signal: true, 64 } 65 66 for _, o := range opts { 67 o(&opt) 68 } 69 70 return opt 71 } 72 73 // Broker to be used for service 74 func Broker(b broker.Broker) Option { 75 return func(o *Options) { 76 o.Broker = b 77 // Update Client and Server 78 o.Client.Init(client.Broker(b)) 79 o.Server.Init(server.Broker(b)) 80 } 81 } 82 83 func Cmd(c cmd.Cmd) Option { 84 return func(o *Options) { 85 o.Cmd = c 86 } 87 } 88 89 // Client to be used for service 90 func Client(c client.Client) Option { 91 return func(o *Options) { 92 o.Client = c 93 } 94 } 95 96 // Context specifies a context for the service. 97 // Can be used to signal shutdown of the service and for extra option values. 98 func Context(ctx context.Context) Option { 99 return func(o *Options) { 100 o.Context = ctx 101 } 102 } 103 104 // HandleSignal toggles automatic installation of the signal handler that 105 // traps TERM, INT, and QUIT. Users of this feature to disable the signal 106 // handler, should control liveness of the service through the context. 107 func HandleSignal(b bool) Option { 108 return func(o *Options) { 109 o.Signal = b 110 } 111 } 112 113 // Profile to be used for debug profile 114 func Profile(p profile.Profile) Option { 115 return func(o *Options) { 116 o.Profile = p 117 } 118 } 119 120 // Server to be used for service 121 func Server(s server.Server) Option { 122 return func(o *Options) { 123 o.Server = s 124 } 125 } 126 127 // Store sets the store to use 128 func Store(s store.Store) Option { 129 return func(o *Options) { 130 o.Store = s 131 } 132 } 133 134 // Registry sets the registry for the service 135 // and the underlying components 136 func Registry(r registry.Registry) Option { 137 return func(o *Options) { 138 o.Registry = r 139 // Update Client and Server 140 o.Client.Init(client.Registry(r)) 141 o.Server.Init(server.Registry(r)) 142 // Update Broker 143 o.Broker.Init(broker.Registry(r)) 144 } 145 } 146 147 // Tracer sets the tracer for the service 148 func Tracer(t trace.Tracer) Option { 149 return func(o *Options) { 150 o.Server.Init(server.Tracer(t)) 151 } 152 } 153 154 // Auth sets the auth for the service 155 func Auth(a auth.Auth) Option { 156 return func(o *Options) { 157 o.Auth = a 158 o.Server.Init(server.Auth(a)) 159 } 160 } 161 162 // Config sets the config for the service 163 func Config(c config.Config) Option { 164 return func(o *Options) { 165 o.Config = c 166 } 167 } 168 169 // Selector sets the selector for the service client 170 func Selector(s selector.Selector) Option { 171 return func(o *Options) { 172 o.Client.Init(client.Selector(s)) 173 } 174 } 175 176 // Transport sets the transport for the service 177 // and the underlying components 178 func Transport(t transport.Transport) Option { 179 return func(o *Options) { 180 o.Transport = t 181 // Update Client and Server 182 o.Client.Init(client.Transport(t)) 183 o.Server.Init(server.Transport(t)) 184 } 185 } 186 187 // Runtime sets the runtime 188 func Runtime(r runtime.Runtime) Option { 189 return func(o *Options) { 190 o.Runtime = r 191 } 192 } 193 194 // Convenience options 195 196 // Address sets the address of the server 197 func Address(addr string) Option { 198 return func(o *Options) { 199 o.Server.Init(server.Address(addr)) 200 } 201 } 202 203 // Name of the service 204 func Name(n string) Option { 205 return func(o *Options) { 206 o.Server.Init(server.Name(n)) 207 } 208 } 209 210 // Version of the service 211 func Version(v string) Option { 212 return func(o *Options) { 213 o.Server.Init(server.Version(v)) 214 } 215 } 216 217 // Metadata associated with the service 218 func Metadata(md map[string]string) Option { 219 return func(o *Options) { 220 o.Server.Init(server.Metadata(md)) 221 } 222 } 223 224 // Flags that can be passed to service 225 func Flags(flags ...cli.Flag) Option { 226 return func(o *Options) { 227 o.Cmd.App().Flags = append(o.Cmd.App().Flags, flags...) 228 } 229 } 230 231 // Action can be used to parse user provided cli options 232 func Action(a func(*cli.Context) error) Option { 233 return func(o *Options) { 234 o.Cmd.App().Action = a 235 } 236 } 237 238 // RegisterTTL specifies the TTL to use when registering the service 239 func RegisterTTL(t time.Duration) Option { 240 return func(o *Options) { 241 o.Server.Init(server.RegisterTTL(t)) 242 } 243 } 244 245 // RegisterInterval specifies the interval on which to re-register 246 func RegisterInterval(t time.Duration) Option { 247 return func(o *Options) { 248 o.Server.Init(server.RegisterInterval(t)) 249 } 250 } 251 252 // WrapClient is a convenience method for wrapping a Client with 253 // some middleware component. A list of wrappers can be provided. 254 // Wrappers are applied in reverse order so the last is executed first. 255 func WrapClient(w ...client.Wrapper) Option { 256 return func(o *Options) { 257 // apply in reverse 258 for i := len(w); i > 0; i-- { 259 o.Client = w[i-1](o.Client) 260 } 261 } 262 } 263 264 // WrapCall is a convenience method for wrapping a Client CallFunc 265 func WrapCall(w ...client.CallWrapper) Option { 266 return func(o *Options) { 267 o.Client.Init(client.WrapCall(w...)) 268 } 269 } 270 271 // WrapHandler adds a handler Wrapper to a list of options passed into the server 272 func WrapHandler(w ...server.HandlerWrapper) Option { 273 return func(o *Options) { 274 var wrappers []server.Option 275 276 for _, wrap := range w { 277 wrappers = append(wrappers, server.WrapHandler(wrap)) 278 } 279 280 // Init once 281 o.Server.Init(wrappers...) 282 } 283 } 284 285 // WrapSubscriber adds a subscriber Wrapper to a list of options passed into the server 286 func WrapSubscriber(w ...server.SubscriberWrapper) Option { 287 return func(o *Options) { 288 var wrappers []server.Option 289 290 for _, wrap := range w { 291 wrappers = append(wrappers, server.WrapSubscriber(wrap)) 292 } 293 294 // Init once 295 o.Server.Init(wrappers...) 296 } 297 } 298 299 // Before and Afters 300 301 // BeforeStart run funcs before service starts 302 func BeforeStart(fn func() error) Option { 303 return func(o *Options) { 304 o.BeforeStart = append(o.BeforeStart, fn) 305 } 306 } 307 308 // BeforeStop run funcs before service stops 309 func BeforeStop(fn func() error) Option { 310 return func(o *Options) { 311 o.BeforeStop = append(o.BeforeStop, fn) 312 } 313 } 314 315 // AfterStart run funcs after service starts 316 func AfterStart(fn func() error) Option { 317 return func(o *Options) { 318 o.AfterStart = append(o.AfterStart, fn) 319 } 320 } 321 322 // AfterStop run funcs after service stops 323 func AfterStop(fn func() error) Option { 324 return func(o *Options) { 325 o.AfterStop = append(o.AfterStop, fn) 326 } 327 }