From a9ae30ac488c0b0dc5bde387b93ba8cfc5a6f482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Thu, 8 Sep 2022 14:26:54 +0800 Subject: [PATCH] - add api ip --- api.go | 9 +++++++++ api_gorm.go | 3 +++ api_mongo.go | 3 +++ const.go | 2 +- 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/api.go b/api.go index ff05c17..d635824 100644 --- a/api.go +++ b/api.go @@ -16,6 +16,7 @@ type ApiClient struct { mongoClient *dorm.MongoClient // 数据库驱动 zapLog *ZapLog // 日志服务 logDebug bool // 日志开关 + currentIp string // 当前ip gormConfig struct { tableName string // 表名 insideIp string // 内网ip @@ -50,6 +51,7 @@ type ApiClientConfig struct { MongoClientFun apiMongoClientFun // 日志配置 Debug bool // 日志开关 ZapLog *ZapLog // 日志服务 + CurrentIp string // 当前ip } // NewApiClient 创建接口实例化 @@ -65,6 +67,13 @@ func NewApiClient(config *ApiClientConfig) (*ApiClient, error) { c.logDebug = config.Debug + if config.CurrentIp == "" { + config.CurrentIp = goip.GetOutsideIp(ctx) + } + if config.CurrentIp != "" && config.CurrentIp != "0.0.0.0" { + c.currentIp = config.CurrentIp + } + gormClient, gormTableName := config.GormClientFun() mongoClient, mongoDatabaseName, mongoCollectionName := config.MongoClientFun() diff --git a/api_gorm.go b/api_gorm.go index 574e55f..c988129 100644 --- a/api_gorm.go +++ b/api_gorm.go @@ -84,6 +84,7 @@ type apiPostgresqlLog struct { RequestMethod string `gorm:"index;comment:【请求】方式" json:"request_method,omitempty"` //【请求】方式 RequestParams string `gorm:"comment:【请求】参数" json:"request_params,omitempty"` //【请求】参数 RequestHeader string `gorm:"comment:【请求】头部" json:"request_header,omitempty"` //【请求】头部 + RequestIp string `gorm:"index;comment:【请求】请求Ip" json:"request_ip,omitempty"` //【请求】请求Ip ResponseHeader string `gorm:"comment:【返回】头部" json:"response_header,omitempty"` //【返回】头部 ResponseStatusCode int `gorm:"index;comment:【返回】状态码" json:"response_status_code,omitempty"` //【返回】状态码 ResponseBody string `gorm:"comment:【返回】数据" json:"response_content,omitempty"` //【返回】数据 @@ -108,6 +109,8 @@ func (c *ApiClient) gormRecord(ctx context.Context, postgresqlLog apiPostgresqlL postgresqlLog.TraceId = gotrace_id.GetTraceIdContext(ctx) + postgresqlLog.RequestIp = c.currentIp + err = c.gormClient.Db.Table(c.gormConfig.tableName).Create(&postgresqlLog).Error if err != nil { c.zapLog.WithTraceId(ctx).Sugar().Errorf("[golog.api.gormRecord]:%s", err) diff --git a/api_mongo.go b/api_mongo.go index 90e5f7c..7132512 100644 --- a/api_mongo.go +++ b/api_mongo.go @@ -130,6 +130,7 @@ type apiMongolLog struct { RequestMethod string `json:"request_method,omitempty" bson:"request_method,omitempty"` //【请求】方式 RequestParams interface{} `json:"request_params,omitempty" bson:"request_params,omitempty"` //【请求】参数 RequestHeader interface{} `json:"request_header,omitempty" bson:"request_header,omitempty"` //【请求】头部 + RequestIp string `json:"request_ip,omitempty" bson:"request_ip,omitempty"` //【请求】请求Ip ResponseHeader interface{} `json:"response_header,omitempty" bson:"response_header,omitempty"` //【返回】头部 ResponseStatusCode int `json:"response_status_code,omitempty" bson:"response_status_code,omitempty"` //【返回】状态码 ResponseBody interface{} `json:"response_body,omitempty" bson:"response_body,omitempty"` //【返回】内容 @@ -152,6 +153,8 @@ func (c *ApiClient) mongoRecord(ctx context.Context, mongoLog apiMongolLog) (err mongoLog.LogId = primitive.NewObjectID() + mongoLog.RequestIp = c.currentIp + _, err = c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).InsertOne(mongoLog) if err != nil { c.zapLog.WithTraceId(ctx).Sugar().Errorf("[golog.api.mongoRecord]:%s", err) diff --git a/const.go b/const.go index 7a1b440..271637a 100644 --- a/const.go +++ b/const.go @@ -1,5 +1,5 @@ package golog const ( - Version = "1.0.69" + Version = "1.0.70" )