You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
golog/api_gorm.go

90 lines
2.1 KiB

2 years ago
package golog
import (
"context"
5 months ago
"errors"
2 years ago
"go.dtapp.net/gorequest"
4 months ago
"gorm.io/gorm"
2 years ago
)
5 months ago
// ApiGorm 接口日志
type ApiGorm struct {
4 months ago
gormClient *gorm.DB // 数据库驱动
5 months ago
config struct {
systemHostname string // 主机名
systemOs string // 系统类型
systemVersion string // 系统版本
systemKernel string // 系统内核
systemKernelVersion string // 系统内核版本
systemBootTime uint64 // 系统开机时间
cpuCores int // CPU核数
cpuModelName string // CPU型号名称
cpuMhz float64 // CPU兆赫
systemInsideIp string // 内网ip
systemOutsideIp string // 外网ip
goVersion string // go版本
sdkVersion string // sdk版本
}
gormConfig struct {
stats bool // 状态
tableName string // 表名
2 years ago
}
2 years ago
}
5 months ago
// ApiGormFun 接口日志驱动
type ApiGormFun func() *ApiGorm
2 years ago
5 months ago
// NewApiGorm 创建接口实例化
4 months ago
func NewApiGorm(ctx context.Context, systemOutsideIp string, gormClient *gorm.DB, gormTableName string) (*ApiGorm, error) {
2 years ago
5 months ago
gl := &ApiGorm{}
2 years ago
5 months ago
// 配置信息
if systemOutsideIp == "" {
return nil, errors.New("没有设置外网IP")
2 years ago
}
5 months ago
gl.setConfig(ctx, systemOutsideIp)
2 years ago
4 months ago
if gormClient == nil {
5 months ago
gl.gormConfig.stats = false
} else {
5 months ago
gl.gormClient = gormClient
2 years ago
5 months ago
if gormTableName == "" {
return nil, errors.New("没有设置表名")
} else {
gl.gormConfig.tableName = gormTableName
2 years ago
}
5 months ago
// 创建模型
gl.gormAutoMigrate(ctx)
gl.gormConfig.stats = true
2 years ago
}
5 months ago
return gl, nil
2 years ago
}
5 months ago
// Middleware 中间件
4 months ago
func (ag *ApiGorm) Middleware(ctx context.Context, request gorequest.Response) {
if ag.gormConfig.stats {
ag.gormMiddleware(ctx, request)
2 years ago
}
5 months ago
}
// MiddlewareXml 中间件
4 months ago
func (ag *ApiGorm) MiddlewareXml(ctx context.Context, request gorequest.Response) {
if ag.gormConfig.stats {
ag.gormMiddlewareXml(ctx, request)
2 years ago
}
5 months ago
}
5 months ago
// MiddlewareCustom 中间件
4 months ago
func (ag *ApiGorm) MiddlewareCustom(ctx context.Context, api string, request gorequest.Response) {
if ag.gormConfig.stats {
ag.gormMiddlewareCustom(ctx, api, request)
5 months ago
}
2 years ago
}