Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit 7ba00ff

Browse files
committed
Response forwarding
1 parent 88ea713 commit 7ba00ff

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Diff for: main.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package main
1616

1717
import (
18+
"bytes"
1819
"fmt"
1920
"io/ioutil"
2021
"log"
@@ -148,12 +149,18 @@ func (s *Specification) newTask(w http.ResponseWriter, r *http.Request) {
148149
case result := <-resultsChannel:
149150
log.Printf("-> %s %d %s\n", result.id, result.statusCode, result.data)
150151
if s.Sink != "" {
151-
ww := w
152-
ww.WriteHeader(result.statusCode)
153-
ww.Header().Set("HOST", s.Sink)
154-
ww.Write(result.data)
155-
156-
result.data = []byte("ok")
152+
resp, err := http.Post(s.Sink, "application/json", bytes.NewReader(result.data))
153+
if err != nil {
154+
msg := fmt.Sprintf("sink %q is not accepting the request: %s", s.Sink, err)
155+
log.Printf("-> ! %s %s\n", result.id, msg)
156+
result.data = []byte(msg)
157+
result.statusCode = http.StatusBadGateway
158+
} else if resp.StatusCode < 200 || resp.StatusCode > 299 {
159+
msg := fmt.Sprintf("sink %q responding with the error: %s", s.Sink, resp.Status)
160+
log.Printf("-> ! %s %s\n", result.id, msg)
161+
result.data = []byte(msg)
162+
result.statusCode = resp.StatusCode
163+
}
157164
}
158165
w.WriteHeader(result.statusCode)
159166
w.Write(result.data)

0 commit comments

Comments
 (0)