Skip to content

feat(go): mongo db sample #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

feat(go): mongo db sample #100

wants to merge 3 commits into from

Conversation

thomas-tacquet
Copy link
Contributor

No description provided.

@thomas-tacquet thomas-tacquet self-assigned this Apr 17, 2025
@thomas-tacquet thomas-tacquet requested a review from norbjd April 18, 2025 08:19

## Step 1 - Mongo

After Mongo DB creation, in the console find the public endpoint of the database, it should look lile: `<scw_database_id>.mgdb.<scw_region>.scw.cloud`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
After Mongo DB creation, in the console find the public endpoint of the database, it should look lile: `<scw_database_id>.mgdb.<scw_region>.scw.cloud`
After Mongo DB creation, in the console find the public endpoint of the database, it should look like: `<scw_database_id>.mgdb.<scw_region>.scw.cloud`

> ```

- Create a Serverless Function namespace. [Documentation](https://www.scaleway.com/en/docs/serverless-functions/how-to/create-manage-delete-functions-namespace/#creating-a-serverless-functions-namespace)
- In the created namespace, create Serverless Function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- In the created namespace, create Serverless Function
- In the created namespace, create a Serverless Function


- Create a Serverless Function namespace. [Documentation](https://www.scaleway.com/en/docs/serverless-functions/how-to/create-manage-delete-functions-namespace/#creating-a-serverless-functions-namespace)
- In the created namespace, create Serverless Function
- Select the Go runtime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Select the Go runtime
- Select the latest Go runtime

@@ -0,0 +1,22 @@
module github.com/scaleway/serverless-examples/functions/go-mongo

go 1.24.1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
go 1.24.1
go 1.24

Otherwise, this might break if someone tries to use another patch version, depending on the Go runtime under the hood.

}

// This is a basic sample that does not use certificate for authentication, not recommended for production.
mongo_uri := fmt.Sprintf(`mongodb+srv://%s:%s@%s/?tls=true&tlsInsecure=true`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: code panics if password contains a @ (escape issue): error parsing uri: unescaped @ sign in user info.

I'm not sure if we can escape it, but I feel the connection should be done using options.Credential instead of formatting the string. I've tried with:

credentials := options.Credential{
  Username: mongoUser,
  Password: mongoPassword,
}

var err error
client, err = mongo.Connect(context.TODO(), options.Client().ApplyURI(mongo_uri).SetAuth(credentials))

But I'm facing another issue (unable to authenticate using "SCRAM-SHA-256"). This might have something to do with AuthSource field in Credential, but couldn't figure out.

Anyway, like this, it doesn't work with special @ character.

panic(err)
}

slog.Info("Inserted document with ID:", insertResult.InsertedID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: this displays Inserted document with ID: !BADKEY=abcd.... Maybe slog.Info("Inserted document", "id", insertResult.InsertedID) would fix (untested). Or find a way to display only the ID in string format (no !BADKEY in front).

}

// This is a basic sample that does not use certificate for authentication, not recommended for production.
mongo_uri := fmt.Sprintf(`mongodb+srv://%s:%s@%s/?tls=true&tlsInsecure=true`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
mongo_uri := fmt.Sprintf(`mongodb+srv://%s:%s@%s/?tls=true&tlsInsecure=true`,
mongoURI := fmt.Sprintf(`mongodb+srv://%s:%s@%s/?tls=true&tlsInsecure=true`,

## Step 1 - Mongo

After Mongo DB creation, in the console find the public endpoint of the database, it should look lile: `<scw_database_id>.mgdb.<scw_region>.scw.cloud`

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it lacks the info that the MongoDB must be publicly accessible. Inside a private network, Mongo DB won't be reachable from the container as of today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants