Second Commit
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Empty makes a 204 response without a body
|
||||
func Empty(writer http.ResponseWriter) {
|
||||
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
writer.WriteHeader(204)
|
||||
writer.Write([]byte{})
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// HTML makes an HTML response
|
||||
func HTML(writer http.ResponseWriter, status int, data string) {
|
||||
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
writer.WriteHeader(status)
|
||||
writer.Write([]byte(data))
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// JSON makes a JSON response
|
||||
func JSON(writer http.ResponseWriter, status int, data interface{}) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
|
||||
if err != nil {
|
||||
log.Println("JSON Marshal failed: ", err.Error())
|
||||
|
||||
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
writer.WriteHeader(503)
|
||||
fmt.Fprint(writer, "JSON marshalling failed:", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
writer.WriteHeader(status)
|
||||
writer.Write(jsonData)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Text makes a textual response
|
||||
func Text(writer http.ResponseWriter, status int, data string) {
|
||||
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
writer.WriteHeader(status)
|
||||
writer.Write([]byte(data))
|
||||
}
|
||||
Reference in New Issue
Block a user