package dao import ( "context" "os" "time" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func get() (db *mongo.Database, err error) { ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() uri := "mongodb://" + os.Getenv("MONGO_USER") + ":" + os.Getenv("MONGO_PWD") + "@" + os.Getenv("MONGO_CLUSTER") client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri)) if err != nil { return nil, err } return client.Database(os.Getenv("MONGO_DB")), nil } func disconnect(client *mongo.Client) { if err := client.Disconnect(context.TODO()); err != nil { panic(err) } }