Skip to content

Commit 9eac1a8

Browse files
committed
improve response handling and Redis client initialization
1 parent ce458e9 commit 9eac1a8

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

‎webhooks.go‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/sirupsen/logrus"
2626

2727
"github.com/jerry-enebeli/blnk/config"
28+
redis_db "github.com/jerry-enebeli/blnk/internal/redis-db"
2829

2930
"github.com/hibiken/asynq"
3031
)
@@ -112,13 +113,20 @@ func processHTTP(data NewWebhook) error {
112113
return nil
113114
}
114115

115-
var response map[string]interface{}
116-
err = json.NewDecoder(resp.Body).Decode(&response)
116+
// Read the response body
117+
body, err := io.ReadAll(resp.Body)
117118
if err != nil {
118-
log.Println("Error decoding response:", err)
119+
log.Println("Error reading response body:", err)
119120
return err
120121
}
121122

123+
// Try to decode JSON response, but don't fail if it's not JSON
124+
var response map[string]interface{}
125+
if err := json.Unmarshal(body, &response); err != nil {
126+
log.Printf("Response is not JSON (this is OK): %s\n", string(body))
127+
return nil
128+
}
129+
122130
log.Println("Webhook notification sent successfully:", response)
123131
return nil
124132
}
@@ -140,7 +148,13 @@ func SendWebhook(newWebhook NewWebhook) error {
140148
return nil
141149
}
142150

143-
client := asynq.NewClient(asynq.RedisClientOpt{Addr: conf.Redis.Dns})
151+
redisOption, err := redis_db.ParseRedisURL(conf.Redis.Dns)
152+
if err != nil {
153+
log.Fatalf("Error parsing Redis URL: %v", err)
154+
}
155+
queueOptions := asynq.RedisClientOpt{Addr: redisOption.Addr, Password: redisOption.Password, DB: redisOption.DB}
156+
client := asynq.NewClient(queueOptions)
157+
144158
payload, err := json.Marshal(newWebhook)
145159
if err != nil {
146160
log.Fatal(err)
@@ -178,7 +192,6 @@ func ProcessWebhook(_ context.Context, task *asynq.Task) error {
178192
log.Printf("Error unmarshaling task payload: %v", err)
179193
return err
180194
}
181-
log.Printf("Processing webhook: %+v\n", payload.Event)
182195
err = processHTTP(payload)
183196
if err != nil {
184197
return err

0 commit comments

Comments
 (0)