Commit 3c9a1dc1 authored by luxq's avatar luxq

update package

parent cae0d7b2
package db
import (
"context"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func ConnectMongoDB(uri string, username, passwd string) (*mongo.Client, error) {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri).SetAuth(options.Credential{
Username: username,
Password: passwd,
}))
if err != nil {
return nil, err
}
return client, nil
}
......@@ -27,7 +27,7 @@ services:
volumes:
- type: volume
source: MONGO_DATA
target: /data/db
target: /data/operator
- type: volume
source: MONGO_CONFIG
target: /data/configdb
......
module mogo
module github.com/odysseus/mogo
go 1.21
......
package db
package operator
type DbTaskInfo struct {
ID string `bson:"_id,omitempty" json:"id"`
......
package db
package operator
import (
"context"
......
package db
package operator
import (
"context"
......
package db
package operator
import (
"context"
......@@ -8,6 +8,7 @@ import (
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"math/rand"
"mogo/types"
......@@ -25,6 +26,19 @@ var (
workerRunningCollection = "worker_running"
)
func ConnectMongoDB(uri string, username, passwd string) (*mongo.Client, error) {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri).SetAuth(options.Credential{
Username: username,
Password: passwd,
}))
if err != nil {
return nil, err
}
return client, nil
}
func init() {
client, err := ConnectMongoDB("mongodb://localhost:27017", "admin", "admin")
if err != nil {
......@@ -38,7 +52,7 @@ func initdata(client *mongo.Client) []string {
db := NewDBWorker(client, database)
dbRunning := NewDBWorkerRunning(client, database)
// Insert 1,000,000 DbWorkerInfo to db
// Insert 1,000,000 DbWorkerInfo to operator
for i := 0; i < 1000; i++ {
worker := generateAWroker()
result, err := db.InsertWorker(context.Background(), worker)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment