Add content-type to API responses
This commit is contained in:
+29
-8
@@ -88,6 +88,30 @@ func HandleFunc(pattern string, handler http.HandlerFunc) {
|
|||||||
http.HandleFunc(pattern, handler)
|
http.HandleFunc(pattern, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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")
|
||||||
|
_ = json.NewEncoder(w).Encode(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ResponsePrettyJSON(w http.ResponseWriter, v any) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
const StreamNotFound = "stream not found"
|
const StreamNotFound = "stream not found"
|
||||||
|
|
||||||
var basePath string
|
var basePath string
|
||||||
@@ -131,9 +155,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
app.Info["host"] = r.Host
|
app.Info["host"] = r.Host
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(app.Info); err != nil {
|
ResponseJSON(w, app.Info)
|
||||||
log.Warn().Err(err).Caller().Send()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func exitHandler(w http.ResponseWriter, r *http.Request) {
|
func exitHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -158,11 +180,10 @@ func ResponseStreams(w http.ResponseWriter, streams []Stream) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var response struct {
|
var response = struct {
|
||||||
Streams []Stream `json:"streams"`
|
Streams []Stream `json:"streams"`
|
||||||
|
}{
|
||||||
|
Streams: streams,
|
||||||
}
|
}
|
||||||
response.Streams = streams
|
ResponseJSON(w, response)
|
||||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,9 +21,7 @@ func configHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "", http.StatusNotFound)
|
http.Error(w, "", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, err = w.Write(data); err != nil {
|
ResponseText(w, data)
|
||||||
log.Warn().Err(err).Caller().Send()
|
|
||||||
}
|
|
||||||
|
|
||||||
case "POST", "PATCH":
|
case "POST", "PATCH":
|
||||||
data, err := io.ReadAll(r.Body)
|
data, err := io.ReadAll(r.Body)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package debug
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
@@ -51,7 +52,5 @@ func stackHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
"Total: %d, Skipped: %d", runtime.NumGoroutine(), skipped),
|
"Total: %d, Skipped: %d", runtime.NumGoroutine(), skipped),
|
||||||
)
|
)
|
||||||
|
|
||||||
if _, err := w.Write(buf[:i]); err != nil {
|
api.ResponseText(w, buf[:i])
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package hass
|
|||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
"github.com/AlexxIT/go2rtc/internal/streams"
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
"github.com/AlexxIT/go2rtc/internal/webrtc"
|
"github.com/AlexxIT/go2rtc/internal/webrtc"
|
||||||
"net"
|
"net"
|
||||||
@@ -11,8 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func apiOK(w http.ResponseWriter, r *http.Request) {
|
func apiOK(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
api.ResponseRawJSON(w, `{"status":1,"payload":{}}`)
|
||||||
_, _ = w.Write([]byte(`{"status":1,"payload":{}}`))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiStream(w http.ResponseWriter, r *http.Request) {
|
func apiStream(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package homekit
|
package homekit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
"github.com/AlexxIT/go2rtc/internal/app/store"
|
"github.com/AlexxIT/go2rtc/internal/app/store"
|
||||||
"github.com/AlexxIT/go2rtc/internal/streams"
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/hap"
|
"github.com/AlexxIT/go2rtc/pkg/hap"
|
||||||
@@ -54,7 +54,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
items = append(items, device)
|
items = append(items, device)
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = json.NewEncoder(w).Encode(items)
|
api.ResponseJSON(w, items)
|
||||||
|
|
||||||
case "POST":
|
case "POST":
|
||||||
// TODO: post params...
|
// TODO: post params...
|
||||||
@@ -64,14 +64,14 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
name := r.URL.Query().Get("name")
|
name := r.URL.Query().Get("name")
|
||||||
if err := hkPair(id, pin, name); err != nil {
|
if err := hkPair(id, pin, name); err != nil {
|
||||||
log.Error().Err(err).Caller().Send()
|
log.Error().Err(err).Caller().Send()
|
||||||
_, err = w.Write([]byte(err.Error()))
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
src := r.URL.Query().Get("src")
|
src := r.URL.Query().Get("src")
|
||||||
if err := hkDelete(src); err != nil {
|
if err := hkDelete(src); err != nil {
|
||||||
log.Error().Err(err).Caller().Send()
|
log.Error().Err(err).Caller().Send()
|
||||||
_, err = w.Write([]byte(err.Error()))
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package streams
|
package streams
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"github.com/AlexxIT/go2rtc/internal/api"
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
"github.com/AlexxIT/go2rtc/internal/app"
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
"github.com/AlexxIT/go2rtc/internal/app/store"
|
"github.com/AlexxIT/go2rtc/internal/app/store"
|
||||||
@@ -81,16 +80,14 @@ func streamsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// without source - return all streams list
|
// without source - return all streams list
|
||||||
if src == "" && r.Method != "POST" {
|
if src == "" && r.Method != "POST" {
|
||||||
_ = json.NewEncoder(w).Encode(streams)
|
api.ResponseJSON(w, streams)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not sure about all this API. Should be rewrited...
|
// Not sure about all this API. Should be rewrited...
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
e := json.NewEncoder(w)
|
api.ResponsePrettyJSON(w, streams[src])
|
||||||
e.SetIndent("", " ")
|
|
||||||
_ = e.Encode(streams[src])
|
|
||||||
|
|
||||||
case "PUT":
|
case "PUT":
|
||||||
name := query.Get("name")
|
name := query.Get("name")
|
||||||
@@ -121,7 +118,7 @@ func streamsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err := stream.Play(src); err != nil {
|
if err := stream.Play(src); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
} else {
|
} else {
|
||||||
_ = json.NewEncoder(w).Encode(stream)
|
api.ResponseJSON(w, stream)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
http.Error(w, "", http.StatusNotFound)
|
http.Error(w, "", http.StatusNotFound)
|
||||||
|
|||||||
@@ -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)
|
||||||
_, _ = w.Write([]byte(data))
|
api.ResponseRawJSON(w, data)
|
||||||
|
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
if ok {
|
if ok {
|
||||||
|
|||||||
Reference in New Issue
Block a user