logrequest_middleware.go 626 字节
package middleware

import (
	"github.com/zeromicro/go-zero/rest/handler"
	"net/http"
)

type LogRequestMiddleware struct {
	logRequest bool
}

// NewLogRequestMiddleware 记录请求
// logRequest true开启记录,false关闭记录
func NewLogRequestMiddleware(logRequest bool) *LogRequestMiddleware {
	return &LogRequestMiddleware{
		logRequest: logRequest,
	}
}
func (m *LogRequestMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
	if !m.logRequest {
		return func(writer http.ResponseWriter, request *http.Request) {
			next(writer, request)
		}
	}
	return handler.DetailedLogHandler(next).(http.HandlerFunc)
}