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