From 888159d2b684dc90b6c36a952c0bee3f4c2a067e Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Wed, 31 May 2023 14:41:57 +0300 Subject: [PATCH] Update API response mime type --- internal/api/api.go | 21 +++++++++++---------- internal/api/config.go | 3 ++- internal/debug/stack.go | 2 +- internal/hass/api.go | 2 +- internal/webtorrent/init.go | 2 +- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/internal/api/api.go b/internal/api/api.go index ff8df898..5117036b 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -2,6 +2,7 @@ package api import ( "encoding/json" + "fmt" "github.com/AlexxIT/go2rtc/internal/app" "github.com/rs/zerolog" "net" @@ -75,6 +76,11 @@ func Init() { }() } +const ( + MimeJSON = "application/json" + MimeText = "text/plain" +) + var Handler http.Handler // HandleFunc handle pattern with relative path: @@ -91,25 +97,20 @@ func HandleFunc(pattern string, handler http.HandlerFunc) { // ResponseJSON important always add Content-Type // so go won't need to call http.DetectContentType func ResponseJSON(w http.ResponseWriter, v any) { - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", MimeJSON) _ = json.NewEncoder(w).Encode(v) } func ResponsePrettyJSON(w http.ResponseWriter, v any) { - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", MimeJSON) enc := json.NewEncoder(w) enc.SetIndent("", " ") _ = enc.Encode(v) } -func ResponseRawJSON(w http.ResponseWriter, s string) { - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(s)) -} - -func ResponseText(w http.ResponseWriter, b []byte) { - w.Header().Set("Content-Type", "text/plain") - _, _ = w.Write(b) +func Response(w http.ResponseWriter, body any, contentType string) { + w.Header().Set("Content-Type", contentType) + _, _ = fmt.Fprint(w, body) } const StreamNotFound = "stream not found" diff --git a/internal/api/config.go b/internal/api/config.go index 6221d62b..a5548b05 100644 --- a/internal/api/config.go +++ b/internal/api/config.go @@ -21,7 +21,8 @@ func configHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, "", http.StatusNotFound) return } - ResponseText(w, data) + // https://www.ietf.org/archive/id/draft-ietf-httpapi-yaml-mediatypes-00.html + Response(w, data, "application/yaml") case "POST", "PATCH": data, err := io.ReadAll(r.Body) diff --git a/internal/debug/stack.go b/internal/debug/stack.go index b88d9540..d2508d9f 100644 --- a/internal/debug/stack.go +++ b/internal/debug/stack.go @@ -52,5 +52,5 @@ func stackHandler(w http.ResponseWriter, r *http.Request) { "Total: %d, Skipped: %d", runtime.NumGoroutine(), skipped), ) - api.ResponseText(w, buf[:i]) + api.Response(w, buf[:i], api.MimeText) } diff --git a/internal/hass/api.go b/internal/hass/api.go index d12d2fae..eb50781a 100644 --- a/internal/hass/api.go +++ b/internal/hass/api.go @@ -12,7 +12,7 @@ import ( ) func apiOK(w http.ResponseWriter, r *http.Request) { - api.ResponseRawJSON(w, `{"status":1,"payload":{}}`) + api.Response(w, `{"status":1,"payload":{}}`, api.MimeJSON) } func apiStream(w http.ResponseWriter, r *http.Request) { diff --git a/internal/webtorrent/init.go b/internal/webtorrent/init.go index b07ccdaf..083bf50b 100644 --- a/internal/webtorrent/init.go +++ b/internal/webtorrent/init.go @@ -141,7 +141,7 @@ func apiHandle(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) data := fmt.Sprintf(`{"share":%q,"pwd":%q}`, share, pwd) - api.ResponseRawJSON(w, data) + api.Response(w, data, api.MimeJSON) case "DELETE": if ok {