summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-11-28 07:59:27 -0500
committerTucker Evans <tuckerevans24@gmail.com>2019-11-28 07:59:27 -0500
commit950e9f1bd29ffdc82740e9ac237089eb61a25661 (patch)
tree37025e8aa9166b0d0d4bfa5631589eeed4ac4dda
parent4a7b855e85eb9e1dd04c7d9b0ede1f387bee31bc (diff)
Add sendResponse function
sendResponse function implements creating and sending a APIRespones so that this code will not be rewritten for every response situation i.e. the different http methods.
-rw-r--r--backend/main.go14
1 files changed, 13 insertions, 1 deletions
diff --git a/backend/main.go b/backend/main.go
index 9639b74..5b0e8e7 100644
--- a/backend/main.go
+++ b/backend/main.go
@@ -30,9 +30,21 @@ func MakeAPIResponse(status int, msg string, data interface{}) *APIResponse {
}
}
-func RecipeList(w http.ResponseWriter, r *http.Request) {
+func sendResponse(w http.ResponseWriter, code int, msg string, data interface{}) {
w.Header().Set("Access-Control-Allow-Origin", "*") //Enable CORS
+ w.WriteHeader(code)
+
+ resp := MakeAPIResponse(code, msg, data)
+
+ w.Header().Set("Content-Type",
+ "application/json; charset=UTF-8")
+
+ if err := json.NewEncoder(w).Encode(resp); err != nil {
+ panic(err)
+ }
+}
+func RecipeList(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
var ids []int
var id int