github.com/searKing/golang/go@v1.2.117/net/http/handlerinterceptorchain.options.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package http
     6  
     7  import (
     8  	"net/http"
     9  
    10  	"github.com/searKing/golang/go/net/http/internal"
    11  )
    12  
    13  func WithHandlerInterceptor(
    14  	// Intercept the execution of a handler.
    15  	// The default implementation returns true.
    16  	// Parameters:
    17  	// request - current HTTP request
    18  	// response - current HTTP response
    19  	// Returns:
    20  	// true if the execution chain should proceed with the next interceptor or the handler itself.
    21  	// Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself.
    22  	preHandle func(w http.ResponseWriter, r *http.Request) error,
    23  
    24  	// Intercept the execution of a handler.
    25  	// The default implementation is empty.
    26  	// Parameters:
    27  	// handler - current HTTP handler
    28  	// Returns:
    29  	// handler - wrapped HTTP handler
    30  	wrapHandle func(h http.Handler) http.Handler,
    31  
    32  	// Intercept the execution of a handler.
    33  	// The default implementation is empty.
    34  	// Parameters:
    35  	// request - current HTTP request
    36  	// response - current HTTP response
    37  	postHandle func(w http.ResponseWriter, r *http.Request),
    38  	// Callback after completion of request processing, that is, after rendering the view.
    39  	// The default implementation is empty.
    40  	// Parameters:
    41  	// request - current HTTP request
    42  	// response - current HTTP response
    43  	// ex - any exception thrown on handler execution, if any; this does not include exceptions that have been handled through an exception resolverreturns a new server interceptors with recovery from panic.
    44  	afterCompletion func(w http.ResponseWriter, r *http.Request, err any),
    45  ) HandlerInterceptorChainOption {
    46  	return HandlerInterceptorChainOptionFunc(func(chain *HandlerInterceptorChain) {
    47  		chain.interceptors = append(chain.interceptors, internal.HandlerInterceptor{
    48  			PreHandleFunc:       preHandle,
    49  			WrapHandleFunc:      wrapHandle,
    50  			PostHandleFunc:      postHandle,
    51  			AfterCompletionFunc: afterCompletion,
    52  		})
    53  	})
    54  }