Update API response mime type
This commit is contained in:
+11
-10
@@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"github.com/AlexxIT/go2rtc/internal/app"
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"net"
|
"net"
|
||||||
@@ -75,6 +76,11 @@ func Init() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
MimeJSON = "application/json"
|
||||||
|
MimeText = "text/plain"
|
||||||
|
)
|
||||||
|
|
||||||
var Handler http.Handler
|
var Handler http.Handler
|
||||||
|
|
||||||
// HandleFunc handle pattern with relative path:
|
// HandleFunc handle pattern with relative path:
|
||||||
@@ -91,25 +97,20 @@ func HandleFunc(pattern string, handler http.HandlerFunc) {
|
|||||||
// ResponseJSON important always add Content-Type
|
// ResponseJSON important always add Content-Type
|
||||||
// so go won't need to call http.DetectContentType
|
// so go won't need to call http.DetectContentType
|
||||||
func ResponseJSON(w http.ResponseWriter, v any) {
|
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)
|
_ = json.NewEncoder(w).Encode(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResponsePrettyJSON(w http.ResponseWriter, v any) {
|
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 := json.NewEncoder(w)
|
||||||
enc.SetIndent("", " ")
|
enc.SetIndent("", " ")
|
||||||
_ = enc.Encode(v)
|
_ = enc.Encode(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResponseRawJSON(w http.ResponseWriter, s string) {
|
func Response(w http.ResponseWriter, body any, contentType string) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", contentType)
|
||||||
_, _ = w.Write([]byte(s))
|
_, _ = fmt.Fprint(w, body)
|
||||||
}
|
|
||||||
|
|
||||||
func ResponseText(w http.ResponseWriter, b []byte) {
|
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
|
||||||
_, _ = w.Write(b)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const StreamNotFound = "stream not found"
|
const StreamNotFound = "stream not found"
|
||||||
|
|||||||
@@ -21,7 +21,8 @@ func configHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "", http.StatusNotFound)
|
http.Error(w, "", http.StatusNotFound)
|
||||||
return
|
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":
|
case "POST", "PATCH":
|
||||||
data, err := io.ReadAll(r.Body)
|
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),
|
"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) {
|
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) {
|
func apiStream(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ func apiHandle(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
data := fmt.Sprintf(`{"share":%q,"pwd":%q}`, share, pwd)
|
data := fmt.Sprintf(`{"share":%q,"pwd":%q}`, share, pwd)
|
||||||
api.ResponseRawJSON(w, data)
|
api.Response(w, data, api.MimeJSON)
|
||||||
|
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
if ok {
|
if ok {
|
||||||
|
|||||||
Reference in New Issue
Block a user