package utils import ( "encoding/json" "net/http" ) // JSONResponse convenient way to define jwtErrors type JSONResponse struct { Status string `json:"status"` Details string `json:"details"` } // TokenResponseStruct define JWT return format type TokenResponseStruct struct { Token string `json:"token"` Expiration int64 `json:"expiration"` } // JSONResponseToken convenient way to define jwtErrors type JSONResponseToken struct { Status string `json:"status"` Details TokenResponseStruct `json:"details"` } // SendJSONErrorResponse sends an API error encoding in JSON func SendJSONErrorResponse(w http.ResponseWriter, err string, status int) { var response = JSONResponse{ Status: "error", Details: err, } js, error := json.Marshal(response) if error != nil { http.Error(w, error.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(status) w.Write(js) } // SendJSONAcknowledgeResponse sends an API error encoding in JSON func SendJSONAcknowledgeResponse(w http.ResponseWriter, message string, status int) { var response = JSONResponse{ Status: "ok", Details: message, } js, error := json.Marshal(response) if error != nil { http.Error(w, error.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(status) w.Write(js) } // SendJSONTokenAcknowledgeResponse send token as JSON format func SendJSONTokenAcknowledgeResponse(w http.ResponseWriter, message TokenResponseStruct, status int) { var response = JSONResponseToken{ Status: "ok", Details: message, } js, error := json.Marshal(response) if error != nil { http.Error(w, error.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(status) w.Write(js) }