upp/use.go

59 lines
908 B
Go
Raw Permalink Normal View History

package uzone
import (
"context"
"github.com/elastic/go-elasticsearch/v7"
"github.com/loveuer/uzone/pkg/cache"
"github.com/loveuer/uzone/pkg/interfaces"
"github.com/loveuer/uzone/pkg/tool"
"gorm.io/gorm"
)
func (u *uzone) Debug() bool {
return u.debug
}
func (u *uzone) UseCtx() context.Context {
return u.ctx
}
func (u *uzone) UseDB(ctx ...context.Context) *gorm.DB {
var c context.Context
if len(ctx) > 0 {
c = ctx[0]
} else {
c = tool.Timeout(30)
}
tx := u.db.Session(&gorm.Session{
Context: c,
})
if u.Debug() {
tx = tx.Debug()
}
return tx
}
func (u *uzone) UseCache() cache.Cache {
return u.cache
}
func (u *uzone) UseES() *elasticsearch.Client {
return u.es
}
func (u *uzone) UseLogger(ctxs ...context.Context) interfaces.Logger {
logger := u.logger.Get().(*uzone_logger)
logger.ctx = u.UseCtx()
if len(ctxs) > 0 {
logger.ctx = ctxs[0]
}
return logger
}