@@ -44,10 +44,10 @@ type NotificationHelper struct {
44
44
45
45
// Config contains the configuration for the Notification Helper.
46
46
type Config struct {
47
- NatsAddress string `docs:";The NATS server address." mapstructure:"nats_address "`
48
- NatsToken string `docs:";The token to authenticate against the NATS server" mapstructure:"nats_token "`
49
- NatsStream string `docs:"reva-notifications;The notifications NATS stream." mapstructure:"nats_stream "`
50
- Templates map [string ]interface {} `docs:";Notification templates for the service." mapstructure:"templates "`
47
+ NatsAddress string `mapstructure:"nats_address" docs:";The NATS server address."`
48
+ NatsToken string `mapstructure:"nats_token" docs:";The token to authenticate against the NATS server"`
49
+ NatsStream string `mapstructure:"nats_stream" docs:"reva-notifications;The notifications NATS stream."`
50
+ Templates map [string ]interface {} `mapstructure:"templates" docs:";Notification templates for the service."`
51
51
}
52
52
53
53
func defaultConfig () * Config {
@@ -57,7 +57,7 @@ func defaultConfig() *Config {
57
57
}
58
58
59
59
// New creates a new Notification Helper.
60
- func New (name string , log * zerolog.Logger ) * NotificationHelper {
60
+ func New (name string , m map [ string ] interface {}, log * zerolog.Logger ) * NotificationHelper {
61
61
annotatedLogger := log .With ().Str ("service" , name ).Str ("scope" , "notifications" ).Logger ()
62
62
63
63
conf := defaultConfig ()
@@ -67,8 +67,18 @@ func New(name string, log *zerolog.Logger) *NotificationHelper {
67
67
Log : & annotatedLogger ,
68
68
}
69
69
70
+ if len (m ) == 0 {
71
+ log .Info ().Msgf ("no 'notifications' field in service config, notifications will be disabled" )
72
+ return nh
73
+ }
74
+
75
+ if err := mapstructure .Decode (m , conf ); err != nil {
76
+ log .Error ().Err (err ).Msgf ("decoding config failed, notifications will be disabled" )
77
+ return nh
78
+ }
79
+
70
80
if err := nh .connect (); err != nil {
71
- log .Error ().Err (err ).Msg ("connecting to nats failed, notifications will be disabled" )
81
+ log .Error ().Err (err ).Msgf ("connecting to nats failed, notifications will be disabled" )
72
82
return nh
73
83
}
74
84
@@ -120,7 +130,7 @@ func (nh *NotificationHelper) Stop() {
120
130
return
121
131
}
122
132
if err := nh .nc .Drain (); err != nil {
123
- nh .Log .Error ().Err (err ). Send ()
133
+ nh .Log .Error ().Err (err )
124
134
}
125
135
}
126
136
0 commit comments