github.com/tachunwu/scale@v0.0.0-20230215135019-20d7de63a560/pkg/jetstream/jetstream.go (about) 1 package jetstream 2 3 import ( 4 "github.com/nats-io/nats-server/v2/server" 5 natsserver "github.com/nats-io/nats-server/v2/server" 6 "github.com/nats-io/nats.go" 7 "go.uber.org/zap" 8 ) 9 10 var BucketName string = "materialize" 11 12 func NewJetStream() *natsserver.Server { 13 // Enable logger 14 logger, _ := zap.NewProduction() 15 defer logger.Sync() 16 17 // Setting server 18 opts := &server.Options{ 19 JetStream: true, 20 NoLog: true, 21 Trace: true, 22 NoSigs: true, 23 StoreDir: "./js", 24 } 25 26 // Initialize new server with options 27 ns, err := server.NewServer(opts) 28 29 if err != nil { 30 panic(err) 31 } 32 33 // Start the server via goroutine 34 ns.ConfigureLogger() 35 ns.Start() 36 logger.Info( 37 "Embedded JetStream started", 38 zap.String("ClientURL", ns.ClientURL()), 39 ) 40 // Create KV Bucket 41 nc, _ := nats.Connect(nats.DefaultURL) 42 js, _ := nc.JetStream() 43 js.CreateKeyValue(&nats.KeyValueConfig{ 44 Bucket: BucketName, 45 }) 46 47 logger.Info( 48 "JetStream KV Bucket created", 49 zap.String("bucket_name", BucketName), 50 ) 51 return ns 52 }