Second Commit

This commit is contained in:
2017-08-06 17:48:01 +02:00
parent 591462e3c1
commit d754bfb686
17 changed files with 1205 additions and 0 deletions
+12
View File
@@ -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{})
}
+12
View File
@@ -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))
}
+26
View File
@@ -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)
}
+12
View File
@@ -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))
}