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/api.go

158 lines
3.7 KiB

2 years ago
package golog
import (
2 years ago
"context"
2 years ago
"errors"
"go.dtapp.net/dorm"
2 years ago
"go.dtapp.net/goip"
2 years ago
"go.dtapp.net/gorequest"
2 years ago
"os"
"runtime"
2 years ago
)
2 years ago
// ApiClient 接口
type ApiClient struct {
gormClient *dorm.GormClient // 数据库驱动
mongoClient *dorm.MongoClient // 数据库驱动
gormConfig struct {
2 years ago
tableName string // 表名
insideIp string // 内网ip
hostname string // 主机名
goVersion string // go版本
debug bool // 日志开关
2 years ago
}
mongoConfig struct {
databaseName string // 库名
collectionName string // 表名
insideIp string // 内网ip
hostname string // 主机名
goVersion string // go版本
debug bool // 日志开关
}
2 years ago
log struct {
gorm bool // 日志开关
mongo bool // 日志开关
}
2 years ago
}
2 years ago
// client 数据库服务
// string 表名
type apiGormClientFun func() (*dorm.GormClient, string)
2 years ago
// client 数据库服务
// string 库名
// string 表名
type apiMongoClientFun func() (*dorm.MongoClient, string, string)
// ApiClientConfig 接口实例配置
type ApiClientConfig struct {
GormClientFun apiGormClientFun // 日志配置
MongoClientFun apiMongoClientFun // 日志配置
Debug bool // 日志开关
}
// NewApiClient 创建接口实例化
// client 数据库服务
// tableName 表名
2 years ago
func NewApiClient(config *ApiClientConfig) (*ApiClient, error) {
var ctx = context.Background()
2 years ago
2 years ago
c := &ApiClient{}
2 years ago
2 years ago
gormClient, gormTableName := config.GormClientFun()
mongoClient, mongoDatabaseName, mongoCollectionName := config.MongoClientFun()
2 years ago
2 years ago
if (gormClient == nil || gormClient.Db == nil) || (mongoClient == nil || mongoClient.Db == nil) {
return nil, errors.New("没有设置驱动")
2 years ago
}
2 years ago
2 years ago
hostname, _ := os.Hostname()
2 years ago
if gormClient != nil || gormClient.Db != nil {
2 years ago
2 years ago
c.gormClient = gormClient
2 years ago
if gormTableName == "" {
return nil, errors.New("没有设置表名")
}
c.gormConfig.tableName = gormTableName
2 years ago
c.gormConfig.debug = config.Debug
2 years ago
2 years ago
err := c.gormClient.Db.Table(c.gormConfig.tableName).AutoMigrate(&apiPostgresqlLog{})
if err != nil {
return nil, errors.New("创建表失败:" + err.Error())
}
2 years ago
2 years ago
c.gormConfig.hostname = hostname
c.gormConfig.insideIp = goip.GetInsideIp(ctx)
c.gormConfig.goVersion = runtime.Version()
2 years ago
c.log.gorm = true
2 years ago
}
2 years ago
2 years ago
if mongoClient != nil || mongoClient.Db != nil {
2 years ago
c.mongoClient = mongoClient
2 years ago
if mongoDatabaseName == "" {
return nil, errors.New("没有设置库名")
}
c.mongoConfig.databaseName = mongoDatabaseName
2 years ago
if mongoCollectionName == "" {
return nil, errors.New("没有设置表名")
}
c.mongoConfig.collectionName = mongoCollectionName
2 years ago
c.mongoConfig.debug = config.Debug
2 years ago
c.mongoConfig.hostname = hostname
c.mongoConfig.insideIp = goip.GetInsideIp(ctx)
c.mongoConfig.goVersion = runtime.Version()
2 years ago
c.log.mongo = true
// 创建时间序列集合
c.mongoCreateCollection()
// 创建索引
c.mongoCreateIndexes()
}
2 years ago
return c, nil
}
2 years ago
// Middleware 中间件
func (c *ApiClient) Middleware(ctx context.Context, request gorequest.Response, sdkVersion string) {
if c.log.gorm {
2 years ago
c.GormMiddleware(ctx, request, sdkVersion)
2 years ago
}
if c.log.mongo {
2 years ago
c.MongoMiddleware(ctx, request, sdkVersion)
2 years ago
}
}
2 years ago
// MiddlewareXml 中间件
func (c *ApiClient) MiddlewareXml(ctx context.Context, request gorequest.Response, sdkVersion string) {
if c.log.gorm {
2 years ago
c.GormMiddlewareXml(ctx, request, sdkVersion)
2 years ago
}
if c.log.mongo {
2 years ago
c.MongoMiddlewareXml(ctx, request, sdkVersion)
2 years ago
}
}
2 years ago
// MiddlewareCustom 中间件
func (c *ApiClient) MiddlewareCustom(ctx context.Context, api string, request gorequest.Response, sdkVersion string) {
if c.log.gorm {
2 years ago
c.GormMiddlewareCustom(ctx, api, request, sdkVersion)
2 years ago
}
if c.log.mongo {
2 years ago
c.MongoMiddlewareCustom(ctx, api, request, sdkVersion)
2 years ago
}
}