30 lines
539 B
Go
30 lines
539 B
Go
|
package response
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
func JSON(w http.ResponseWriter, status int, data any) error {
|
||
|
return JSONWithHeaders(w, status, data, nil)
|
||
|
}
|
||
|
|
||
|
func JSONWithHeaders(w http.ResponseWriter, status int, data any, headers http.Header) error {
|
||
|
js, err := json.MarshalIndent(data, "", "\t")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
js = append(js, '\n')
|
||
|
|
||
|
for key, value := range headers {
|
||
|
w.Header()[key] = value
|
||
|
}
|
||
|
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
w.WriteHeader(status)
|
||
|
w.Write(js)
|
||
|
|
||
|
return nil
|
||
|
}
|