Skip to content

Commit

Permalink
move httpLogWriter in a dedicated file
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Nov 6, 2021
1 parent 21a149c commit 9645d18
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
28 changes: 0 additions & 28 deletions internal/core/api.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package core

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httputil"
Expand All @@ -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()
}
Expand Down
35 changes: 35 additions & 0 deletions internal/core/httplogwriter.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 9645d18

Please sign in to comment.