1 Star 0 Fork 0

hwfo/hwf

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
hwf_ctx.go 3.19 KB
一键复制 编辑 原始数据 按行查看 历史
haitgo 提交于 2025-12-27 21:05 +08:00 . 细节优化
package hwf
import (
"context"
"log"
"sync"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
// Context is the framework context used in routes and modules.
// It carries logger and ORM handles and supports transactions.
type HwfCtx struct {
context.Context
logger *Logger
orm *ORM
eng *engine
// request-scoped key-value storage
data map[string]any
dataMu sync.RWMutex
}
func NewCtx() *HwfCtx {
return &HwfCtx{
Context: context.Background(),
logger: GetLogger(),
orm: GetORM(),
eng: eng,
data: map[string]any{},
}
}
// Ctx returns the underlying context.Context.
func (c *HwfCtx) Ctx() context.Context {
return c.Context
}
func (c *HwfCtx) Log() *Logger {
return c.logger
}
// DB returns the default gorm.DB directly (simplified).
func (c *HwfCtx) DB() *gorm.DB {
if c.orm == nil {
return nil
}
return c.orm.DB()
}
// ORM returns the ORM wrapper for advanced helpers like Transaction.
func (c *HwfCtx) ORM() *ORM {
if c.orm == nil {
return nil
}
return c.orm
}
// DBNamed returns a named database handle if configured, otherwise the default.
func (c *HwfCtx) DBNamed(name string) *gorm.DB {
if orm := GetNamedORM(name); orm != nil {
return orm.DB()
}
return c.DB()
}
// Page 分页查询 返回总条数和错误信息
func (c *HwfCtx) PageFind(db *gorm.DB, page int, pageSize int, ptr any) (int64, error) {
var total int64
if err := db.Count(&total).Error; err != nil {
return 0, err
}
if page <= 0 {
page = 1
}
if pageSize <= 0 || pageSize > 100 {
pageSize = 10
}
err := db.Offset((page - 1) * pageSize).Limit(pageSize).Find(ptr).Error
return total, err
}
// Transaction helper to run fn within a transaction.
func (c *HwfCtx) Transaction(fn func() error) error {
return c.orm.Transaction(fn)
}
// Deprecated: StdLog for quick logging in examples.
func (c *HwfCtx) StdLog() *log.Logger { return c.logger.std }
// Resource returns the resource engine for reading embedded files.
func (c *HwfCtx) Resource() ResourceEngine {
if app := c.eng.app; app != nil {
if r, ok := app.(AppPackResourcer); ok {
return newResource(r.PackResource())
}
}
return nil
}
// PutHook puts a hook payload for the given name in the context.
func (c *HwfCtx) PutHook(name string, payload any) {
if h := getHooks(); h != nil {
h.Put(name, c, payload)
}
}
// Set stores a value in the request-scoped storage.
func (c *HwfCtx) SetVal(key string, val any) {
c.dataMu.Lock()
if c.data == nil {
c.data = map[string]any{}
}
c.data[key] = val
c.dataMu.Unlock()
}
// Get retrieves a value from the request-scoped storage.
// Returns (nil, false) if key missing.
func (c *HwfCtx) GetVal(key string) (any, bool) {
c.dataMu.RLock()
v, ok := c.data[key]
c.dataMu.RUnlock()
return v, ok
}
// Redis returns the Redis client if configured.
func (c *HwfCtx) Redis() *redis.Client {
return GetRedis()
}
// NamedRedis returns the named Redis client if configured.
func (c *HwfCtx) NamedRedis(name string) *redis.Client {
return GetNamedRedis(name)
}
// Returns zero value and false if key is missing or type assertion fails.
func CtxGet[T any](c *HwfCtx, key string) (T, bool) {
var zero T
v, ok := c.GetVal(key)
if !ok {
return zero, false
}
val, ok := v.(T)
return val, ok
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hwfo/hwf.git
git@gitee.com:hwfo/hwf.git
hwfo
hwf
hwf
ba98abdfc0af

搜索帮助