🔧 Update Go module dependencies and modify NATS configuration to exclusively support JetStream mode. Upgrade go.uber.org/zap to version 1.27.1 and golang.org/x/crypto to version 0.45.0. Revise Makefile and documentation to reflect changes in NATS mode, ensuring consistent messaging behavior across development and production environments. Enhance README and configuration files to clarify JetStream usage and remove references to Core NATS mode.

This commit is contained in:
windyboy
2025-11-21 09:59:47 +08:00
parent ee7cac95f0
commit 6eff35b56f
16 changed files with 91 additions and 382 deletions
+2 -60
View File
@@ -20,8 +20,7 @@ type Publisher struct {
logger *zap.Logger
}
// ProvidePublisher creates a NATS publisher.
// When js is nil (core mode), returns a CorePublisher that uses plain NATS.
// ProvidePublisher creates a NATS JetStream publisher.
func ProvidePublisher(
js nats.JetStreamContext,
nc *nats.Conn,
@@ -29,7 +28,7 @@ func ProvidePublisher(
logger *zap.Logger,
) (port.Publisher, error) {
if js == nil {
return ProvideCorePublisher(nc, cfg, logger)
return nil, fmt.Errorf("JetStream context is required")
}
return &Publisher{
js: js,
@@ -38,63 +37,6 @@ func ProvidePublisher(
}, nil
}
// CorePublisher publishes messages to plain NATS (non-JetStream)
type CorePublisher struct {
conn *nats.Conn
cfg *config.Config
logger *zap.Logger
}
// ProvideCorePublisher creates a NATS publisher for core mode
func ProvideCorePublisher(
conn *nats.Conn,
cfg *config.Config,
logger *zap.Logger,
) (port.Publisher, error) {
return &CorePublisher{
conn: conn,
cfg: cfg,
logger: logger,
}, nil
}
// Publish publishes a message using plain NATS
func (p *CorePublisher) Publish(message any) error {
topic := p.cfg.Publisher.Topic
if topic == "" {
p.logger.Error("publisher topic is not configured")
return fmt.Errorf("publisher topic is not configured")
}
// Marshal message to JSON
messageBytes, err := json.Marshal(message)
if err != nil {
p.logger.Error("failed to marshal message",
zap.String("topic", topic),
zap.Error(err),
)
return fmt.Errorf("failed to marshal message: %w", err)
}
// Publish to plain NATS
err = p.conn.Publish(topic, messageBytes)
if err != nil {
p.logger.Error("failed to publish message",
zap.String("topic", topic),
zap.Int("message_size", len(messageBytes)),
zap.Error(err),
)
return fmt.Errorf("failed to publish message: %w", err)
}
p.logger.Debug("Published message",
zap.String("topic", topic),
zap.Int("size", len(messageBytes)),
)
return nil
}
// Publish publishes a message
func (p *Publisher) Publish(message any) error {
topic := p.cfg.Publisher.Topic