Add content-type to API responses

This commit is contained in:
Alexey Khit
2023-05-28 06:37:59 +03:00
parent 1612f9c81e
commit 0e270081fe
7 changed files with 42 additions and 27 deletions
+29 -8
View File
@@ -88,6 +88,30 @@ func HandleFunc(pattern string, handler http.HandlerFunc) {
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"
var basePath string
@@ -131,9 +155,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
app.Info["host"] = r.Host
mu.Unlock()
if err := json.NewEncoder(w).Encode(app.Info); err != nil {
log.Warn().Err(err).Caller().Send()
}
ResponseJSON(w, app.Info)
}
func exitHandler(w http.ResponseWriter, r *http.Request) {
@@ -158,11 +180,10 @@ func ResponseStreams(w http.ResponseWriter, streams []Stream) {
return
}
var response struct {
var response = struct {
Streams []Stream `json:"streams"`
}{
Streams: streams,
}
response.Streams = streams
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
ResponseJSON(w, response)
}
+1 -3
View File
@@ -21,9 +21,7 @@ func configHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "", http.StatusNotFound)
return
}
if _, err = w.Write(data); err != nil {
log.Warn().Err(err).Caller().Send()
}
ResponseText(w, data)
case "POST", "PATCH":
data, err := io.ReadAll(r.Body)