单机模式引入gorm数据库框架初步
This commit is contained in:
1 parent
3966606689
commit
348a7f460e
20 files changed
+558
-107
No files matched your search
@@ -0,0 +1,48 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type AccountGorm struct {
|
||||
OpenId string `gorm:"column:open_id;type:varchar(255);primaryKey"`
|
||||
Uid uint32 `gorm:"column:uid;type:bigint(20)"`
|
||||
IsForbid bool `gorm:"column:is_forbid;type:tinyint(1)"`
|
||||
ForbidEndTime uint32 `gorm:"column:forbid_end_time;type:bigint(20)"`
|
||||
}
|
||||
|
||||
func (a AccountGorm) TableName() string {
|
||||
return "account"
|
||||
}
|
||||
|
||||
func (d *Dao) InsertAccountGorm(account *Account) error {
|
||||
err := d.gormDb.Create(&AccountGorm{
|
||||
OpenId: account.OpenId,
|
||||
Uid: account.Uid,
|
||||
IsForbid: account.IsForbid,
|
||||
ForbidEndTime: account.ForbidEndTime,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) QueryAccountByOpenIdGorm(openId string) (*Account, error) {
|
||||
accountGorm := new(AccountGorm)
|
||||
err := d.gormDb.Where("open_id = ?", openId).First(accountGorm).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &Account{
|
||||
OpenId: accountGorm.OpenId,
|
||||
Uid: accountGorm.Uid,
|
||||
IsForbid: accountGorm.IsForbid,
|
||||
ForbidEndTime: accountGorm.ForbidEndTime,
|
||||
}, nil
|
||||
}
|
||||
+15
-16
@@ -2,8 +2,7 @@ package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"hk4e/pkg/logger"
|
||||
"errors"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
@@ -18,23 +17,23 @@ type Account struct {
|
||||
ForbidEndTime uint32 `bson:"forbid_end_time"`
|
||||
}
|
||||
|
||||
func (d *Dao) InsertAccount(account *Account) (primitive.ObjectID, error) {
|
||||
db := d.db.Collection("account")
|
||||
id, err := db.InsertOne(context.TODO(), account)
|
||||
if err != nil {
|
||||
return primitive.ObjectID{}, err
|
||||
} else {
|
||||
_id, ok := id.InsertedID.(primitive.ObjectID)
|
||||
if !ok {
|
||||
logger.Error("get insert id error")
|
||||
return primitive.ObjectID{}, nil
|
||||
}
|
||||
return _id, nil
|
||||
func (d *Dao) InsertAccount(account *Account) error {
|
||||
if d.mongo == nil {
|
||||
return d.InsertAccountGorm(account)
|
||||
}
|
||||
db := d.mongoDb.Collection("account")
|
||||
_, err := db.InsertOne(context.TODO(), account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dao) QueryAccountByOpenId(openId string) (*Account, error) {
|
||||
db := d.db.Collection("account")
|
||||
if d.mongo == nil {
|
||||
return d.QueryAccountByOpenIdGorm(openId)
|
||||
}
|
||||
db := d.mongoDb.Collection("account")
|
||||
result := db.FindOne(
|
||||
context.TODO(),
|
||||
bson.D{{"open_id", openId}},
|
||||
@@ -42,7 +41,7 @@ func (d *Dao) QueryAccountByOpenId(openId string) (*Account, error) {
|
||||
account := new(Account)
|
||||
err := result.Decode(account)
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
if errors.Is(err, mongo.ErrNoDocuments) {
|
||||
return nil, nil
|
||||
} else {
|
||||
return nil, err
|
||||
|
||||
+74
-16
@@ -2,19 +2,27 @@ package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hk4e/common/config"
|
||||
"hk4e/pkg/logger"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
gormlogger "gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
type Dao struct {
|
||||
mongo *mongo.Client
|
||||
db *mongo.Database
|
||||
mongoDb *mongo.Database
|
||||
gormDb *gorm.DB
|
||||
redis *redis.Client
|
||||
redisCluster *redis.ClusterClient
|
||||
}
|
||||
@@ -22,19 +30,65 @@ type Dao struct {
|
||||
func NewDao() (*Dao, error) {
|
||||
r := new(Dao)
|
||||
|
||||
clientOptions := options.Client().ApplyURI(config.GetConfig().Database.Url).SetMinPoolSize(10).SetMaxPoolSize(100)
|
||||
client, err := mongo.Connect(context.TODO(), clientOptions)
|
||||
if err != nil {
|
||||
logger.Error("mongo connect error: %v", err)
|
||||
return nil, err
|
||||
if strings.Contains(config.GetConfig().Database.Url, "mongodb://") {
|
||||
clientOptions := options.Client().ApplyURI(config.GetConfig().Database.Url)
|
||||
clientOptions = clientOptions.SetMinPoolSize(10)
|
||||
clientOptions = clientOptions.SetMaxPoolSize(100)
|
||||
client, err := mongo.Connect(context.TODO(), clientOptions)
|
||||
if err != nil {
|
||||
logger.Error("mongo connect error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
err = client.Ping(context.TODO(), readpref.Primary())
|
||||
if err != nil {
|
||||
logger.Error("mongo ping error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
r.mongo = client
|
||||
r.mongoDb = client.Database("gate_hk4e")
|
||||
} else {
|
||||
if strings.Contains(config.GetConfig().Database.Url, "mysql://") {
|
||||
dsn := strings.ReplaceAll(config.GetConfig().Database.Url, "mysql://", "")
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
Logger: gormlogger.Default.LogMode(gormlogger.Info),
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("gorm open error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
r.gormDb = db
|
||||
sqlDb, err := db.DB()
|
||||
if err != nil {
|
||||
logger.Error("sql db open error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
sqlDb.SetMaxIdleConns(10)
|
||||
sqlDb.SetMaxOpenConns(100)
|
||||
sqlDb.SetConnMaxLifetime(time.Hour)
|
||||
} else if strings.Contains(config.GetConfig().Database.Url, "sqlite://") {
|
||||
dsn := strings.ReplaceAll(config.GetConfig().Database.Url, "sqlite://", "")
|
||||
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{
|
||||
Logger: gormlogger.Default.LogMode(gormlogger.Info),
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("gorm open error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
r.gormDb = db
|
||||
} else {
|
||||
err := errors.New(fmt.Sprintf("not support db type, url: %v", config.GetConfig().Database.Url))
|
||||
logger.Error("%v", err)
|
||||
return nil, err
|
||||
}
|
||||
tableList := []any{new(AccountGorm)}
|
||||
for _, table := range tableList {
|
||||
err := r.gormDb.AutoMigrate(table)
|
||||
if err != nil {
|
||||
logger.Error("auto migrate error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
err = client.Ping(context.TODO(), readpref.Primary())
|
||||
if err != nil {
|
||||
logger.Error("mongo ping error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
r.mongo = client
|
||||
r.db = client.Database("gate_hk4e")
|
||||
|
||||
if !config.GetConfig().Hk4e.StandaloneModeEnable {
|
||||
r.redis = nil
|
||||
@@ -57,6 +111,7 @@ func NewDao() (*Dao, error) {
|
||||
MinIdleConns: 1,
|
||||
})
|
||||
}
|
||||
var err error = nil
|
||||
if r.redisCluster != nil {
|
||||
err = r.redisCluster.Ping(context.TODO()).Err()
|
||||
} else {
|
||||
@@ -72,12 +127,15 @@ func NewDao() (*Dao, error) {
|
||||
}
|
||||
|
||||
func (d *Dao) CloseDao() {
|
||||
err := d.mongo.Disconnect(context.TODO())
|
||||
if err != nil {
|
||||
logger.Error("mongo close error: %v", err)
|
||||
if d.mongo != nil {
|
||||
err := d.mongo.Disconnect(context.TODO())
|
||||
if err != nil {
|
||||
logger.Error("mongo close error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if !config.GetConfig().Hk4e.StandaloneModeEnable {
|
||||
var err error = nil
|
||||
if d.redisCluster != nil {
|
||||
err = d.redisCluster.Close()
|
||||
} else {
|
||||
|
||||
+1
-1
@@ -511,7 +511,7 @@ func (k *KcpConnManager) doGateLogin(req *proto.GetPlayerTokenReq, session *Sess
|
||||
IsForbid: false,
|
||||
ForbidEndTime: 0,
|
||||
}
|
||||
_, err = k.db.InsertAccount(account)
|
||||
err = k.db.InsertAccount(account)
|
||||
if err != nil {
|
||||
logger.Error("insert account error: %v, openId: %v", err, req.AccountUid)
|
||||
return k.loginFailRsp(0, proto.Retcode_RET_SVR_ERROR, false, 0)
|
||||
|
||||
Reference in new issue
Block a user