summaryrefslogtreecommitdiff
path: root/app/internal/utils/json.go
diff options
context:
space:
mode:
authorsoler_j <soler_j@etna-alternance.net>2025-05-04 21:32:12 +0200
committersoler_j <soler_j@etna-alternance.net>2025-05-04 21:32:12 +0200
commitdc3c72813a6358cde4c1bb3d1eaf618c6d46c460 (patch)
treeba342fe817171dd5f96d2d0aadf9bf95191a1842 /app/internal/utils/json.go
parentc981f4e36780c583556c03fc56fe43c0830f982e (diff)
Ajout de la gestion des middlewares pour le logging et l'authentification, ainsi que l'implémentation de fonctions utilitaires pour les réponses JSON.
Diffstat (limited to 'app/internal/utils/json.go')
-rw-r--r--app/internal/utils/json.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/app/internal/utils/json.go b/app/internal/utils/json.go
new file mode 100644
index 0000000..d3c6b71
--- /dev/null
+++ b/app/internal/utils/json.go
@@ -0,0 +1,22 @@
+package utils
+
+import (
+ "encoding/json"
+ "net/http"
+)
+
+func RespondWithError(w http.ResponseWriter, code int, message string) {
+ RespondWithJSON(w, map[string]string{"error": message}, code)
+}
+
+func RespondWithJSON(w http.ResponseWriter, payload interface{}, code ...int) {
+ var c int = http.StatusOK
+ if len(code) > 0 {
+ c = code[0]
+ }
+ response, _ := json.Marshal(payload)
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(c)
+ w.Write(response)
+}