diff options
| author | Kumar Damani <damani.kumar@gmail.com> | 2020-06-10 10:53:46 +0000 |
|---|---|---|
| committer | Kumar Damani <damani.kumar@gmail.com> | 2020-06-10 10:53:46 +0000 |
| commit | 16a60795670e48b06e36f73a5a890edbb4491d19 (patch) | |
| tree | a34a0a107271c548020613197c2e426da142a127 /src/common | |
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/common.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/common/common.go b/src/common/common.go new file mode 100644 index 0000000..3e50632 --- /dev/null +++ b/src/common/common.go @@ -0,0 +1,32 @@ +// ONLY PUT FUNCTIONS / VARS THAT WILL BE USED BY MANY PACKAGES +package common + +import ( + "encoding/json" + "fmt" + "github.com/gorilla/mux" + "log" + "net/http" +) + +// Every respose MUST be a single string. +type response struct { + Mssg string +} + +// Builds and write json response given http status code, and message. +func JsonRespond(w *http.ResponseWriter, rc int, body string) { + res1D := &response{Mssg: body} + res1B, _ := json.Marshal(res1D) + (*w).WriteHeader(rc) + log.Printf("RC: %d | Message: %s", rc, body) + fmt.Fprintln(*w, string(res1B)) +} + +// Assign a handler function to an endpoint. +func MakeHTTPHandler(route string, h http.HandlerFunc) http.Handler { + r := mux.NewRouter() + r.HandleFunc("/"+route, h) + r.HandleFunc("/"+route+"/", h) + return r +} |
