123456789101112131415161718192021222324252627282930313233343536373839 |
- package mgodb
- import (
- "context"
- "eta/eta_forum_admin/utils"
- "fmt"
- "go.mongodb.org/mongo-driver/mongo"
- "go.mongodb.org/mongo-driver/mongo/options"
- "go.mongodb.org/mongo-driver/mongo/readpref"
- "time"
- )
- var MgoClient *mongo.Client
- func init() {
- var err error
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- credential := options.Credential{
- AuthMechanism: utils.MONGODB_CREDENTIAL,
- Username: "hzeta",
- Password: "hzeta2023",
- }
- clientOpts := options.Client().ApplyURI(utils.MONGODB_URL).SetAuth(credential)
- clientOpts.SetMinPoolSize(0)
- clientOpts.SetMaxPoolSize(100)
- clientOpts.SetConnectTimeout(10 * time.Second)
- MgoClient, err = mongo.Connect(context.TODO(), clientOpts)
- if err != nil {
- panic(err)
- }
- if err = MgoClient.Ping(ctx, readpref.Primary()); err != nil {
- panic(err)
- }
- fmt.Println("Connected to MongoDB!")
- }
|