SimpleCloudNotifier/server/common/ginresp/wrapper.go

26 lines
369 B
Go
Raw Normal View History

2022-11-13 19:17:07 +01:00
package ginresp
import "github.com/gin-gonic/gin"
type WHandlerFunc func(*gin.Context) HTTPResponse
func Wrap(fn WHandlerFunc) gin.HandlerFunc {
2022-11-18 21:25:40 +01:00
return func(g *gin.Context) {
2022-11-13 19:17:07 +01:00
2022-11-19 12:47:23 +01:00
reqctx := g.Request.Context()
2022-11-18 21:25:40 +01:00
wrap := fn(g)
2022-11-13 19:17:07 +01:00
2022-11-18 21:25:40 +01:00
if g.Writer.Written() {
2022-11-13 19:17:07 +01:00
panic("Writing in WrapperFunc is not supported")
}
2022-11-19 12:47:23 +01:00
if reqctx.Err() == nil {
wrap.Write(g)
}
2022-11-13 19:17:07 +01:00
}
}