github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/http/context.go (about)

     1  package http
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
     8  )
     9  
    10  func NewContextInjectorMw(ctx contextx.WithContext) Middleware {
    11  	return func(next http.Handler) http.Handler {
    12  		return http.HandlerFunc(
    13  			func(rw http.ResponseWriter, req *http.Request) {
    14  				if ctx != nil {
    15  					req = req.WithContext(ctx(req.Context()))
    16  				}
    17  				next.ServeHTTP(rw, req)
    18  			},
    19  		)
    20  	}
    21  }
    22  
    23  type Middleware = func(http.Handler) http.Handler
    24  
    25  func WithContextCompose(contexts ...contextx.WithContext) contextx.WithContext {
    26  	return func(ctx context.Context) context.Context {
    27  		for i := range contexts {
    28  			ctx = contexts[i](ctx)
    29  		}
    30  		return ctx
    31  	}
    32  }