Created
July 18, 2018 17:09
-
-
Save s1s1ty/fbebe710add636e0b5cdc9089fe34a18 to your computer and use it in GitHub Desktop.
6th part
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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