From 9b3f33443c3533f168ebacd98a1119ac6999eb75 Mon Sep 17 00:00:00 2001 From: Christoph Kluge Date: Tue, 13 Aug 2024 18:22:06 +0200 Subject: [PATCH] add get function without argument --- cmd/cc-backend/main.go | 2 +- internal/natsMessenger/natsMessenger.go | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/cc-backend/main.go b/cmd/cc-backend/main.go index 29a4467..64fdccb 100644 --- a/cmd/cc-backend/main.go +++ b/cmd/cc-backend/main.go @@ -216,7 +216,7 @@ func main() { // Start NATS Messenger if Config exists wg.Add(1) - nm := natsMessenger.GetNatsMessenger(config.Keys.Nats) + nm := natsMessenger.StartNatsMessenger(config.Keys.Nats) wg.Done() // Start HTTP server diff --git a/internal/natsMessenger/natsMessenger.go b/internal/natsMessenger/natsMessenger.go index e535f2e..a69d689 100644 --- a/internal/natsMessenger/natsMessenger.go +++ b/internal/natsMessenger/natsMessenger.go @@ -72,8 +72,8 @@ type ReceiveEventNatsRequest struct { Value *int64 `json:"value,omitempty" example:"150"` // Optional Value Set for Evenr, eg powercap } -// Get Singleton -func GetNatsMessenger(config *schema.NatsConfig) *NatsMessenger { +// Start Singleton +func StartNatsMessenger(config *schema.NatsConfig) *NatsMessenger { // Check if Config present if config == nil { log.Info("No NATS config found: Skip NATS init.") @@ -132,8 +132,15 @@ func GetNatsMessenger(config *schema.NatsConfig) *NatsMessenger { } }) log.Infof("NATS server and subscriptions on port '%d' established\n", config.Port) - } else { - log.Infof("Single NatsMessenger instance already created on port '%d'\n", config.Port) + } + + return natsMessengerInstance +} + +// Get Singleton +func GetNatsMessenger() *NatsMessenger { + if natsMessengerInstance == nil { + log.Fatalf("NatsMessengerInstance not initialized!") } return natsMessengerInstance