diff options
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 +} |
