github.com/lovung/GoCleanArchitecture@v0.0.0-20210302152432-50d91fd29f9f/app/internal/interface/restful/middleware/context_middleware.go (about)

     1  package middleware
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/gin-gonic/gin"
     7  	"github.com/lovung/GoCleanArchitecture/app/config"
     8  )
     9  
    10  // AddTimeout for context
    11  func AddTimeout(ctx *gin.Context) {
    12  	// Pass a context with a timeout to tell a blocking function that it
    13  	// should abandon its work after the timeout elapses.
    14  	ctxWTimeout, cancel := context.WithTimeout(
    15  		ctx.Request.Context(),
    16  		config.GetConfig().HTTPServer.Timeout,
    17  	)
    18  	ctx.Request = ctx.Request.WithContext(ctxWTimeout)
    19  	defer cancel()
    20  	ctx.Next()
    21  }