diff options
| author | soler_j <soler_j@etna-alternance.net> | 2025-05-04 21:32:12 +0200 |
|---|---|---|
| committer | soler_j <soler_j@etna-alternance.net> | 2025-05-04 21:32:12 +0200 |
| commit | dc3c72813a6358cde4c1bb3d1eaf618c6d46c460 (patch) | |
| tree | ba342fe817171dd5f96d2d0aadf9bf95191a1842 /app/internal/utils/json.go | |
| parent | c981f4e36780c583556c03fc56fe43c0830f982e (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.go | 22 |
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) +} |
