-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: main
Are you sure you want to change the base?
Conversation
|
||
## 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` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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`, |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
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` | ||
|
There was a problem hiding this comment.
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.
No description provided.