- 优化
continuous-integration/drone/push Build is passing Details

master
李光春 2 years ago
parent 0b7a7a28bb
commit 054024fa98

@ -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)
}

@ -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()
}

@ -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
}

@ -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)
}

Loading…
Cancel
Save