mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Merge pull request #14666 from bergquist/close_body
Closes the body properly on successful webhooks
This commit is contained in:
7
devenv/docker/blocks/alert_webhook_listener/Dockerfile
Normal file
7
devenv/docker/blocks/alert_webhook_listener/Dockerfile
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
FROM golang:latest
|
||||||
|
ADD main.go /
|
||||||
|
WORKDIR /
|
||||||
|
RUN go build -o main .
|
||||||
|
EXPOSE 3010
|
||||||
|
ENTRYPOINT ["/main"]
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
alert_webhook_listener:
|
||||||
|
build: docker/blocks/alert_webhook_listener
|
||||||
|
network_mode: host
|
||||||
|
ports:
|
||||||
|
- "3010:3010"
|
||||||
24
devenv/docker/blocks/alert_webhook_listener/main.go
Normal file
24
devenv/docker/blocks/alert_webhook_listener/main.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func hello(w http.ResponseWriter, r *http.Request) {
|
||||||
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
line := fmt.Sprintf("webbhook: -> %s", string(body))
|
||||||
|
fmt.Println(line)
|
||||||
|
io.WriteString(w, line)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", hello)
|
||||||
|
http.ListenAndServe(":3010", nil)
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -69,11 +70,14 @@ func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook *
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode/100 == 2 {
|
if resp.StatusCode/100 == 2 {
|
||||||
|
// flushing the body enables the transport to reuse the same connection
|
||||||
|
io.Copy(ioutil.Discard, resp.Body)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user