generated from root/miduo_server
35 lines
641 B
Go
35 lines
641 B
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
"github.com/gogf/gf/frame/g"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"time"
|
|
)
|
|
|
|
type Database struct {
|
|
Mongo *mongo.Client
|
|
}
|
|
|
|
var DB *Database
|
|
|
|
//初始化
|
|
func init() {
|
|
DB = &Database{
|
|
Mongo: SetConnect(),
|
|
}
|
|
}
|
|
|
|
// 连接设置
|
|
func SetConnect() *mongo.Client {
|
|
uri := g.Cfg().GetString("mongo.default")
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri).SetMaxPoolSize(20)) // 连接池
|
|
if err != nil {
|
|
g.Log().Error(err.Error())
|
|
}
|
|
return client
|
|
}
|