2024-06-02 11:57:14 +00:00
|
|
|
package context
|
|
|
|
|
2024-06-02 13:08:38 +00:00
|
|
|
import (
|
|
|
|
"git.supernets.org/perp/gopay/internal/config"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
2024-06-02 11:57:14 +00:00
|
|
|
|
|
|
|
// Request context
|
|
|
|
type Context struct {
|
2024-06-02 13:08:38 +00:00
|
|
|
*gin.Context
|
|
|
|
*gin.Engine
|
2024-06-02 11:57:14 +00:00
|
|
|
Config *config.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a new Context
|
2024-06-02 13:08:38 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-06-02 11:57:14 +00:00
|
|
|
}
|