From 054024fa982903e340745e4968617b4e7ac07f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Thu, 19 May 2022 09:37:11 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.go | 42 ++++++++++++++++++++++++++++++++++++------ app.go | 48 ------------------------------------------------ client.go | 42 ------------------------------------------ gin.go | 42 ++++++++++++++++++++++++++++++++++++------ 4 files changed, 72 insertions(+), 102 deletions(-) delete mode 100644 app.go delete mode 100644 client.go diff --git a/api.go b/api.go index 0a8027c..a6c12a7 100644 --- a/api.go +++ b/api.go @@ -1,13 +1,22 @@ package golog import ( + "go.dtapp.net/goip" "gorm.io/datatypes" "gorm.io/gorm" "log" + "os" + "runtime" + "strings" ) -// 接口定义 -type api struct { +type ApiConfig struct { + Db *gorm.DB // 驱动 + TableName string // 表名 +} + +// Api 接口 +type Api struct { db *gorm.DB // pgsql数据库 tableName string // 日志表名 insideIp string // 内网ip @@ -15,6 +24,27 @@ type api struct { goVersion string // go版本 } +// NewApi 创建接口实例化 +func NewApi(config *ApiConfig) *Api { + app := &Api{} + if config.Db == nil { + panic("驱动不正常") + } + if config.TableName == "" { + panic("表名不能为空") + } + hostname, _ := os.Hostname() + + app.db = config.Db + app.tableName = config.TableName + app.hostname = hostname + app.insideIp = goip.GetInsideIp() + app.goVersion = strings.TrimPrefix(runtime.Version(), "go") + + app.AutoMigrate() + return app +} + // ApiPostgresqlLog 结构体 type ApiPostgresqlLog struct { LogId uint `gorm:"primaryKey" json:"log_id"` //【记录】编号 @@ -36,7 +66,7 @@ type ApiPostgresqlLog struct { } // AutoMigrate 自动迁移 -func (p *api) AutoMigrate() { +func (p *Api) AutoMigrate() { err := p.db.Table(p.tableName).AutoMigrate(&ApiPostgresqlLog{}) if err != nil { panic("创建表失败:" + err.Error()) @@ -44,7 +74,7 @@ func (p *api) AutoMigrate() { } // Record 记录日志 -func (p *api) Record(content ApiPostgresqlLog) *gorm.DB { +func (p *Api) Record(content ApiPostgresqlLog) *gorm.DB { content.SystemHostName = p.hostname if content.SystemInsideIp == "" { content.SystemInsideIp = p.insideIp @@ -52,12 +82,12 @@ func (p *api) Record(content ApiPostgresqlLog) *gorm.DB { content.GoVersion = p.goVersion resp := p.db.Table(p.tableName).Create(&content) if resp.RowsAffected == 0 { - log.Println("api:", resp.Error) + log.Println("Api:", resp.Error) } return resp } // Query 查询 -func (p *api) Query() *gorm.DB { +func (p *Api) Query() *gorm.DB { return p.db.Table(p.tableName) } diff --git a/app.go b/app.go deleted file mode 100644 index de5fe25..0000000 --- a/app.go +++ /dev/null @@ -1,48 +0,0 @@ -package golog - -import ( - "go.dtapp.net/goip" - "gorm.io/gorm" - "os" - "runtime" - "strings" -) - -type App struct { - Gin gin // 框架日志 - Api api // 接口日志 - Pgsql *gorm.DB // pgsql数据库 - TableName string // 日志表名 -} - -// InitClientApi 接口实例化 -func (a *App) InitClientApi() { - if a.Pgsql == nil { - panic("驱动不正常") - } - if a.TableName == "" { - panic("表名不能为空") - } - a.Api.db = a.Pgsql - a.Api.tableName = a.TableName - a.Api.hostname, _ = os.Hostname() - a.Api.insideIp = goip.GetInsideIp() - a.Api.goVersion = strings.TrimPrefix(runtime.Version(), "go") - a.Api.AutoMigrate() -} - -// InitClientGin 框架实例化 -func (a *App) InitClientGin() { - if a.Pgsql == nil { - panic("驱动不正常") - } - if a.TableName == "" { - panic("表名不能为空") - } - a.Gin.db = a.Pgsql - a.Gin.tableName = a.TableName - a.Gin.hostname, _ = os.Hostname() - a.Gin.insideIp = goip.GetInsideIp() - a.Gin.goVersion = strings.TrimPrefix(runtime.Version(), "go") - a.Gin.AutoMigrate() -} diff --git a/client.go b/client.go deleted file mode 100644 index 466f2b1..0000000 --- a/client.go +++ /dev/null @@ -1,42 +0,0 @@ -package golog - -import ( - "go.dtapp.net/goip" - "gorm.io/gorm" - "os" - "runtime" - "strings" -) - -type Client struct { - Gin gin // 框架日志 - Api api // 接口日志 -} - -// NewClientGin 创建框架实例化 -func NewClientGin(db *gorm.DB, tableName string) *Client { - if db == nil { - panic("驱动不正常") - } - if tableName == "" { - panic("表名不能为空") - } - hostname, _ := os.Hostname() - client := &Client{Gin: gin{db: db, tableName: tableName, hostname: hostname, insideIp: goip.GetInsideIp(), goVersion: strings.TrimPrefix(runtime.Version(), "go")}} - client.Gin.AutoMigrate() - return client -} - -// NewClientApi 创建接口实例化 -func NewClientApi(db *gorm.DB, tableName string) *Client { - if db == nil { - panic("驱动不正常") - } - if tableName == "" { - panic("表名不能为空") - } - hostname, _ := os.Hostname() - client := &Client{Api: api{db: db, tableName: tableName, hostname: hostname, insideIp: goip.GetInsideIp(), goVersion: strings.TrimPrefix(runtime.Version(), "go")}} - client.Api.AutoMigrate() - return client -} diff --git a/gin.go b/gin.go index 6f30ab3..f7d16ce 100644 --- a/gin.go +++ b/gin.go @@ -1,13 +1,22 @@ package golog import ( + "go.dtapp.net/goip" "gorm.io/datatypes" "gorm.io/gorm" "log" + "os" + "runtime" + "strings" ) -// 框架定义 -type gin struct { +type GinConfig struct { + Db *gorm.DB // 驱动 + TableName string // 表名 +} + +// Gin 框架 +type Gin struct { db *gorm.DB // pgsql数据库 tableName string // 日志表名 insideIp string // 内网ip @@ -15,6 +24,27 @@ type gin struct { goVersion string // go版本 } +// NewGin 创建框架实例化 +func NewGin(config *GinConfig) *Gin { + app := &Gin{} + if config.Db == nil { + panic("驱动不正常") + } + if config.TableName == "" { + panic("表名不能为空") + } + hostname, _ := os.Hostname() + + app.db = config.Db + app.tableName = config.TableName + app.hostname = hostname + app.insideIp = goip.GetInsideIp() + app.goVersion = strings.TrimPrefix(runtime.Version(), "go") + + app.AutoMigrate() + return app +} + // GinPostgresqlLog 结构体 type GinPostgresqlLog struct { LogId uint `gorm:"primaryKey" json:"log_id"` //【记录】编号 @@ -47,7 +77,7 @@ type GinPostgresqlLog struct { } // AutoMigrate 自动迁移 -func (p *gin) AutoMigrate() { +func (p *Gin) AutoMigrate() { err := p.db.Table(p.tableName).AutoMigrate(&GinPostgresqlLog{}) if err != nil { panic("创建表失败:" + err.Error()) @@ -55,7 +85,7 @@ func (p *gin) AutoMigrate() { } // Record 记录日志 -func (p *gin) Record(content GinPostgresqlLog) *gorm.DB { +func (p *Gin) Record(content GinPostgresqlLog) *gorm.DB { content.SystemHostName = p.hostname if content.SystemInsideIp == "" { content.SystemInsideIp = p.insideIp @@ -63,12 +93,12 @@ func (p *gin) Record(content GinPostgresqlLog) *gorm.DB { content.GoVersion = p.goVersion resp := p.db.Table(p.tableName).Create(&content) if resp.RowsAffected == 0 { - log.Println("gin:", resp.Error) + log.Println("Gin:", resp.Error) } return resp } // Query 查询 -func (p *gin) Query() *gorm.DB { +func (p *Gin) Query() *gorm.DB { return p.db.Table(p.tableName) }