示例#1
0
func httpInterceptor(w http.ResponseWriter, req *http.Request) {
	startTime := time.Now()

	router.ServeHTTP(w, req)

	finishTime := time.Now()
	elapsedTime := finishTime.Sub(startTime)

	common.LogAccess(w, req, elapsedTime)
}
示例#2
0
func httpInterceptor(router http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		startTime := time.Now()

		router.ServeHTTP(w, req)

		finishTime := time.Now()
		elapsedTime := finishTime.Sub(startTime)

		switch req.Method {
		case "GET":
			// We may not always want to StatusOK, but for the sake of
			// this example we will
			common.LogAccess(w, req, elapsedTime)
		case "POST":
			// here we might use http.StatusCreated
		}

	})
}