From 6356ddd9f33cf4790b955739cea9f2822e9cf51b Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 8 Aug 2022 21:00:45 +0200 Subject: [PATCH] internal: replace ioutils Signed-off-by: Jens Langhammer --- internal/config/config.go | 5 ++--- internal/web/metrics.go | 9 ++------- internal/web/sentry_proxy.go | 10 ++++++---- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 7a49ceb15..c97be3633 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,7 +2,6 @@ package config import ( "fmt" - "io/ioutil" "net/url" "os" "reflect" @@ -62,7 +61,7 @@ func (c *Config) Setup(paths ...string) { } func (c *Config) LoadConfig(path string) error { - raw, err := ioutil.ReadFile(path) + raw, err := os.ReadFile(path) if err != nil { return fmt.Errorf("Failed to load config file: %w", err) } @@ -129,7 +128,7 @@ func (c *Config) parseScheme(rawVal string) string { } return u.RawQuery } else if u.Scheme == "file" { - d, err := ioutil.ReadFile(u.Path) + d, err := os.ReadFile(u.Path) if err != nil { return u.RawQuery } diff --git a/internal/web/metrics.go b/internal/web/metrics.go index e7e58cce5..942c9e6d6 100644 --- a/internal/web/metrics.go +++ b/internal/web/metrics.go @@ -1,7 +1,7 @@ package web import ( - "io/ioutil" + "io" "net/http" "github.com/gorilla/mux" @@ -43,12 +43,7 @@ func RunMetricsServer() { l.WithError(err).Warning("failed to get upstream metrics") return } - bm, err := ioutil.ReadAll(res.Body) - if err != nil { - l.WithError(err).Warning("failed to get upstream metrics") - return - } - _, err = rw.Write(bm) + _, err = io.Copy(rw, res.Body) if err != nil { l.WithError(err).Warning("failed to get upstream metrics") return diff --git a/internal/web/sentry_proxy.go b/internal/web/sentry_proxy.go index 328307608..8fb79dafb 100644 --- a/internal/web/sentry_proxy.go +++ b/internal/web/sentry_proxy.go @@ -1,8 +1,9 @@ package web import ( + "bytes" "encoding/json" - "io/ioutil" + "io" "net/http" "strings" @@ -19,13 +20,14 @@ func (ws *WebServer) APISentryProxy(rw http.ResponseWriter, r *http.Request) { rw.WriteHeader(http.StatusBadRequest) return } - fullBody, err := ioutil.ReadAll(r.Body) + fb := &bytes.Buffer{} + _, err := io.Copy(fb, r.Body) if err != nil { ws.log.Debug("failed to read body") rw.WriteHeader(http.StatusBadRequest) return } - lines := strings.Split(string(fullBody), "\n") + lines := strings.Split(string(fb.Bytes()), "\n") if len(lines) < 1 { rw.WriteHeader(http.StatusBadRequest) return @@ -42,7 +44,7 @@ func (ws *WebServer) APISentryProxy(rw http.ResponseWriter, r *http.Request) { rw.WriteHeader(http.StatusBadRequest) return } - res, err := http.DefaultClient.Post("https://sentry.beryju.org/api/8/envelope/", "application/octet-stream", strings.NewReader(string(fullBody))) + res, err := http.DefaultClient.Post("https://sentry.beryju.org/api/8/envelope/", "application/octet-stream", fb) if err != nil { ws.log.WithError(err).Warning("failed to proxy sentry") rw.WriteHeader(http.StatusBadRequest)