From 9645d18ae430e6e8f29f0ca58c855ea3b6655e54 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sat, 6 Nov 2021 12:53:49 +0100 Subject: [PATCH] move httpLogWriter in a dedicated file --- internal/core/api.go | 28 --------------------------- internal/core/httplogwriter.go | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 28 deletions(-) create mode 100644 internal/core/httplogwriter.go diff --git a/internal/core/api.go b/internal/core/api.go index 2345f459f5c..f43cd999b1d 100644 --- a/internal/core/api.go +++ b/internal/core/api.go @@ -1,10 +1,8 @@ package core import ( - "bytes" "context" "encoding/json" - "fmt" "net" "net/http" "net/http/httputil" @@ -17,32 +15,6 @@ import ( "github.com/aler9/rtsp-simple-server/internal/logger" ) -type httpLogWriter struct { - gin.ResponseWriter - buf bytes.Buffer -} - -func (w *httpLogWriter) Write(b []byte) (int, error) { - w.buf.Write(b) - return w.ResponseWriter.Write(b) -} - -func (w *httpLogWriter) WriteString(s string) (int, error) { - w.buf.WriteString(s) - return w.ResponseWriter.WriteString(s) -} - -func (w *httpLogWriter) dump() string { - var buf bytes.Buffer - fmt.Fprintf(&buf, "%s %d %s\n", "HTTP/1.1", w.ResponseWriter.Status(), http.StatusText(w.ResponseWriter.Status())) - w.ResponseWriter.Header().Write(&buf) - buf.Write([]byte("\n")) - if w.buf.Len() > 0 { - fmt.Fprintf(&buf, "(body of %d bytes)", w.buf.Len()) - } - return buf.String() -} - func interfaceIsEmpty(i interface{}) bool { return reflect.ValueOf(i).Kind() != reflect.Ptr || reflect.ValueOf(i).IsNil() } diff --git a/internal/core/httplogwriter.go b/internal/core/httplogwriter.go new file mode 100644 index 00000000000..44b064d6581 --- /dev/null +++ b/internal/core/httplogwriter.go @@ -0,0 +1,35 @@ +package core + +import ( + "bytes" + "fmt" + "net/http" + + "github.com/gin-gonic/gin" +) + +type httpLogWriter struct { + gin.ResponseWriter + buf bytes.Buffer +} + +func (w *httpLogWriter) Write(b []byte) (int, error) { + w.buf.Write(b) + return w.ResponseWriter.Write(b) +} + +func (w *httpLogWriter) WriteString(s string) (int, error) { + w.buf.WriteString(s) + return w.ResponseWriter.WriteString(s) +} + +func (w *httpLogWriter) dump() string { + var buf bytes.Buffer + fmt.Fprintf(&buf, "%s %d %s\n", "HTTP/1.1", w.ResponseWriter.Status(), http.StatusText(w.ResponseWriter.Status())) + w.ResponseWriter.Header().Write(&buf) + buf.Write([]byte("\n")) + if w.buf.Len() > 0 { + fmt.Fprintf(&buf, "(body of %d bytes)", w.buf.Len()) + } + return buf.String() +}