mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-03-01 00:35:36 +08:00
优化架构
This commit is contained in:
34
dispatch/dao/dao.go
Normal file
34
dispatch/dao/dao.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"hk4e/common/config"
|
||||
"hk4e/logger"
|
||||
)
|
||||
|
||||
type Dao struct {
|
||||
client *mongo.Client
|
||||
db *mongo.Database
|
||||
}
|
||||
|
||||
func NewDao() (r *Dao) {
|
||||
r = new(Dao)
|
||||
clientOptions := options.Client().ApplyURI(config.CONF.Database.Url)
|
||||
client, err := mongo.Connect(context.TODO(), clientOptions)
|
||||
if err != nil {
|
||||
logger.LOG.Error("mongo connect error: %v", err)
|
||||
return nil
|
||||
}
|
||||
r.client = client
|
||||
r.db = client.Database("gate_hk4e")
|
||||
return r
|
||||
}
|
||||
|
||||
func (d *Dao) CloseDao() {
|
||||
err := d.client.Disconnect(context.TODO())
|
||||
if err != nil {
|
||||
logger.LOG.Error("mongo close error: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user