-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb876cc
commit cc928c4
Showing
6 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
go 1.21.5 | ||
|
||
use ./server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
"go.mongodb.org/mongo-driver/mongo/readpref" | ||
) | ||
|
||
var ( | ||
connectionStringEnvVar = "AZURE_COSMOS_JARVIS_DATABASE_CONNECTION_CONNECTIONSTRING" | ||
) | ||
|
||
func getMongoCollectionConnection(collectionName string) (*mongo.Collection, context.Context, func()) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
|
||
if connectionString, ok := os.LookupEnv(connectionStringEnvVar); ok { | ||
client, err := mongo.Connect(ctx, options.Client().ApplyURI(connectionString)) | ||
if err != nil { | ||
log.Fatalf("Failed to connect to mongo: %s\n", err) | ||
} | ||
err = client.Ping(ctx, readpref.Primary()) | ||
if err != nil { | ||
log.Fatalf("Failed to ping mongos: %s\n", err) | ||
} | ||
|
||
cleanup := func() { | ||
cancel() | ||
if err = client.Disconnect(ctx); err != nil { | ||
log.Printf("WARN: Failed to disconnect from Cosmos: %s\n", err) | ||
} | ||
} | ||
|
||
// If in development mode, append -dev to collection | ||
if gin.Mode() != gin.ReleaseMode { | ||
collectionName = fmt.Sprintf("%s-dev", collectionName) | ||
} | ||
|
||
collection := client.Database("db").Collection(collectionName) | ||
return collection, ctx, cleanup | ||
} | ||
|
||
// Fatal | ||
log.Fatalf("Failed to connect to mongo: '%s' not set \n", connectionStringEnvVar) | ||
return nil, nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters