Browse Source

api: print all requests and responses when logLevel is debug

pull/509/head
aler9 4 years ago committed by Alessandro Ros
parent
commit
5943660ab3
  1. 60
      internal/core/api.go

60
internal/core/api.go

@ -1,10 +1,13 @@
package core package core
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"net" "net"
"net/http" "net/http"
"net/http/httputil"
"reflect" "reflect"
"sync" "sync"
"time" "time"
@ -258,17 +261,17 @@ func newAPI(
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
router := gin.New() router := gin.New()
group := router.Group("/", a.mwLog)
router.GET("/config/get", a.onConfigGet) group.GET("/config/get", a.onConfigGet)
router.POST("/config/set", a.onConfigSet) group.POST("/config/set", a.onConfigSet)
router.POST("/config/paths/add/:name", a.onConfigPathsAdd) group.POST("/config/paths/add/:name", a.onConfigPathsAdd)
router.POST("/config/paths/edit/:name", a.onConfigPathsEdit) group.POST("/config/paths/edit/:name", a.onConfigPathsEdit)
router.POST("/config/paths/delete/:name", a.onConfigPathsDelete) group.POST("/config/paths/delete/:name", a.onConfigPathsDelete)
router.GET("/paths/list", a.onPathsList) group.GET("/paths/list", a.onPathsList)
router.GET("/rtspsessions/list", a.onRTSPSessionsList) group.GET("/rtspsessions/list", a.onRTSPSessionsList)
router.POST("/rtspsessions/kick/:id", a.onRTSPSessionsKick) group.POST("/rtspsessions/kick/:id", a.onRTSPSessionsKick)
router.GET("/rtmpconns/list", a.onRTMPConnsList) group.GET("/rtmpconns/list", a.onRTMPConnsList)
router.POST("/rtmpconns/kick/:id", a.onRTMPConnsKick) group.POST("/rtmpconns/kick/:id", a.onRTMPConnsKick)
a.s = &http.Server{ a.s = &http.Server{
Handler: router, Handler: router,
@ -291,6 +294,41 @@ func (a *api) log(level logger.Level, format string, args ...interface{}) {
a.parent.Log(level, "[API] "+format, args...) a.parent.Log(level, "[API] "+format, args...)
} }
type logWriter struct {
gin.ResponseWriter
buf bytes.Buffer
}
func (w *logWriter) Write(b []byte) (int, error) {
w.buf.Write(b)
return w.ResponseWriter.Write(b)
}
func (w *logWriter) WriteString(s string) (int, error) {
w.buf.WriteString(s)
return w.ResponseWriter.WriteString(s)
}
func (a *api) mwLog(ctx *gin.Context) {
byts, _ := httputil.DumpRequest(ctx.Request, true)
a.log(logger.Debug, "[c->s] %s", string(byts))
blw := &logWriter{ResponseWriter: ctx.Writer}
ctx.Writer = blw
ctx.Next()
ctx.Writer.Header().Set("Server", "rtsp-simple-server")
var buf bytes.Buffer
fmt.Fprintf(&buf, "HTTP/1.1 %d %s\n", ctx.Writer.Status(), http.StatusText(ctx.Writer.Status()))
ctx.Writer.Header().Write(&buf)
buf.Write([]byte("\n"))
buf.Write(blw.buf.Bytes())
a.log(logger.Debug, "[s->c] %s", buf.String())
}
func (a *api) onConfigGet(ctx *gin.Context) { func (a *api) onConfigGet(ctx *gin.Context) {
a.mutex.Lock() a.mutex.Lock()
c := a.conf c := a.conf

Loading…
Cancel
Save