added nats + worker modules

This commit is contained in:
2025-12-19 09:59:00 -05:00
parent b457c74d49
commit ca758cc708
12 changed files with 559 additions and 31 deletions

74
nats/options.go Normal file
View File

@@ -0,0 +1,74 @@
package nats
import (
"strings"
"time"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
)
type Option func(*config)
type config struct {
serverOpts *server.Options
connectOpts []nats.Option
externalURL string
readyTimeout time.Duration
shutdownTimeout time.Duration
}
func WithExternalURL(url string) Option {
return func(c *config) { c.externalURL = url }
}
func WithServerOpts(opts *server.Options) Option {
return func(c *config) { c.serverOpts = opts }
}
func WithConnectOpts(opts ...nats.Option) Option {
return func(c *config) { c.connectOpts = append(c.connectOpts, opts...) }
}
func WithReadyTimeout(d time.Duration) Option {
return func(c *config) { c.readyTimeout = d }
}
func WithCluster(name string, routes []string) Option {
return func(c *config) {
if c.serverOpts == nil {
c.serverOpts = &server.Options{}
}
c.serverOpts.Cluster = server.ClusterOpts{
Name: name,
}
c.serverOpts.Routes = server.RoutesFromStr(strings.Join(routes, ","))
}
}
func WithClusterListen() Option {
return func(c *config) {
if c.serverOpts == nil {
c.serverOpts = &server.Options{}
}
c.serverOpts.Cluster.ListenStr = "0.0.0.0:6222"
}
}
func WithJetStream(enabled bool, storeDir ...string) Option {
return func(c *config) {
if c.serverOpts == nil {
c.serverOpts = &server.Options{}
}
c.serverOpts.JetStream = enabled
if len(storeDir) > 0 {
c.serverOpts.StoreDir = storeDir[0]
}
}
}
func WithClientName(name string) Option {
return func(c *config) {
c.connectOpts = append(c.connectOpts, nats.Name(name))
}
}