github.com/avenga/couper@v1.12.2/handler/middleware/custom_logs.go (about)

     1  package middleware
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/hashicorp/hcl/v2"
     9  
    10  	"github.com/avenga/couper/config/request"
    11  )
    12  
    13  var _ http.Handler = &CustomLogs{}
    14  
    15  type CustomLogs struct {
    16  	bodies      []hcl.Body
    17  	handlerName string
    18  	next        http.Handler
    19  }
    20  
    21  func NewCustomLogsHandler(bodies []hcl.Body, next http.Handler, handlerName string) http.Handler {
    22  	return NewHandler(&CustomLogs{
    23  		bodies:      bodies,
    24  		handlerName: handlerName,
    25  		next:        next,
    26  	}, next)
    27  }
    28  
    29  func (c *CustomLogs) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    30  	ctx := context.WithValue(req.Context(), request.LogCustomAccess, c.bodies)
    31  	*req = *req.WithContext(ctx)
    32  
    33  	c.next.ServeHTTP(rw, req)
    34  }
    35  
    36  func (c *CustomLogs) String() string {
    37  	if hs, stringer := c.next.(fmt.Stringer); stringer {
    38  		return hs.String()
    39  	}
    40  
    41  	return c.handlerName
    42  }