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