Compare commits
No commits in common. "9f25186d75dc3824c532a21d0a377f7444f1c539" and "108691267db2852ea2d1e6815470c9baccf42493" have entirely different histories.
9f25186d75
...
108691267d
|
@ -1,18 +0,0 @@
|
|||
stages:
|
||||
- sync
|
||||
|
||||
variables:
|
||||
Path: "$GOPATH/mongo.games.com/game" # 项目相对于GOPATH的路径
|
||||
|
||||
default:
|
||||
tags:
|
||||
- gitlab
|
||||
|
||||
sync_job:
|
||||
stage: sync
|
||||
only:
|
||||
- main
|
||||
script:
|
||||
- git checkout $CI_COMMIT_REF_NAME
|
||||
- git pull origin $CI_COMMIT_REF_NAME
|
||||
- rsync -rvc --no-perms --delete ./* $PATH
|
|
@ -2,6 +2,7 @@ package core
|
|||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"mongo.games.com/goserver/core/logger"
|
||||
"mongo.games.com/goserver/core/viperx"
|
||||
|
@ -46,7 +47,12 @@ func RegisterConfigEncryptor(h viperx.ConfigFileEncryptorHook) {
|
|||
|
||||
// LoadPackages 加载功能包
|
||||
func LoadPackages(configFile string) {
|
||||
vp := viperx.GetViper(configFile)
|
||||
val := strings.Split(configFile, ".")
|
||||
if len(val) != 2 {
|
||||
panic("config file name error")
|
||||
}
|
||||
|
||||
vp := viperx.GetViper(val[0], val[1])
|
||||
|
||||
var err error
|
||||
var notFoundConfig []string
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
package mongox
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"mongo.games.com/goserver/core"
|
||||
"mongo.games.com/goserver/core/viperx"
|
||||
)
|
||||
|
||||
var config = Configuration{}
|
||||
|
||||
type Configuration struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func (c *Configuration) Name() string {
|
||||
return "mongox"
|
||||
}
|
||||
|
||||
func (c *Configuration) Init() error {
|
||||
if c.Path == "" {
|
||||
c.Path = "mongo.yaml"
|
||||
}
|
||||
|
||||
vp := viperx.GetViper(c.Path)
|
||||
|
||||
cfg := &Config{}
|
||||
if err := vp.Unmarshal(cfg); err != nil {
|
||||
panic(fmt.Sprintf("mongox init error: %v", err))
|
||||
}
|
||||
|
||||
Init(cfg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Configuration) Close() error {
|
||||
Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
core.RegistePackage(&config)
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
package mongox
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
|
@ -13,7 +12,7 @@ import (
|
|||
type DatabaseType string
|
||||
|
||||
const (
|
||||
Global = "global"
|
||||
KeyGlobal = "global"
|
||||
|
||||
DatabaseUser DatabaseType = "user"
|
||||
DatabaseLog DatabaseType = "log"
|
||||
|
@ -27,7 +26,7 @@ func GetClient() (*mongo.Client, error) {
|
|||
return nil, NotInitError
|
||||
}
|
||||
|
||||
c, err := _manager.GetCollection(Global, string(DatabaseLog), "empty")
|
||||
c, err := _manager.GetCollection(KeyGlobal, string(DatabaseLog), "empty")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -61,7 +60,7 @@ func GetGlobalDatabase(database DatabaseType) (*Database, error) {
|
|||
return nil, NotInitError
|
||||
}
|
||||
|
||||
return _manager.GetDatabase(Global, string(database))
|
||||
return _manager.GetDatabase(KeyGlobal, string(database))
|
||||
}
|
||||
|
||||
func GetGlobalUserDatabase() (*Database, error) {
|
||||
|
@ -84,7 +83,7 @@ func GetGlobalCollection(database DatabaseType, collection string) (*Collection,
|
|||
return nil, NotInitError
|
||||
}
|
||||
|
||||
return _manager.GetCollection(Global, string(database), collection)
|
||||
return _manager.GetCollection(KeyGlobal, string(database), collection)
|
||||
}
|
||||
|
||||
func GetGlobalUserCollection(collection string) (*Collection, error) {
|
||||
|
@ -124,8 +123,8 @@ type ICollectionName interface {
|
|||
CollectionName() string
|
||||
}
|
||||
|
||||
// GetCollectionName 获取文档名
|
||||
func GetCollectionName(model any) string {
|
||||
// GetTableName 获取文档名
|
||||
func GetTableName(model any) string {
|
||||
if m, ok := model.(ICollectionName); ok {
|
||||
return m.CollectionName()
|
||||
}
|
||||
|
@ -141,37 +140,17 @@ func GetCollectionName(model any) string {
|
|||
return strings.ToLower(t.Name())
|
||||
}
|
||||
|
||||
// IDatabaseName 数据库名称接口
|
||||
type IDatabaseName interface {
|
||||
DatabaseName() string
|
||||
}
|
||||
|
||||
func GetDatabaseName(model any) (string, error) {
|
||||
if m, ok := model.(IDatabaseName); ok {
|
||||
return m.DatabaseName(), nil
|
||||
}
|
||||
|
||||
return "", errors.New("not set database name")
|
||||
}
|
||||
|
||||
// GetDao 获取文档操作接口
|
||||
// GetCollectionDao 获取文档操作接口
|
||||
// key: 平台id 或 KeyGlobal
|
||||
// database: 数据库类型 DatabaseType
|
||||
// f: 文档接口创建函数; 结合 tools/mongoctl 使用
|
||||
func GetDao[T, M any](key string, f func(database *mongo.Database, c *mongo.Collection) (T, M)) (T, error) {
|
||||
// f: 文档接口创建函数; 结合 tools/mongoctl 生成
|
||||
func GetCollectionDao[T any](key string, database DatabaseType, model any, f func(database *mongo.Database, c *mongo.Collection) T) (T, error) {
|
||||
collectionName := GetTableName(model)
|
||||
c, err := GetCollection(key, database, collectionName)
|
||||
if err != nil {
|
||||
var z T
|
||||
t, m := f(nil, nil)
|
||||
databaseName, err := GetDatabaseName(m)
|
||||
if err != nil {
|
||||
logger.Logger.Errorf("GetDao error: %v", err)
|
||||
logger.Logger.Errorf("GetCollectionDao key:%v database:%v model:%v error: %v", key, database, collectionName, err)
|
||||
return z, err
|
||||
}
|
||||
collectionName := GetCollectionName(m)
|
||||
c, err := GetCollection(key, DatabaseType(databaseName), collectionName)
|
||||
if err != nil {
|
||||
logger.Logger.Errorf("GetDao key:%v database:%v model:%v error: %v", key, databaseName, collectionName, err)
|
||||
return z, err
|
||||
}
|
||||
t, _ = f(c.Database.Database, c.Collection)
|
||||
return t, nil
|
||||
return f(c.Database.Database, c.Collection), nil
|
||||
}
|
||||
|
|
|
@ -151,9 +151,6 @@ func (g *generator) makeModelExternalDao(m *model) {
|
|||
replaces[varDaoPrefixNameKey] = m.daoPrefixName
|
||||
replaces[varDaoPackageNameKey] = m.daoPkgName
|
||||
replaces[varDaoPackagePathKey] = m.daoPkgPath
|
||||
replaces[varModelPackagePathKey] = m.modelPkgPath
|
||||
replaces[varModelPackageNameKey] = m.modelPkgName
|
||||
replaces[varModelClassNameKey] = m.modelClassName
|
||||
|
||||
err = doWrite(file, template.ExternalTemplate, replaces)
|
||||
if err != nil {
|
||||
|
|
|
@ -4,22 +4,8 @@ const ExternalTemplate = `
|
|||
package ${VarDaoPackageName}
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"mongo.games.com/goserver/core/logger"
|
||||
"mongo.games.com/goserver/core/mongox"
|
||||
|
||||
"${VarDaoPackagePath}/internal"
|
||||
${VarModelPackageName} "${VarModelPackagePath}"
|
||||
)
|
||||
|
||||
var (
|
||||
_ = context.Background()
|
||||
_ = logger.Logger
|
||||
_ = bson.M{}
|
||||
_ = mongo.Database{}
|
||||
)
|
||||
|
||||
type ${VarDaoPrefixName}Columns = internal.${VarDaoPrefixName}Columns
|
||||
|
@ -28,28 +14,14 @@ type ${VarDaoClassName} struct {
|
|||
*internal.${VarDaoClassName}
|
||||
}
|
||||
|
||||
func Get${VarDaoClassName}(key string) (*${VarDaoClassName}, error) {
|
||||
return mongox.GetDao(key, New${VarDaoClassName})
|
||||
}
|
||||
|
||||
func New${VarDaoClassName}(db *mongo.Database, c *mongo.Collection) (*${VarDaoClassName}, any) {
|
||||
if db == nil || c == nil {
|
||||
return &${VarDaoClassName}{}, &${VarModelPackageName}.${VarModelClassName}{}
|
||||
}
|
||||
|
||||
func New${VarDaoClassName}(db *mongo.Database, c *mongo.Collection) *${VarDaoClassName} {
|
||||
v := internal.New${VarDaoClassName}(nil)
|
||||
v.Database = db
|
||||
v.Collection = c
|
||||
|
||||
//todo: 创建索引,删除代码
|
||||
m :=&${VarModelPackageName}.${VarModelClassName}{}
|
||||
name := mongox.GetCollectionName(m)
|
||||
panic(fmt.Sprintf("创建索引 %s", name))
|
||||
panic("创建索引")
|
||||
//c.Indexes().CreateOne()
|
||||
//c.Indexes().CreateMany()
|
||||
//todo: 创建索引,删除代码
|
||||
|
||||
return &${VarDaoClassName}{${VarDaoClassName}: v}, &${VarModelPackageName}.${VarModelClassName}{}
|
||||
return &${VarDaoClassName}{${VarDaoClassName}: v}
|
||||
}
|
||||
`
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
@ -30,11 +29,12 @@ func RegisterConfigEncryptor(h ConfigFileEncryptorHook) {
|
|||
}
|
||||
|
||||
// GetViper 获取viper配置
|
||||
// name: 配置文件路径和名称,json、yaml、ini等
|
||||
func GetViper(name string) *viper.Viper {
|
||||
buf, err := ReadFile(name)
|
||||
// name: 配置文件名,不带后缀
|
||||
// filetype: 配置文件类型,如json、yaml、ini等
|
||||
func GetViper(name, filetype string) *viper.Viper {
|
||||
buf, err := ReadFile(name, filetype)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Error while reading config file %s: %v", name, err))
|
||||
panic(fmt.Sprintf("Error while reading config file %s: %v", name+filetype, err))
|
||||
}
|
||||
|
||||
if configFileEH != nil {
|
||||
|
@ -44,20 +44,17 @@ func GetViper(name string) *viper.Viper {
|
|||
}
|
||||
|
||||
vp := viper.New()
|
||||
ext := filepath.Ext(name)
|
||||
if len(ext) > 0 {
|
||||
ext = ext[1:] // 去掉点
|
||||
}
|
||||
vp.SetConfigType(ext)
|
||||
vp.SetConfigName(name)
|
||||
vp.SetConfigType(filetype)
|
||||
if err = vp.ReadConfig(bytes.NewReader(buf)); err != nil {
|
||||
panic(fmt.Sprintf("Error while reading config file %s: %v", name, err))
|
||||
panic(fmt.Sprintf("Error while reading config file %s: %v", name+filetype, err))
|
||||
}
|
||||
return vp
|
||||
}
|
||||
|
||||
func ReadFile(name string) ([]byte, error) {
|
||||
func ReadFile(name, filetype string) ([]byte, error) {
|
||||
for _, v := range paths {
|
||||
file := filepath.Join(v, name)
|
||||
file := fmt.Sprintf("%s/%s.%s", v, name, filetype)
|
||||
if _, err := os.Stat(file); err == nil {
|
||||
return os.ReadFile(file)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue