Skip to content

Commit

Permalink
refactor sendReport in func
Browse files Browse the repository at this point in the history
  • Loading branch information
amircybersec committed Oct 13, 2023
1 parent c876e79 commit 777a241
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions x/examples/outline-connectivity/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,37 @@ func unwrapAll(err error) error {
}
}

func sendReport(record jsonRecord, collectorFlag *string) error {
jsonData, err := json.Marshal(record)
if err != nil {
log.Fatalf("Error encoding JSON: %s\n", err)
return err
}

req, err := http.NewRequest("POST", *collectorFlag, bytes.NewBuffer(jsonData))
if err != nil {
debugLog.Printf("Error creating the HTTP request: %s\n", err)
return err
}

req.Header.Set("Content-Type", "application/json; charset=utf-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Error sending the HTTP request: %s\n", err)
return err
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
debugLog.Printf("Error reading the HTTP response body: %s\n", err)
return err
}
debugLog.Printf("Response: %s\n", respBody)
return nil
}

func main() {
verboseFlag := flag.Bool("v", false, "Enable debug output")
transportFlag := flag.String("transport", "", "Transport config")
Expand Down Expand Up @@ -155,34 +186,10 @@ func main() {
}
// Send error report to collector if specified
if !success && *collectorFlag != "" {
jsonData, err := json.Marshal(record)
err = sendReport(record, collectorFlag)
if err != nil {
log.Fatalf("Error encoding JSON: %s\n", err)
return
log.Fatalf("HTTP request failed: %v", err)
}

req, err := http.NewRequest("POST", *collectorFlag, bytes.NewReader(jsonData))
if err != nil {
debugLog.Printf("Error creating the HTTP request: %s\n", err)
return
}

req.Header.Set("Content-Type", "application/json; charset=utf-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Error sending the HTTP request: %s\n", err)
return
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
debugLog.Printf("Error reading the HTTP response body: %s\n", err)
return
}

debugLog.Printf("Response: %s\n", respBody)
}
}
}
Expand Down

0 comments on commit 777a241

Please sign in to comment.