Skip to content

Instantly share code, notes, and snippets.

@s1s1ty
Created July 18, 2018 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s1s1ty/fbebe710add636e0b5cdc9089fe34a18 to your computer and use it in GitHub Desktop.
Save s1s1ty/fbebe710add636e0b5cdc9089fe34a18 to your computer and use it in GitHub Desktop.
6th part
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
func catch(err error) {
if err != nil {
panic(err)
}
}
// respondwithError return error message
func respondWithError(w http.ResponseWriter, code int, msg string) {
respondwithJSON(w, code, map[string]string{"message": msg})
}
// respondwithJSON write json response format
func respondwithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
fmt.Println(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
// Logger return log message
func Logger() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println(time.Now(), r.Method, r.URL)
router.ServeHTTP(w, r) // dispatch the request
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment