From d12699caa38b6a1ebfc0053ccc41893dc41562df Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Mon, 24 Nov 2025 20:21:14 +0700 Subject: [PATCH] feat(couchbase): test connection --- go.mod | 5 +++- go.sum | 2 ++ main.go | 74 +++++++++++++++++++++++++++++++++------------------------ 3 files changed, 49 insertions(+), 32 deletions(-) diff --git a/go.mod b/go.mod index 4f10e4f..bb6342a 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module github.com/tiennm99/couchbase-keepalive go 1.24.10 -require github.com/couchbase/gocb/v2 v2.11.1 +require ( + github.com/couchbase/gocb/v2 v2.11.1 + github.com/joho/godotenv v1.5.1 +) require ( github.com/couchbase/gocbcore/v10 v10.8.1 // indirect diff --git a/go.sum b/go.sum index ca44927..96e95f9 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,8 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= diff --git a/main.go b/main.go index 786f419..ec9ffdc 100644 --- a/main.go +++ b/main.go @@ -3,20 +3,52 @@ package main import ( "fmt" "log" + "os" "time" "github.com/couchbase/gocb/v2" + "github.com/joho/godotenv" ) func main() { // Uncomment following line to enable logging // gocb.SetLogger(gocb.VerboseStdioLogger()) + if err := godotenv.Load(); err != nil { + log.Println("Warning: .env file not found") + } + // Update this to your cluster details - connectionString := "cb..cloud.couchbase.com" - bucketName := "travel-sample" - username := "username" - password := "Password!123" + connectionString, isExist := os.LookupEnv("COUCHBASE_CONNECTION_STRING") + if !isExist { + log.Fatal("Warning: COUCHBASE_CONNECTION_STRING not set!") + return + } + username, isExist := os.LookupEnv("COUCHBASE_USERNAME") + if !isExist { + log.Fatal("Warning: COUCHBASE_USERNAME not set!") + return + } + password, isExist := os.LookupEnv("COUCHBASE_PASSWORD") + if !isExist { + log.Fatal("Warning: COUCHBASE_PASSWORD not set!") + return + } + bucketName, isExist := os.LookupEnv("COUCHBASE_BUCKET_NAME") + if !isExist { + log.Fatal("Warning: COUCHBASE_BUCKET_NAME not set!") + return + } + scopeName, isExist := os.LookupEnv("COUCHBASE_SCOPE_NAME") + if !isExist { + log.Fatal("Warning: COUCHBASE_SCOPE_NAME not set!") + return + } + collectionName, isExist := os.LookupEnv("COUCHBASE_COLLECTION_NAME") + if !isExist { + log.Fatal("Warning: COUCHBASE_COLLECTION_NAME not set!") + return + } options := gocb.ClusterOptions{ Authenticator: gocb.PasswordAuthenticator{ @@ -33,7 +65,7 @@ func main() { } // Initialize the Connection - cluster, err := gocb.Connect("couchbases://"+connectionString, options) + cluster, err := gocb.Connect(connectionString, options) if err != nil { log.Fatal(err) } @@ -48,7 +80,7 @@ func main() { // Get a reference to the default collection, required for older Couchbase server versions // col := bucket.DefaultCollection() - col := bucket.Scope("tenant_agent_00").Collection("users") + col := bucket.Scope(scopeName).Collection(collectionName) // Create and store a Document type User struct { @@ -57,12 +89,16 @@ func main() { Interests []string `json:"interests"` } + upsertOptions := gocb.UpsertOptions{ + Expiry: 60 * time.Second, + } + _, err = col.Upsert("u:jade", User{ Name: "Jade", Email: "jade@test-email.com", Interests: []string{"Swimming", "Rowing"}, - }, nil) + }, &upsertOptions) if err != nil { log.Fatal(err) } @@ -79,28 +115,4 @@ func main() { log.Fatal(err) } fmt.Printf("User: %v\n", inUser) - - // Perform a N1QL Query - inventoryScope := bucket.Scope("inventory") - queryResult, err := inventoryScope.Query( - fmt.Sprintf("SELECT * FROM airline WHERE id=10"), - &gocb.QueryOptions{}, - ) - if err != nil { - log.Fatal(err) - } - - // Print each found Row - for queryResult.Next() { - var result interface{} - err := queryResult.Row(&result) - if err != nil { - log.Fatal(err) - } - fmt.Println(result) - } - - if err := queryResult.Err(); err != nil { - log.Fatal(err) - } }