natsio/stream.go

29 lines
556 B
Go
Raw Normal View History

2021-10-08 04:23:59 +00:00
package natsio
import (
"errors"
"fmt"
"github.com/nats-io/nats.go"
)
// AddStream add new stream, with default config
func AddStream(name string, subjects []string) error {
// Get info about the stream
2021-10-08 04:33:32 +00:00
stream, _ := natsJS.StreamInfo(name)
2021-10-08 04:23:59 +00:00
// If stream not found, create new
if stream == nil {
2021-10-08 04:33:32 +00:00
_, err := natsJS.AddStream(&nats.StreamConfig{
2021-10-08 04:23:59 +00:00
Name: name,
Subjects: subjects,
Storage: nats.FileStorage,
})
if err != nil {
msg := fmt.Sprintf("add stream error: %s", err.Error())
return errors.New(msg)
}
}
return nil
}