Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ This command shows your active topics and their count.`,
req.Header.Set("Content-Type", "application/json")

// Execute the request
resp, err := http.DefaultClient.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("HTTP GET request failed: %v", err)
}
Expand Down Expand Up @@ -146,7 +146,7 @@ This command shows your active topics and their count.`,
req.Header.Set("Content-Type", "application/json")

// Execute the request
resp, err := http.DefaultClient.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("HTTP GET request failed: %v", err)
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/publish.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
Expand Down Expand Up @@ -206,22 +207,25 @@ var publishCmd = &cobra.Command{
}

url := baseURL + "/api/v1/publish"
req, err := http.NewRequest("POST", url, strings.NewReader(string(reqBytes)))
req, err := http.NewRequest("POST", url, bytes.NewReader(reqBytes))
if err != nil {
return err
return fmt.Errorf("failed to create publish request: %v", err)
}
// Only set Authorization header if auth is enabled
if !IsAuthDisabled() && token != nil {
req.Header.Set("Authorization", "Bearer "+token.Token)
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("HTTP publish failed: %v", err)
}
defer resp.Body.Close() //nolint:errcheck
body, _ := io.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read publish response: %v", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("publish error: %s", string(body))
}
Expand Down
11 changes: 7 additions & 4 deletions cmd/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,16 @@ var subscribeCmd = &cobra.Command{
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("HTTP POST subscribe failed: %v", err)
}

defer resp.Body.Close() //nolint:errcheck
body, _ := io.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read subscribe response: %v", err)
}

if resp.StatusCode != 200 {
return fmt.Errorf("HTTP POST subscribe error: %s", string(body))
Expand Down Expand Up @@ -291,7 +294,7 @@ var subscribeCmd = &cobra.Command{
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
fmt.Printf("Webhook request error: %v\n", err)
return
Expand Down Expand Up @@ -404,7 +407,7 @@ var subscribeCmd = &cobra.Command{
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
fmt.Printf("Webhook request error: %v\n", err)
return
Expand Down
7 changes: 7 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package cmd

import (
"net/http"
"regexp"
"time"
)

// httpClient is a shared HTTP client with a sensible timeout.
// Use this instead of http.DefaultClient to prevent indefinite hangs
// when the remote server is unreachable or slow to respond.
var httpClient = &http.Client{Timeout: 30 * time.Second}

// extractIPFromURL extracts IP address from URL string
func extractIPFromURL(url string) string {
ipRegex := regexp.MustCompile(`\d+\.\d+\.\d+\.\d+`)
Expand Down