|
|
@ -14,12 +14,14 @@ import (
|
|
|
|
|
|
|
|
|
|
|
|
// GinClient 框架
|
|
|
|
// GinClient 框架
|
|
|
|
type GinClient struct {
|
|
|
|
type GinClient struct {
|
|
|
|
gormClient *gorm.DB // 驱动
|
|
|
|
gormClient *gorm.DB // 数据库驱动
|
|
|
|
mongoCollectionClient *dorm.MongoClient // 驱动(温馨提示:需要已选择库)
|
|
|
|
mongoClient *dorm.MongoClient // 数据库驱动
|
|
|
|
ipService *goip.Client // ip服务
|
|
|
|
ipService *goip.Client // ip服务
|
|
|
|
config struct {
|
|
|
|
config struct {
|
|
|
|
logType string // 日志类型
|
|
|
|
logType string // 日志类型
|
|
|
|
tableName string // 表名
|
|
|
|
tableName string // 表名
|
|
|
|
|
|
|
|
databaseName string // 库名
|
|
|
|
|
|
|
|
collectionName string // 表名
|
|
|
|
insideIp string // 内网ip
|
|
|
|
insideIp string // 内网ip
|
|
|
|
hostname string // 主机名
|
|
|
|
hostname string // 主机名
|
|
|
|
goVersion string // go版本
|
|
|
|
goVersion string // go版本
|
|
|
@ -27,6 +29,8 @@ type GinClient struct {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewGinClient 创建框架实例化
|
|
|
|
// NewGinClient 创建框架实例化
|
|
|
|
|
|
|
|
// WithGormClient && WithTableName
|
|
|
|
|
|
|
|
// WithMongoCollectionClient && WithDatabaseName && WithCollectionName
|
|
|
|
func NewGinClient(attrs ...*OperationAttr) (*GinClient, error) {
|
|
|
|
func NewGinClient(attrs ...*OperationAttr) (*GinClient, error) {
|
|
|
|
|
|
|
|
|
|
|
|
c := &GinClient{}
|
|
|
|
c := &GinClient{}
|
|
|
@ -35,13 +39,19 @@ func NewGinClient(attrs ...*OperationAttr) (*GinClient, error) {
|
|
|
|
c.gormClient = attr.gormClient
|
|
|
|
c.gormClient = attr.gormClient
|
|
|
|
c.config.logType = attr.logType
|
|
|
|
c.config.logType = attr.logType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if attr.mongoCollectionClient != nil {
|
|
|
|
if attr.mongoClient != nil {
|
|
|
|
c.mongoCollectionClient = attr.mongoCollectionClient
|
|
|
|
c.mongoClient = attr.mongoClient
|
|
|
|
c.config.logType = attr.logType
|
|
|
|
c.config.logType = attr.logType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if attr.tableName != "" {
|
|
|
|
if attr.tableName != "" {
|
|
|
|
c.config.tableName = attr.tableName
|
|
|
|
c.config.tableName = attr.tableName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr.databaseName != "" {
|
|
|
|
|
|
|
|
c.config.databaseName = attr.databaseName
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr.collectionName != "" {
|
|
|
|
|
|
|
|
c.config.collectionName = attr.collectionName
|
|
|
|
|
|
|
|
}
|
|
|
|
if attr.ipService != nil {
|
|
|
|
if attr.ipService != nil {
|
|
|
|
c.ipService = attr.ipService
|
|
|
|
c.ipService = attr.ipService
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -51,11 +61,11 @@ func NewGinClient(attrs ...*OperationAttr) (*GinClient, error) {
|
|
|
|
case logTypeGorm:
|
|
|
|
case logTypeGorm:
|
|
|
|
|
|
|
|
|
|
|
|
if c.gormClient == nil {
|
|
|
|
if c.gormClient == nil {
|
|
|
|
return nil, errors.New("驱动不能为空")
|
|
|
|
return nil, errors.New("没有设置驱动")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if c.config.tableName == "" {
|
|
|
|
if c.config.tableName == "" {
|
|
|
|
return nil, errors.New("表名不能为空")
|
|
|
|
return nil, errors.New("没有设置表名")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err := c.gormClient.Table(c.config.tableName).AutoMigrate(&apiPostgresqlLog{})
|
|
|
|
err := c.gormClient.Table(c.config.tableName).AutoMigrate(&apiPostgresqlLog{})
|
|
|
@ -65,12 +75,16 @@ func NewGinClient(attrs ...*OperationAttr) (*GinClient, error) {
|
|
|
|
|
|
|
|
|
|
|
|
case logTypeMongo:
|
|
|
|
case logTypeMongo:
|
|
|
|
|
|
|
|
|
|
|
|
if c.mongoCollectionClient.Db == nil {
|
|
|
|
if c.mongoClient.Db == nil {
|
|
|
|
return nil, errors.New("驱动不能为空")
|
|
|
|
return nil, errors.New("没有设置驱动")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if c.config.tableName == "" {
|
|
|
|
if c.config.tableName == "" {
|
|
|
|
return nil, errors.New("表名不能为空")
|
|
|
|
return nil, errors.New("没有设置库名")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if c.config.collectionName == "" {
|
|
|
|
|
|
|
|
return nil, errors.New("没有设置表名")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|