Initial commit

This commit is contained in:
2023-07-02 15:26:53 +01:00
committed by GitHub
commit 16ca35eb45
42 changed files with 4536 additions and 0 deletions

34
helpers/json.go Normal file
View File

@@ -0,0 +1,34 @@
package helpers
import (
"encoding/json"
"log"
"net/http"
)
func RespondWithError(w http.ResponseWriter, code int, msg string) {
if code > 499 {
log.Println("Responding with 5XX error: ", msg)
}
type errResponse struct {
Error string `json:"error"`
}
RespondWithJson(w, code, errResponse{
Error: msg,
})
}
func RespondWithJson(w http.ResponseWriter, code int, payload interface{}) {
data, err := json.Marshal(payload)
if err != nil {
log.Printf("Failed to marshall JSON response: %v", payload)
w.WriteHeader(500)
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(data)
}