mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-12 10:02:48 +00:00
convert to chi web framework - add tls support
This commit is contained in:
32
server/response/response.go
Normal file
32
server/response/response.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultUnauthorized = []byte("Unauthorized.")
|
||||
DefaultInternalError = []byte("Internal error.")
|
||||
)
|
||||
|
||||
// JSON - marshals the provided interface and returns JSON to client
|
||||
func JSON(w http.ResponseWriter, content interface{}) {
|
||||
|
||||
output, err := json.Marshal(content)
|
||||
|
||||
if err != nil {
|
||||
ERR(w, http.StatusInternalServerError, []byte("Internal error."))
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("content-type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(output)
|
||||
}
|
||||
|
||||
// ERR - send error response
|
||||
func ERR(w http.ResponseWriter, status int, content []byte) {
|
||||
w.WriteHeader(status)
|
||||
w.Write(content)
|
||||
}
|
||||
Reference in New Issue
Block a user