gopay/internal/context/context.go

36 lines
633 B
Go

package context
import (
"git.supernets.org/perp/gopay/internal/config"
"github.com/gin-gonic/gin"
)
// Request context
type Context struct {
*gin.Context
*gin.Engine
Config *config.Config
}
// Return a new Context
func New(cfg *config.Config) *Context {
// Create engine
engine := gin.New()
engine.Use(gin.Recovery())
// Debug level
if cfg.Log.Level == "debug" {
engine.Use(gin.Logger())
}
return &Context{Engine: engine, Config: cfg}
}
// Handle gin route with context
func (ctx *Context) API(handler func(ctx *Context)) func(*gin.Context) {
return func(c *gin.Context) {
ctx.Context = c
handler(ctx)
}
}