Update API response mime type
This commit is contained in:
+11
-10
@@ -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"
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user