Initial QSfera import
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package nats
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
nserver "github.com/nats-io/nats-server/v2/server"
|
||||
)
|
||||
|
||||
var NATSListenAndServeLoopTimer = 1 * time.Second
|
||||
|
||||
type NATSServer struct {
|
||||
server *nserver.Server
|
||||
}
|
||||
|
||||
// NatsOption configures the new NATSServer instance
|
||||
func NewNATSServer(logger nserver.Logger, opts ...NatsOption) (*NATSServer, error) {
|
||||
natsOpts := &nserver.Options{}
|
||||
|
||||
for _, o := range opts {
|
||||
o(natsOpts)
|
||||
}
|
||||
|
||||
// enable JetStream
|
||||
natsOpts.JetStream = true
|
||||
// The NATS server itself runs the signal handling. We set `natsOpts.NoSigs = true` because we want to handle signals ourselves
|
||||
natsOpts.NoSigs = true
|
||||
|
||||
server, err := nserver.NewServer(natsOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
server.SetLoggerV2(logger, true, true, false)
|
||||
|
||||
return &NATSServer{
|
||||
server: server,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListenAndServe runs the NATSServer in a blocking way until the server is shutdown or an error occurs
|
||||
func (n *NATSServer) ListenAndServe() (err error) {
|
||||
n.server.Start() // it won't block
|
||||
n.server.WaitForShutdown() // block until the server is fully shutdown
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shutdown stops the NATSServer gracefully
|
||||
func (n *NATSServer) Shutdown() {
|
||||
n.server.Shutdown()
|
||||
n.server.WaitForShutdown()
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package nats
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
|
||||
nserver "github.com/nats-io/nats-server/v2/server"
|
||||
)
|
||||
|
||||
// NatsOption configures the nats server
|
||||
type NatsOption func(*nserver.Options)
|
||||
|
||||
// Host sets the host URL for the nats server
|
||||
func Host(url string) NatsOption {
|
||||
return func(o *nserver.Options) {
|
||||
o.Host = url
|
||||
}
|
||||
}
|
||||
|
||||
// Port sets the host URL for the nats server
|
||||
func Port(port int) NatsOption {
|
||||
return func(o *nserver.Options) {
|
||||
o.Port = port
|
||||
}
|
||||
}
|
||||
|
||||
// ClusterID sets the name for the nats cluster
|
||||
func ClusterID(clusterID string) NatsOption {
|
||||
return func(o *nserver.Options) {
|
||||
o.Cluster.Name = clusterID
|
||||
}
|
||||
}
|
||||
|
||||
// StoreDir sets the folder for persistence
|
||||
func StoreDir(StoreDir string) NatsOption {
|
||||
return func(o *nserver.Options) {
|
||||
o.StoreDir = StoreDir
|
||||
}
|
||||
}
|
||||
|
||||
// TLSConfig sets the tls config for the nats server
|
||||
func TLSConfig(c *tls.Config) NatsOption {
|
||||
return func(o *nserver.Options) {
|
||||
o.TLSConfig = c
|
||||
}
|
||||
}
|
||||
|
||||
// AllowNonTLS sets the allow non tls options for the nats server
|
||||
func AllowNonTLS(v bool) NatsOption {
|
||||
return func(o *nserver.Options) {
|
||||
o.AllowNonTLS = v
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user