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 (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"mongo.games.com/goserver/core/logger"
|
"mongo.games.com/goserver/core/logger"
|
||||||
"mongo.games.com/goserver/core/viperx"
|
"mongo.games.com/goserver/core/viperx"
|
||||||
|
@ -46,7 +47,12 @@ func RegisterConfigEncryptor(h viperx.ConfigFileEncryptorHook) {
|
||||||
|
|
||||||
// LoadPackages 加载功能包
|
// LoadPackages 加载功能包
|
||||||
func LoadPackages(configFile string) {
|
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 err error
|
||||||
var notFoundConfig []string
|
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
|
package mongox
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -13,7 +12,7 @@ import (
|
||||||
type DatabaseType string
|
type DatabaseType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Global = "global"
|
KeyGlobal = "global"
|
||||||
|
|
||||||
DatabaseUser DatabaseType = "user"
|
DatabaseUser DatabaseType = "user"
|
||||||
DatabaseLog DatabaseType = "log"
|
DatabaseLog DatabaseType = "log"
|
||||||
|
@ -27,7 +26,7 @@ func GetClient() (*mongo.Client, error) {
|
||||||
return nil, NotInitError
|
return nil, NotInitError
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := _manager.GetCollection(Global, string(DatabaseLog), "empty")
|
c, err := _manager.GetCollection(KeyGlobal, string(DatabaseLog), "empty")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -61,7 +60,7 @@ func GetGlobalDatabase(database DatabaseType) (*Database, error) {
|
||||||
return nil, NotInitError
|
return nil, NotInitError
|
||||||
}
|
}
|
||||||
|
|
||||||
return _manager.GetDatabase(Global, string(database))
|
return _manager.GetDatabase(KeyGlobal, string(database))
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGlobalUserDatabase() (*Database, error) {
|
func GetGlobalUserDatabase() (*Database, error) {
|
||||||
|
@ -84,7 +83,7 @@ func GetGlobalCollection(database DatabaseType, collection string) (*Collection,
|
||||||
return nil, NotInitError
|
return nil, NotInitError
|
||||||
}
|
}
|
||||||
|
|
||||||
return _manager.GetCollection(Global, string(database), collection)
|
return _manager.GetCollection(KeyGlobal, string(database), collection)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGlobalUserCollection(collection string) (*Collection, error) {
|
func GetGlobalUserCollection(collection string) (*Collection, error) {
|
||||||
|
@ -124,8 +123,8 @@ type ICollectionName interface {
|
||||||
CollectionName() string
|
CollectionName() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCollectionName 获取文档名
|
// GetTableName 获取文档名
|
||||||
func GetCollectionName(model any) string {
|
func GetTableName(model any) string {
|
||||||
if m, ok := model.(ICollectionName); ok {
|
if m, ok := model.(ICollectionName); ok {
|
||||||
return m.CollectionName()
|
return m.CollectionName()
|
||||||
}
|
}
|
||||||
|
@ -141,37 +140,17 @@ func GetCollectionName(model any) string {
|
||||||
return strings.ToLower(t.Name())
|
return strings.ToLower(t.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
// IDatabaseName 数据库名称接口
|
// GetCollectionDao 获取文档操作接口
|
||||||
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 获取文档操作接口
|
|
||||||
// key: 平台id 或 KeyGlobal
|
// key: 平台id 或 KeyGlobal
|
||||||
// database: 数据库类型 DatabaseType
|
// database: 数据库类型 DatabaseType
|
||||||
// f: 文档接口创建函数; 结合 tools/mongoctl 使用
|
// f: 文档接口创建函数; 结合 tools/mongoctl 生成
|
||||||
func GetDao[T, M any](key string, f func(database *mongo.Database, c *mongo.Collection) (T, M)) (T, error) {
|
func GetCollectionDao[T any](key string, database DatabaseType, model any, f func(database *mongo.Database, c *mongo.Collection) T) (T, error) {
|
||||||
var z T
|
collectionName := GetTableName(model)
|
||||||
t, m := f(nil, nil)
|
c, err := GetCollection(key, database, collectionName)
|
||||||
databaseName, err := GetDatabaseName(m)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Errorf("GetDao error: %v", err)
|
var z T
|
||||||
|
logger.Logger.Errorf("GetCollectionDao key:%v database:%v model:%v error: %v", key, database, collectionName, err)
|
||||||
return z, err
|
return z, err
|
||||||
}
|
}
|
||||||
collectionName := GetCollectionName(m)
|
return f(c.Database.Database, c.Collection), nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,9 +151,6 @@ func (g *generator) makeModelExternalDao(m *model) {
|
||||||
replaces[varDaoPrefixNameKey] = m.daoPrefixName
|
replaces[varDaoPrefixNameKey] = m.daoPrefixName
|
||||||
replaces[varDaoPackageNameKey] = m.daoPkgName
|
replaces[varDaoPackageNameKey] = m.daoPkgName
|
||||||
replaces[varDaoPackagePathKey] = m.daoPkgPath
|
replaces[varDaoPackagePathKey] = m.daoPkgPath
|
||||||
replaces[varModelPackagePathKey] = m.modelPkgPath
|
|
||||||
replaces[varModelPackageNameKey] = m.modelPkgName
|
|
||||||
replaces[varModelClassNameKey] = m.modelClassName
|
|
||||||
|
|
||||||
err = doWrite(file, template.ExternalTemplate, replaces)
|
err = doWrite(file, template.ExternalTemplate, replaces)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -4,22 +4,8 @@ const ExternalTemplate = `
|
||||||
package ${VarDaoPackageName}
|
package ${VarDaoPackageName}
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"mongo.games.com/goserver/core/logger"
|
|
||||||
"mongo.games.com/goserver/core/mongox"
|
|
||||||
|
|
||||||
"${VarDaoPackagePath}/internal"
|
"${VarDaoPackagePath}/internal"
|
||||||
${VarModelPackageName} "${VarModelPackagePath}"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
_ = context.Background()
|
|
||||||
_ = logger.Logger
|
|
||||||
_ = bson.M{}
|
|
||||||
_ = mongo.Database{}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ${VarDaoPrefixName}Columns = internal.${VarDaoPrefixName}Columns
|
type ${VarDaoPrefixName}Columns = internal.${VarDaoPrefixName}Columns
|
||||||
|
@ -28,28 +14,14 @@ type ${VarDaoClassName} struct {
|
||||||
*internal.${VarDaoClassName}
|
*internal.${VarDaoClassName}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Get${VarDaoClassName}(key string) (*${VarDaoClassName}, error) {
|
func New${VarDaoClassName}(db *mongo.Database, c *mongo.Collection) *${VarDaoClassName} {
|
||||||
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}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
v := internal.New${VarDaoClassName}(nil)
|
v := internal.New${VarDaoClassName}(nil)
|
||||||
v.Database = db
|
v.Database = db
|
||||||
v.Collection = c
|
v.Collection = c
|
||||||
|
panic("创建索引")
|
||||||
//todo: 创建索引,删除代码
|
|
||||||
m :=&${VarModelPackageName}.${VarModelClassName}{}
|
|
||||||
name := mongox.GetCollectionName(m)
|
|
||||||
panic(fmt.Sprintf("创建索引 %s", name))
|
|
||||||
//c.Indexes().CreateOne()
|
//c.Indexes().CreateOne()
|
||||||
//c.Indexes().CreateMany()
|
//c.Indexes().CreateMany()
|
||||||
//todo: 创建索引,删除代码
|
return &${VarDaoClassName}{${VarDaoClassName}: v}
|
||||||
|
|
||||||
return &${VarDaoClassName}{${VarDaoClassName}: v}, &${VarModelPackageName}.${VarModelClassName}{}
|
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
@ -30,11 +29,12 @@ func RegisterConfigEncryptor(h ConfigFileEncryptorHook) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetViper 获取viper配置
|
// GetViper 获取viper配置
|
||||||
// name: 配置文件路径和名称,json、yaml、ini等
|
// name: 配置文件名,不带后缀
|
||||||
func GetViper(name string) *viper.Viper {
|
// filetype: 配置文件类型,如json、yaml、ini等
|
||||||
buf, err := ReadFile(name)
|
func GetViper(name, filetype string) *viper.Viper {
|
||||||
|
buf, err := ReadFile(name, filetype)
|
||||||
if err != nil {
|
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 {
|
if configFileEH != nil {
|
||||||
|
@ -44,20 +44,17 @@ func GetViper(name string) *viper.Viper {
|
||||||
}
|
}
|
||||||
|
|
||||||
vp := viper.New()
|
vp := viper.New()
|
||||||
ext := filepath.Ext(name)
|
vp.SetConfigName(name)
|
||||||
if len(ext) > 0 {
|
vp.SetConfigType(filetype)
|
||||||
ext = ext[1:] // 去掉点
|
|
||||||
}
|
|
||||||
vp.SetConfigType(ext)
|
|
||||||
if err = vp.ReadConfig(bytes.NewReader(buf)); err != nil {
|
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
|
return vp
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadFile(name string) ([]byte, error) {
|
func ReadFile(name, filetype string) ([]byte, error) {
|
||||||
for _, v := range paths {
|
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 {
|
if _, err := os.Stat(file); err == nil {
|
||||||
return os.ReadFile(file)
|
return os.ReadFile(file)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue