@@ -22,6 +22,7 @@ import (
2222 "github.com/labstack/echo-contrib/prometheus"
2323 "github.com/labstack/echo/v4"
2424 "github.com/labstack/echo/v4/middleware"
25+ "github.com/modfin/henry/slicez"
2526 "github.com/modfin/mmailer"
2627 "github.com/modfin/mmailer/internal/config"
2728 "github.com/modfin/mmailer/internal/logger"
@@ -202,6 +203,23 @@ func loadServices() {
202203 retry = mmailer .RetryNone
203204 }
204205
206+ domainApiKeys := make (map [string ][]mmailer.ServiceApiKey )
207+
208+ for _ , k := range config .Get ().ServiceDomainApiKeys {
209+ parts := strings .Split (k , ":" )
210+ if len (parts ) != 3 {
211+ logger .Warn (fmt .Sprintf ("couldn't parse row of SERVICE_DOMAIN_API_KEYS, '%s'" , k ))
212+ }
213+ key := mmailer.ServiceApiKey {
214+ Service : strings .ToLower (parts [0 ]),
215+ ApiKey : mmailer.ApiKey {
216+ Domain : strings .ToLower (parts [1 ]),
217+ Key : parts [2 ],
218+ },
219+ }
220+ domainApiKeys [key .Service ] = append (domainApiKeys [key .Service ], key )
221+ }
222+
205223 var services []mmailer.Service
206224 logger .Info ("Services:" )
207225 var weighted = strategyName == "weighted"
@@ -235,7 +253,8 @@ func loadServices() {
235253
236254 posthookUrl := fmt .Sprintf ("%s/posthook?key=%s&service=%s" , config .Get ().PublicURL , config .Get ().PosthookKey , strings .ToLower (parts [0 ]))
237255
238- switch strings .ToLower (parts [0 ]) {
256+ service := strings .ToLower (parts [0 ])
257+ switch service {
239258 case "mailjet" :
240259 if len (parts ) != 3 {
241260 logger .Warn (fmt .Sprintf ("mailjet api string is not valid, %s" , s ))
@@ -252,12 +271,30 @@ func loadServices() {
252271 logger .Info (fmt .Sprintf (" - Mandrill: add the following posthook url %s" , posthookUrl ))
253272 services = append (services , decorate (mandrill .New (parts [1 ])))
254273 case "sendgrid" :
255- if len (parts ) != 2 {
274+ if len (parts ) < 1 || len ( parts ) > 2 {
256275 logger .Warn ("sendgrid api string is not valid," , s )
257276 continue
258277 }
278+ apiKeys := slicez .Map (domainApiKeys [service ], func (k mmailer.ServiceApiKey ) mmailer.ApiKey {
279+ return k .ApiKey
280+ })
281+ if len (parts ) == 2 {
282+ apiKeys = append (apiKeys , mmailer.ApiKey {
283+ Domain : mmailer .ApiKeyAnyDomain ,
284+ Key : parts [1 ],
285+ })
286+ logger .Info (" - Sendgrid: key enabled: AnyDomain" )
287+ }
288+ for _ , k := range domainApiKeys [service ] {
289+ logger .Info (fmt .Sprintf (" - Sendgrid: key enabled: %s" , k .Domain ))
290+ }
291+ if len (apiKeys ) == 0 {
292+ logger .Warn (" - Sendgrid: disabled, no api keys provided" )
293+ continue
294+ }
295+
259296 logger .Info (fmt .Sprintf (" - Sendgrid: add the following posthook url %s" , posthookUrl ))
260- services = append (services , decorate (sendgrid .New (parts [ 1 ] )))
297+ services = append (services , decorate (sendgrid .New (apiKeys )))
261298 case "brev" :
262299 brev , err := brev .New (parts [1 :], posthookUrl )
263300 if err != nil {
0 commit comments