From 950e9f1bd29ffdc82740e9ac237089eb61a25661 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Thu, 28 Nov 2019 07:59:27 -0500 Subject: 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. --- backend/main.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'backend') 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 -- cgit v1.1