go-micro.dev/v5@v5.12.0/config/source/nats/options.go (about)

     1  package nats
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	natsgo "github.com/nats-io/nats.go"
     8  	"go-micro.dev/v5/config/source"
     9  )
    10  
    11  type (
    12  	urlKey    struct{}
    13  	bucketKey struct{}
    14  	keyKey    struct{}
    15  )
    16  
    17  // WithUrl sets the nats url.
    18  func WithUrl(a ...string) source.Option {
    19  	return func(o *source.Options) {
    20  		if o.Context == nil {
    21  			o.Context = context.Background()
    22  		}
    23  		o.Context = context.WithValue(o.Context, urlKey{}, a)
    24  	}
    25  }
    26  
    27  // WithBucket sets the nats key.
    28  func WithBucket(a string) source.Option {
    29  	return func(o *source.Options) {
    30  		if o.Context == nil {
    31  			o.Context = context.Background()
    32  		}
    33  		o.Context = context.WithValue(o.Context, bucketKey{}, a)
    34  	}
    35  }
    36  
    37  // WithKey sets the nats key.
    38  func WithKey(a string) source.Option {
    39  	return func(o *source.Options) {
    40  		if o.Context == nil {
    41  			o.Context = context.Background()
    42  		}
    43  		o.Context = context.WithValue(o.Context, keyKey{}, a)
    44  	}
    45  }
    46  
    47  func Client(url string) (natsgo.JetStreamContext, error) {
    48  	nc, err := natsgo.Connect(url)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	return nc.JetStream(natsgo.MaxWait(10 * time.Second))
    54  }