Skip to content

Commit 511123a

Browse files
review
1 parent 664d4ef commit 511123a

File tree

4 files changed

+54
-43
lines changed

4 files changed

+54
-43
lines changed

functions/go-mongo/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# Mongo + Go example
22

3-
Example to demonstrate the possibility to connect Serverless Functions to MongoDB.
3+
Example to demonstrate the possibility of connecting Serverless Functions to MongoDB.
44

55
For this example, [Scaleway Console](https://console.scaleway.com/) will be used for deployment.
66

77
> [!WARNING]
8-
> This is a basic sample that does not use certificate for authentication, not recommended for production.
8+
> This is a basic sample that does not use a certificate for authentication, not recommended for production.
99
1010
## Requirements
1111

12-
- Mongo DB created [documentation](https://www.scaleway.com/en/docs/managed-mongodb-databases/quickstart/#how-to-create-a-database-instance)
12+
- MongoDB created [documentation](https://www.scaleway.com/en/docs/managed-mongodb-databases/quickstart/#how-to-create-a-database-instance)
1313

1414
## Step 1 - Mongo
1515

16-
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`
16+
After MongoDB creation, in the console, find the public endpoint of the database; it should look like: `<scw_database_id>.mgdb.<scw_region>.scw.cloud`
1717

1818
Once you get the endpoint, keep it somewhere for later use.
1919

2020
## Step 2 - Function Creation
2121

22-
Before creating a the Function, we need to package it into a zip file.
22+
Before creating the Function, we need to package it into a zip file.
2323

2424
> [!TIP]
2525
> It's recommended to do it via command line tools, so the following example shows how to zip this current folder:
@@ -30,17 +30,17 @@ Before creating a the Function, we need to package it into a zip file.
3030
> ```
3131
3232
- 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)
33-
- In the created namespace, create a Serverless Function
34-
- Select the latest Go runtime
35-
- Upload the previously created `go-mongo.zip`
36-
- Ensure the handler is `Handle`
33+
- In the created namespace, create a Serverless Function.
34+
- Select the latest Go runtime.
35+
- Upload the previously created `go-mongo.zip`.
36+
- Ensure the handler is `Handle`.
3737
- Add required Secrets to the Function:
3838
3939
| Key | Value |
4040
| --------------------- | -------------------------------------------------------------------------------------------------------------- |
41-
| MONGO_PUBLIC_ENDPOINT | (replace with required values): `<scw_database_id>.mgdb.<scw_region>.scw.cloud` |
42-
| MONGO_USER | user created during Mongo DB setup |
43-
| MONGO_PASSWORD | password created during Mongo DB user setup |
41+
| MONGO_PUBLIC_ENDPOINT | (replace with required values): `<scw_database_id>.mgdb.<scw_region>.scw.cloud` |
42+
| MONGO_USER | user created during MongoDB setup |
43+
| MONGO_PASSWORD | password created during MongoDB user setup |
4444
4545
## Step 3 - Test
4646
@@ -54,6 +54,6 @@ Result should be similar to:
5454
5555
## Local testing
5656
57-
For testing you can use [Go Offline Testing](https://github.com/scaleway/serverless-functions-go).
57+
For testing, you can use [Go Offline Testing](https://github.com/scaleway/serverless-functions-go).
5858
59-
To run locally, execute: `go run cmd/main.go`.
59+
To run locally, execute: `go run cmd/main.go`.

functions/go-mongo/go.mod

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ require (
88
)
99

1010
require (
11-
github.com/golang/snappy v0.0.4 // indirect
12-
github.com/google/uuid v1.3.0 // indirect
13-
github.com/klauspost/compress v1.16.7 // indirect
11+
github.com/golang/snappy v1.0.0 // indirect
12+
github.com/google/go-cmp v0.7.0 // indirect
13+
github.com/google/uuid v1.6.0 // indirect
14+
github.com/klauspost/compress v1.18.0 // indirect
1415
github.com/montanaflynn/stats v0.7.1 // indirect
16+
github.com/stretchr/testify v1.10.0 // indirect
1517
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
1618
github.com/xdg-go/scram v1.1.2 // indirect
1719
github.com/xdg-go/stringprep v1.0.4 // indirect
1820
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
19-
golang.org/x/crypto v0.26.0 // indirect
20-
golang.org/x/sync v0.8.0 // indirect
21-
golang.org/x/text v0.17.0 // indirect
21+
golang.org/x/crypto v0.38.0 // indirect
22+
golang.org/x/sync v0.14.0 // indirect
23+
golang.org/x/text v0.25.0 // indirect
2224
)

functions/go-mongo/go.sum

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
4-
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
5-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
6-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
7-
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
8-
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9-
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
10-
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
3+
github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs=
4+
github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
5+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
6+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
7+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
8+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9+
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
10+
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
1111
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
1212
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
1313
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1414
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1515
github.com/scaleway/serverless-functions-go v0.1.2 h1:UUToB+XXpLDG9l6c9c0ALhAs1jvb6rV2Lkyj2kZEYCo=
1616
github.com/scaleway/serverless-functions-go v0.1.2/go.mod h1:23Hj74DYzTsVY3QgvsM0MGnx0+0OAAabTC06BOMJE58=
17-
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
18-
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
17+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
18+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1919
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
2020
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
2121
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
@@ -29,16 +29,16 @@ go.mongodb.org/mongo-driver v1.17.3 h1:TQyXhnsWfWtgAhMtOgtYHMTkZIfBTpMTsMnd9ZBeH
2929
go.mongodb.org/mongo-driver v1.17.3/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
3030
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3131
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
32-
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
33-
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
32+
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
33+
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
3434
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
3535
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
3636
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
3737
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
3838
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
3939
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
40-
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
41-
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
40+
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
41+
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
4242
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4343
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4444
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -50,8 +50,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5050
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
5151
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
5252
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
53-
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
54-
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
53+
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
54+
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
5555
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
5656
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
5757
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=

functions/go-mongo/main.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ import (
1515
"go.mongodb.org/mongo-driver/mongo/options"
1616
)
1717

18+
// client is a global variable that holds the MongoDB client instance.
1819
var client *mongo.Client
1920

21+
// RandomDocument represents a document in the MongoDB collection with an ID and a Name.
2022
type RandomDocument struct {
2123
ID int `bson:"id"`
2224
Name string `bson:"name"`
2325
}
2426

27+
// init initializes the MongoDB client connection.
28+
// It reads the MongoDB connection details from environment variables and establishes a connection.
2529
func init() {
2630
mongoPublicEndpoint := os.Getenv("MONGO_PUBLIC_ENDPOINT")
2731
if mongoPublicEndpoint == "" {
@@ -45,7 +49,8 @@ func init() {
4549
mongoPublicEndpoint)
4650

4751
var err error
48-
client, err = mongo.Connect(context.TODO(), options.Client().ApplyURI(mongo_uri))
52+
53+
client, err = mongo.Connect(context.TODO(), options.Client().ApplyURI(mongoURI))
4954
if err != nil {
5055
panic(err)
5156
}
@@ -58,7 +63,9 @@ func init() {
5863
slog.Info("Connected to MongoDB")
5964
}
6065

61-
func Handle(w http.ResponseWriter, r *http.Request) {
66+
// Handle is an HTTP handler function that inserts a random document into the MongoDB collection
67+
// and returns the inserted document as a JSON response.
68+
func Handle(w http.ResponseWriter, _ *http.Request) {
6269
collection := client.Database("testdb").Collection("testcollection")
6370

6471
randomID := rand.Intn(100000000)
@@ -70,22 +77,24 @@ func Handle(w http.ResponseWriter, r *http.Request) {
7077

7178
insertResult, err := collection.InsertOne(context.TODO(), doc)
7279
if err != nil {
73-
panic(err)
80+
http.Error(w, "Failed to insert document", http.StatusInternalServerError)
81+
return
7482
}
7583

76-
slog.Info("Inserted document with ID:", insertResult.InsertedID)
84+
slog.Info("Inserted document", "id", insertResult.InsertedID)
7785

7886
var result RandomDocument
7987

8088
if err := collection.FindOne(context.TODO(), bson.M{"_id": insertResult.InsertedID}).Decode(&result); err != nil {
81-
panic(err)
89+
http.Error(w, "Failed to find document", http.StatusInternalServerError)
90+
return
8291
}
8392

84-
slog.Info("Found document", result.Name, result.ID)
93+
slog.Info("Found document", "name", result.Name, "id", result.ID)
8594

8695
w.Header().Set("Content-Type", "application/json")
8796

8897
if err := json.NewEncoder(w).Encode(result); err != nil {
89-
panic(err)
98+
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
9099
}
91100
}

0 commit comments

Comments
 (0)