- update gin

- update api
master v1.0.41
李光春 2 years ago
parent 101ea45f6c
commit b615f8f1b1

@ -9,24 +9,16 @@ import (
"runtime"
)
type ApiClientConfig struct {
GormClient *dorm.GormClient // 数据库驱动
TableName string // 表名
LogClient *ZapLog // 日志驱动
LogDebug bool // 日志开关
}
// ApiClient 接口
type ApiClient struct {
gormClient *dorm.GormClient // 数据库驱动
mongoClient *dorm.MongoClient // 数据库驱动
logClient *ZapLog // 日志驱动
config struct {
gormConfig struct {
tableName string // 表名
insideIp string // 内网ip
hostname string // 主机名
goVersion string // go版本
logDebug bool // 日志开关
debug bool // 日志开关
}
mongoConfig struct {
databaseName string // 库名
@ -38,49 +30,57 @@ type ApiClient struct {
}
}
// NewApiClient 创建接口实例化
func NewApiClient(config *ApiClientConfig) (*ApiClient, error) {
// NewApiGormClient 创建接口实例化
// client 数据库服务
// tableName 表名
func NewApiGormClient(gormClientFun func() (client *dorm.GormClient, tableName string), debug bool) (*ApiClient, error) {
c := &ApiClient{}
c.gormClient = config.GormClient
c.config.tableName = config.TableName
client, tableName := gormClientFun()
c.logClient = config.LogClient
c.config.logDebug = config.LogDebug
if c.gormClient.Db == nil {
return nil, errors.New("驱动不能为空")
if client == nil || client.Db == nil {
return nil, errors.New("没有设置驱动")
}
if c.config.tableName == "" {
return nil, errors.New("表名不能为空")
c.gormClient = client
if tableName == "" {
return nil, errors.New("没有设置表名")
}
c.gormConfig.tableName = tableName
err := c.gormClient.Db.Table(c.config.tableName).AutoMigrate(&apiPostgresqlLog{})
c.gormConfig.debug = debug
err := c.gormClient.Db.Table(c.gormConfig.tableName).AutoMigrate(&apiPostgresqlLog{})
if err != nil {
return nil, errors.New("创建表失败:" + err.Error())
}
hostname, _ := os.Hostname()
c.config.hostname = hostname
c.config.insideIp = goip.GetInsideIp(context.Background())
c.config.goVersion = runtime.Version()
c.gormConfig.hostname = hostname
c.gormConfig.insideIp = goip.GetInsideIp(context.Background())
c.gormConfig.goVersion = runtime.Version()
return c, nil
}
// NewApiMongoClient 创建接口实例化
func NewApiMongoClient(mongoClient *dorm.MongoClient, databaseName string, collectionName string, debug bool) (*ApiClient, error) {
// client 数据库服务
// databaseName 库名
// collectionName 表名
func NewApiMongoClient(mongoClientFun func() (client *dorm.MongoClient, databaseName string, collectionName string), debug bool) (*ApiClient, error) {
c := &ApiClient{}
if mongoClient.Db == nil {
client, databaseName, collectionName := mongoClientFun()
if client == nil || client.Db == nil {
return nil, errors.New("没有设置驱动")
}
c.mongoClient = mongoClient
c.mongoClient = client
if databaseName == "" {
return nil, errors.New("没有设置库名")

@ -9,6 +9,7 @@ import (
"go.dtapp.net/gourl"
"gorm.io/datatypes"
"gorm.io/gorm"
"log"
"time"
"unicode/utf8"
)
@ -42,20 +43,20 @@ func (c *ApiClient) gormRecord(ctx context.Context, postgresqlLog apiPostgresqlL
postgresqlLog.ResponseBody = datatypes.JSON("")
}
postgresqlLog.SystemHostName = c.config.hostname
postgresqlLog.SystemHostName = c.gormConfig.hostname
if postgresqlLog.SystemInsideIp == "" {
postgresqlLog.SystemInsideIp = c.config.insideIp
postgresqlLog.SystemInsideIp = c.gormConfig.insideIp
}
postgresqlLog.GoVersion = c.config.goVersion
postgresqlLog.GoVersion = c.gormConfig.goVersion
postgresqlLog.TraceId = gotrace_id.GetTraceIdContext(ctx)
return c.gormClient.Db.Table(c.config.tableName).Create(&postgresqlLog).Error
return c.gormClient.Db.Table(c.gormConfig.tableName).Create(&postgresqlLog).Error
}
// GormQuery 查询
func (c *ApiClient) GormQuery() *gorm.DB {
return c.gormClient.Db.Table(c.config.tableName)
return c.gormClient.Db.Table(c.gormConfig.tableName)
}
// GormMiddleware 中间件
@ -79,8 +80,8 @@ func (c *ApiClient) GormMiddleware(ctx context.Context, request gorequest.Respon
SdkVersion: sdkVersion, //【程序】Sdk版本
})
if err != nil {
if c.config.logDebug {
c.logClient.Errorf(ctx, "[log.GormMiddleware]%s", err.Error())
if c.gormConfig.debug {
log.Printf("[log.GormMiddleware]%s\n", err.Error())
}
}
}
@ -103,8 +104,8 @@ func (c *ApiClient) GormMiddlewareXml(ctx context.Context, request gorequest.Res
SdkVersion: sdkVersion, //【程序】Sdk版本
})
if err != nil {
if c.config.logDebug {
c.logClient.Errorf(ctx, "[log.GormMiddlewareXml]%s", err.Error())
if c.gormConfig.debug {
log.Printf("[log.GormMiddlewareXml]%s\n", err.Error())
}
}
}
@ -127,8 +128,8 @@ func (c *ApiClient) GormMiddlewareCustom(ctx context.Context, api string, reques
SdkVersion: sdkVersion, //【程序】Sdk版本
})
if err != nil {
if c.config.logDebug {
c.logClient.Errorf(ctx, "[log.GormMiddlewareCustom]%s", err.Error())
if c.gormConfig.debug {
log.Printf("[log.GormMiddlewareCustom]%s\n", err.Error())
}
}
}

@ -7,6 +7,7 @@ import (
"go.dtapp.net/gotrace_id"
"go.dtapp.net/gourl"
"go.mongodb.org/mongo-driver/bson/primitive"
"log"
"time"
)
@ -73,8 +74,8 @@ func (c *ApiClient) MongoMiddleware(ctx context.Context, request gorequest.Respo
SdkVersion: sdkVersion, //【程序】Sdk版本
})
if err != nil {
if c.config.logDebug {
c.logClient.Errorf(ctx, "[log.MongoMiddleware]%s", err.Error())
if c.mongoConfig.debug {
log.Printf("[log.MongoMiddleware]%s\n", err.Error())
}
}
}
@ -97,8 +98,8 @@ func (c *ApiClient) MongoMiddlewareXml(ctx context.Context, request gorequest.Re
SdkVersion: sdkVersion, //【程序】Sdk版本
})
if err != nil {
if c.config.logDebug {
c.logClient.Errorf(ctx, "[log.MongoMiddlewareXml]%s", err.Error())
if c.mongoConfig.debug {
log.Printf("[log.MongoMiddlewareXml]%s\n", err.Error())
}
}
}
@ -121,8 +122,8 @@ func (c *ApiClient) MongoMiddlewareCustom(ctx context.Context, api string, reque
SdkVersion: sdkVersion, //【程序】Sdk版本
})
if err != nil {
if c.config.logDebug {
c.logClient.Errorf(ctx, "[log.MongoMiddlewareCustom]%s", err.Error())
if c.mongoConfig.debug {
log.Printf("[log.MongoMiddlewareCustom]%s\n", err.Error())
}
}
}

@ -1,5 +1,5 @@
package golog
const (
Version = "1.0.40"
Version = "1.0.41"
)

@ -12,26 +12,17 @@ import (
"runtime"
)
type GinClientConfig struct {
GormClient *dorm.GormClient // 数据库驱动
IpService *goip.Client // ip服务
TableName string // 表名
LogClient *ZapLog // 日志驱动
LogDebug bool // 日志开关
}
// GinClient 框架
type GinClient struct {
gormClient *dorm.GormClient // 数据库驱动
mongoClient *dorm.MongoClient // 数据库驱动
ipService *goip.Client // ip服务
logClient *ZapLog // 日志驱动
config struct {
gormConfig struct {
tableName string // 表名
insideIp string // 内网ip
hostname string // 主机名
goVersion string // go版本
logDebug bool // 日志开关
debug bool // 日志开关
}
mongoConfig struct {
databaseName string // 库名
@ -43,55 +34,61 @@ type GinClient struct {
}
}
// NewGinClient 创建框架实例化
func NewGinClient(config *GinClientConfig) (*GinClient, error) {
// NewGinGormClient 创建框架实例化
// client 数据库服务
// tableName=表名
// ipService ip服务
func NewGinGormClient(gormClientFun func() (client *dorm.GormClient, tableName string), ipService *goip.Client, debug bool) (*GinClient, error) {
c := &GinClient{}
c.gormClient = config.GormClient
c.config.tableName = config.TableName
client, tableName := gormClientFun()
c.logClient = config.LogClient
c.config.logDebug = config.LogDebug
c.ipService = config.IpService
if c.gormClient.Db == nil {
if client == nil || client.Db == nil {
return nil, errors.New("没有设置驱动")
}
if c.config.tableName == "" {
c.gormClient = client
if tableName == "" {
return nil, errors.New("没有设置表名")
}
c.gormConfig.tableName = tableName
err := c.gormClient.Db.Table(c.config.tableName).AutoMigrate(&ginPostgresqlLog{})
c.gormConfig.debug = debug
c.ipService = ipService
err := c.gormClient.Db.Table(c.gormConfig.tableName).AutoMigrate(&ginPostgresqlLog{})
if err != nil {
return nil, errors.New("创建表失败:" + err.Error())
}
hostname, _ := os.Hostname()
c.config.hostname = hostname
c.config.insideIp = goip.GetInsideIp(context.Background())
c.config.goVersion = runtime.Version()
c.gormConfig.hostname = hostname
c.gormConfig.insideIp = goip.GetInsideIp(context.Background())
c.gormConfig.goVersion = runtime.Version()
return c, nil
}
// NewGinMongoClient 创建框架实例化
// mongoClient 数据库服务
// ipService ip服务
// client 数据库服务
// databaseName 库名
// collectionName 表名
func NewGinMongoClient(mongoClient *dorm.MongoClient, ipService *goip.Client, databaseName string, collectionName string, debug bool) (*GinClient, error) {
// ipService ip服务
func NewGinMongoClient(mongoClientFun func() (client *dorm.MongoClient, databaseName string, collectionName string), ipService *goip.Client, debug bool) (*GinClient, error) {
c := &GinClient{}
if mongoClient.Db == nil {
client, databaseName, collectionName := mongoClientFun()
if client == nil || client.Db == nil {
return nil, errors.New("没有设置驱动")
}
c.mongoClient = mongoClient
c.mongoClient = client
if databaseName == "" {
return nil, errors.New("没有设置库名")

@ -2,7 +2,6 @@ package golog
import (
"bytes"
"context"
"encoding/json"
"github.com/gin-gonic/gin"
"go.dtapp.net/gojson"
@ -54,20 +53,20 @@ type ginPostgresqlLog struct {
// gormRecord 记录日志
func (c *GinClient) gormRecord(postgresqlLog ginPostgresqlLog) error {
postgresqlLog.SystemHostName = c.config.hostname
postgresqlLog.SystemHostName = c.gormConfig.hostname
if postgresqlLog.SystemInsideIp == "" {
postgresqlLog.SystemInsideIp = c.config.insideIp
postgresqlLog.SystemInsideIp = c.gormConfig.insideIp
}
postgresqlLog.GoVersion = c.config.goVersion
postgresqlLog.GoVersion = c.gormConfig.goVersion
postgresqlLog.SdkVersion = Version
return c.gormClient.Db.Table(c.config.tableName).Create(&postgresqlLog).Error
return c.gormClient.Db.Table(c.gormConfig.tableName).Create(&postgresqlLog).Error
}
// GormQuery 查询
func (c *GinClient) GormQuery() *gorm.DB {
return c.gormClient.Db.Table(c.config.tableName)
return c.gormClient.Db.Table(c.gormConfig.tableName)
}
// GormMiddleware 中间件
@ -81,7 +80,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
// 获取
data, _ := ioutil.ReadAll(ginCtx.Request.Body)
if c.config.logDebug {
if c.gormConfig.debug {
log.Printf("[golog.GormMiddleware] %s\n", data)
}
@ -103,8 +102,6 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
go func() {
var ctx = gotrace_id.SetGinTraceIdContext(context.Background(), ginCtx)
var dataJson = true
// 解析请求内容
@ -119,12 +116,12 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
xmlBody = goxml.XmlDecode(string(data))
}
if c.config.logDebug {
if c.gormConfig.debug {
log.Printf("[golog.GormMiddleware.len(jsonBody)] %v\n", len(jsonBody))
}
if err != nil {
if c.config.logDebug {
if c.gormConfig.debug {
log.Printf("[golog.GormMiddleware.json.Unmarshal] %s %s\n", jsonBody, err)
}
dataJson = false
@ -132,7 +129,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
}
}
if c.config.logDebug {
if c.gormConfig.debug {
log.Printf("[golog.GormMiddleware.xmlBody] %s\n", xmlBody)
log.Printf("[golog.GormMiddleware.jsonBody] %s\n", jsonBody)
}
@ -170,7 +167,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
host = "https://" + ginCtx.Request.Host
}
if dataJson {
if c.config.logDebug {
if c.gormConfig.debug {
log.Printf("[golog.GormMiddleware.gormRecord.json.request_body] %s\n", jsonBody)
log.Printf("[golog.GormMiddleware.gormRecord.json.request_body] %s\n", gojson.JsonEncodeNoError(jsonBody))
log.Printf("[golog.GormMiddleware.gormRecord.json.request_body] %s\n", datatypes.JSON(gojson.JsonEncodeNoError(jsonBody)))
@ -200,13 +197,12 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
CostTime: endTime - startTime, //【系统】花费时间
})
if err != nil {
if c.config.logDebug {
c.logClient.Errorf(ctx, "[log.gormRecord] %s", err.Error())
if c.gormConfig.debug {
log.Printf("[golog.GormMiddleware.gormRecord.json] %s\n", err)
}
}
} else {
if c.config.logDebug {
if c.gormConfig.debug {
log.Printf("[golog.GormMiddleware.gormRecord.xml.request_body] %s\n", xmlBody)
log.Printf("[golog.GormMiddleware.gormRecord.xml.request_body] %s\n", gojson.JsonEncodeNoError(xmlBody))
log.Printf("[golog.GormMiddleware.gormRecord.xml.request_body] %s\n", datatypes.JSON(gojson.JsonEncodeNoError(xmlBody)))
@ -236,8 +232,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
CostTime: endTime - startTime, //【系统】花费时间
})
if err != nil {
if c.config.logDebug {
c.logClient.Errorf(ctx, "[log.gormRecord] %s", err.Error())
if c.gormConfig.debug {
log.Printf("[golog.GormMiddleware.gormRecord.xml] %s\n", err)
}
}

@ -2,7 +2,6 @@ package golog
import (
"bytes"
"context"
"encoding/json"
"github.com/gin-gonic/gin"
"go.dtapp.net/dorm"
@ -106,8 +105,6 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
go func() {
var ctx = gotrace_id.SetGinTraceIdContext(context.Background(), ginCtx)
var dataJson = true
// 解析请求内容
@ -202,7 +199,6 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
})
if err != nil {
if c.mongoConfig.debug {
c.logClient.Errorf(ctx, "[log.mongoRecord] %s", err.Error())
log.Printf("[golog.MongoMiddleware.mongoRecord.json] %s\n", err)
}
}
@ -236,7 +232,6 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
})
if err != nil {
if c.mongoConfig.debug {
c.logClient.Errorf(ctx, "[log.mongoRecord] %s", err.Error())
log.Printf("[golog.MongoMiddleware.mongoRecord.xml] %s\n", err)
}
}

@ -55,7 +55,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.6.6 // indirect
github.com/pelletier/go-toml/v2 v2.0.3 // indirect
github.com/pelletier/go-toml/v2 v2.0.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda // indirect
github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f // indirect
@ -83,7 +83,7 @@ require (
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c // indirect
golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde // indirect
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect

@ -383,8 +383,8 @@ github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml/v2 v2.0.3 h1:h9JoA60e1dVEOpp0PFwJSmt1Htu057NUq9/bUwaO61s=
github.com/pelletier/go-toml/v2 v2.0.3/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas=
github.com/pelletier/go-toml/v2 v2.0.4 h1:MHHO+ZUPwPZQ6BmnnT81iQg5cuurp78CRH7rNsguSMk=
github.com/pelletier/go-toml/v2 v2.0.4/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas=
github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9iaPbIdPPGyKcA8hKdoy6hAWba7Yac=
github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
@ -667,8 +667,8 @@ golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 h1:TyKJRhyo17yWxOMCTHKWrc5rddHORMlnZ/j57umaUd8=
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 h1:UiNENfZ8gDvpiWw7IpOMQ27spWmThO1RwwdQVbJahJM=
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

Loading…
Cancel
Save