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

     1  package handler
     2  
     3  import (
     4  	"github.com/gin-gonic/gin"
     5  	"github.com/lovung/GoCleanArchitecture/app/internal/appctx"
     6  	"github.com/lovung/GoCleanArchitecture/app/internal/interface/restful/presenter"
     7  )
     8  
     9  // BaseHandler help us respond to client
    10  type BaseHandler struct{}
    11  
    12  // SetMeta to put meta information into context
    13  func (h *BaseHandler) SetMeta(ctx *gin.Context, meta presenter.MetaResponse) {
    14  	newCtx := appctx.SetValue(ctx.Request.Context(), appctx.MetaContextKey, meta)
    15  	ctx.Request = ctx.Request.WithContext(newCtx)
    16  }
    17  
    18  // SetData to put data information into context
    19  func (h *BaseHandler) SetData(ctx *gin.Context, data interface{}) {
    20  	newCtx := appctx.SetValue(ctx.Request.Context(), appctx.DataContextKey, data)
    21  	ctx.Request = ctx.Request.WithContext(newCtx)
    22  }
    23  
    24  // SetError to put meta information into context
    25  func (h *BaseHandler) SetError(ctx *gin.Context, err error) {
    26  	newCtx := appctx.SetValue(ctx.Request.Context(), appctx.ErrorContextKey, err)
    27  	ctx.Request = ctx.Request.WithContext(newCtx)
    28  }