You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
golog/gin.go

138 lines
3.2 KiB

2 years ago
package golog
import (
2 years ago
"bytes"
2 years ago
"context"
2 years ago
"encoding/json"
"errors"
"github.com/gin-gonic/gin"
"go.dtapp.net/dorm"
2 years ago
"go.dtapp.net/goip"
"os"
"runtime"
2 years ago
)
2 years ago
type GinClientConfig struct {
GormClient *dorm.GormClient // 数据库驱动
IpService *goip.Client // ip服务
TableName string // 表名
2 years ago
LogClient *ZapLog // 日志驱动
2 years ago
LogDebug bool // 日志开关
}
2 years ago
// GinClient 框架
type GinClient struct {
2 years ago
gormClient *dorm.GormClient // 数据库驱动
mongoClient *dorm.MongoClient // 数据库驱动
ipService *goip.Client // ip服务
logClient *ZapLog // 日志驱动
config struct {
2 years ago
tableName string // 表名
insideIp string // 内网ip
hostname string // 主机名
goVersion string // go版本
logDebug bool // 日志开关
}
2 years ago
mongoConfig struct {
databaseName string // 库名
collectionName string // 表名
insideIp string // 内网ip
hostname string // 主机名
goVersion string // go版本
debug bool // 日志开关
}
2 years ago
}
2 years ago
// NewGinClient 创建框架实例化
2 years ago
func NewGinClient(config *GinClientConfig) (*GinClient, error) {
2 years ago
2 years ago
c := &GinClient{}
2 years ago
c.gormClient = config.GormClient
c.config.tableName = config.TableName
2 years ago
2 years ago
c.logClient = config.LogClient
c.config.logDebug = config.LogDebug
2 years ago
2 years ago
c.ipService = config.IpService
2 years ago
2 years ago
if c.gormClient.Db == nil {
return nil, errors.New("没有设置驱动")
}
2 years ago
2 years ago
if c.config.tableName == "" {
return nil, errors.New("没有设置表名")
}
2 years ago
2 years ago
err := c.gormClient.Db.Table(c.config.tableName).AutoMigrate(&ginPostgresqlLog{})
if err != nil {
return nil, errors.New("创建表失败:" + err.Error())
2 years ago
}
2 years ago
2 years ago
hostname, _ := os.Hostname()
2 years ago
c.config.hostname = hostname
2 years ago
c.config.insideIp = goip.GetInsideIp(context.Background())
2 years ago
c.config.goVersion = runtime.Version()
2 years ago
2 years ago
return c, nil
2 years ago
}
2 years ago
// NewGinMongoClient 创建框架实例化
// mongoClient 数据库服务
// ipService ip服务
// databaseName 库名
// collectionName 表名
func NewGinMongoClient(mongoClient *dorm.MongoClient, ipService *goip.Client, databaseName string, collectionName string, debug bool) (*GinClient, error) {
c := &GinClient{}
if mongoClient.Db == nil {
return nil, errors.New("没有设置驱动")
}
c.mongoClient = mongoClient
if databaseName == "" {
return nil, errors.New("没有设置库名")
}
c.mongoConfig.databaseName = databaseName
if collectionName == "" {
return nil, errors.New("没有设置表名")
}
c.mongoConfig.collectionName = collectionName
c.ipService = ipService
c.mongoConfig.debug = debug
hostname, _ := os.Hostname()
c.mongoConfig.hostname = hostname
c.mongoConfig.insideIp = goip.GetInsideIp(context.Background())
c.mongoConfig.goVersion = runtime.Version()
return c, nil
}
2 years ago
type bodyLogWriter struct {
gin.ResponseWriter
body *bytes.Buffer
2 years ago
}
2 years ago
func (w bodyLogWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
2 years ago
}
2 years ago
func (w bodyLogWriter) WriteString(s string) (int, error) {
w.body.WriteString(s)
return w.ResponseWriter.WriteString(s)
2 years ago
}
2 years ago
func (c *GinClient) jsonUnmarshal(data string) (result interface{}) {
_ = json.Unmarshal([]byte(data), &result)
return
}