feat: added API gateway

This commit is contained in:
ITQ
2025-12-17 11:00:50 +03:00
parent daa8c24482
commit 340ae43d22
30 changed files with 2838 additions and 3 deletions
+35
View File
@@ -0,0 +1,35 @@
package utils
import (
"encoding/json"
"net/http"
"datarush/internal/gw/domain"
)
func RespondJSON(w http.ResponseWriter, statusCode int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
if data != nil {
if err := json.NewEncoder(w).Encode(data); err != nil {
http.Error(w, "internal server error", http.StatusInternalServerError)
}
}
}
func RespondError(w http.ResponseWriter, err error) {
if appErr, ok := err.(*domain.AppError); ok {
RespondJSON(w, appErr.StatusCode, domain.NewErrorResponse(appErr.Err, appErr.Message))
return
}
RespondJSON(w, http.StatusInternalServerError, domain.NewErrorResponse(err, "internal server error"))
}
func DecodeJSON(r *http.Request, v interface{}) error {
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
return domain.NewBadRequestError("invalid JSON body")
}
return nil
}