Code refactoring for #1744

This commit is contained in:
Alex X
2025-10-07 13:25:42 +03:00
parent 670370056c
commit fe2cc4b525
20 changed files with 269 additions and 362 deletions
+3 -15
View File
@@ -1,7 +1,6 @@
package api
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
@@ -15,7 +14,6 @@ import (
"time"
"github.com/AlexxIT/go2rtc/internal/app"
"github.com/AlexxIT/go2rtc/pkg/shell"
"github.com/rs/zerolog"
)
@@ -167,19 +165,9 @@ func ResponseJSON(w http.ResponseWriter, v any) {
func ResponsePrettyJSON(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", MimeJSON)
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
err := enc.Encode(v)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
redactedJSON := shell.Redact(buf.String())
w.Write([]byte(redactedJSON))
_ = enc.Encode(v)
}
func Response(w http.ResponseWriter, body any, contentType string) {
@@ -202,7 +190,7 @@ var log zerolog.Logger
func middlewareLog(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Trace().Msgf("[api] %s %s %s", r.Method, shell.Redact(r.URL.String()), r.RemoteAddr)
log.Trace().Msgf("[api] %s %s %s", r.Method, r.URL, r.RemoteAddr)
next.ServeHTTP(w, r)
})
}