Skip to content

Commit 1322cfb

Browse files
authored
docs: Go examples use offline testing (#33)
* tests: update go-mnq-sqs-publish example to use serverless local testing * tests: update go-upload-file-s3-multipart example to use serverless local testing * fix(docs): update wrong version for MultiPart Upload to S3 example in README * tests: update cors-go example to use serverless local testing
1 parent 08266d3 commit 1322cfb

File tree

12 files changed

+167
-47
lines changed

12 files changed

+167
-47
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Table of Contents:
3434
| **[CORS Python](functions/cors-python/README.md)** <br/> A Python function which allows CORS requests. | python310 | [Serverless Framework] |
3535
| **[CORS Rust](functions/cors-rust/README.md)** <br/> A Rust function which allows CORS requests. | rust165 | [Serverless Framework] |
3636
| **[Go SQS Publish](functions/go-mnq-sqs-publish/README.md)** <br/> A Go function to publish messages to SQS. | go118 | [Serverless Framework] |
37-
| **[Go MultiPart Upload to S3](functions/go-upload-file-s3-multipart)** <br/> A function to upload file from form-data to S3. | go119 | [Serverless Framework] |
37+
| **[Go MultiPart Upload to S3](functions/go-upload-file-s3-multipart)** <br/> A function to upload file from form-data to S3. | go120 | [Serverless Framework] |
3838
| **[Image Transform](functions/image-transform-node/README.md)** <br/> A function that resizes images from an S3 bucket. | node16 | [Serverless Framework] |
3939
| **[Node MultiPart Upload to S3](functions/node-upload-file-s3-multipart/README.md)** <br/> A function to upload file from form-data to S3. | node19 | [Serverless Framework] |
4040
| **[PHP write to S3](functions/php-s3/README.md)** <br/> A PHP function that connects to, and writes to an S3 bucket. | php82 | [Terraform] |

functions/cors-go/README.md

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This example assumes you are familiar with how serverless functions work. If nee
66

77
This example uses the Scaleway Serverless Framework Plugin. Please set up your environment with the requirements stated in the [Scaleway Serverless Framework Plugin](https://github.com/scaleway/serverless-scaleway-functions) before trying out the example.
88

9+
Additionnaly it uses the [serverless-functions-go](https://github.com/scaleway/serverless-functions-go) library for local testing.
10+
911
## Context
1012

1113
This example shows how to handle preflight CORS requests that will be sent by a browser when invoking a function. The example uses totally permissive, open CORS, you may want to modify this to make it more secure.
@@ -18,22 +20,18 @@ This function uses Golang 1.19 runtime.
1820

1921
## Setup
2022

21-
Once your environment is set up, you can run:
23+
Once your environment is set up, you can test your function locally with:
2224

23-
```console
24-
npm install
25+
```sh
26+
npm install
2527

26-
serverless deploy
28+
go run test/main.go
2729
```
2830

29-
Then, from the given URL, you can run:
30-
31-
```console
32-
# Options request
33-
curl -i -X OPTIONS <function URL>
31+
This will launch a local server, allowing you to test the function. For that, you can run in another terminal:
3432

35-
# Get request
36-
curl -i -X GET <function URL>
33+
```sh
34+
curl -i -X GET http://localhost:8080
3735
```
3836

3937
The result should be similar to:
@@ -45,11 +43,31 @@ access-control-allow-methods: *
4543
access-control-allow-origin: *
4644
content-length: 44
4745
content-type: text/plain
48-
date: Tue, 17 Jan 2023 15:56:52 GMT
46+
date: Tue, 12 Apr 2023 12:34:56 GMT
4947
x-envoy-upstream-service-time: 10
5048
server: envoy
5149

5250
This function is allowing most CORS requests
5351
```
5452

5553
You can also check the result of your function in a browser. It should be "This function is allowing most CORS requests".
54+
55+
## Deploy and run
56+
57+
Finally, if the test succeeded, you can deploy your function with:
58+
59+
```console
60+
serverless deploy
61+
```
62+
63+
Then, from the given URL, you can run:
64+
65+
```console
66+
# Options request
67+
curl -i -X OPTIONS <function URL>
68+
69+
# Get request
70+
curl -i -X GET <function URL>
71+
```
72+
73+
When invoking this function, the output should be similar to the one obtained when testing locally.

functions/cors-go/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
module cors-go
22

33
go 1.19
4+
5+
require github.com/scaleway/serverless-functions-go v0.1.0
6+
7+
require github.com/google/uuid v1.3.0 // indirect

functions/cors-go/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
3+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/scaleway/serverless-functions-go v0.1.0 h1:U967AmTujugxzzvLIWUNoB+/5hLmsys0Xe7n4L38XPA=
6+
github.com/scaleway/serverless-functions-go v0.1.0/go.mod h1:SKb5XA5bONwJkecQElrKLYOVrg/5kmcaduB440HQbIg=
7+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
8+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

functions/cors-go/test/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
handler "cors-go"
5+
6+
"github.com/scaleway/serverless-functions-go/local"
7+
)
8+
9+
func main() {
10+
// Replace "Handle" with your function handler name if necessary
11+
local.ServeHandler(handler.HandleWithCors, local.WithPort(8080))
12+
}
Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
# MNQ SQS Publish Example using Golang
1+
# MNQ SQS Publish message using Golang
22

33
You can use this example to publish a message to an SQS queue using the MNQ namespace from the Scaleway Go SDK.
44

55
## Requirements
66

7-
This example assumes you are familiar with how serverless functions work. If needed, you can check [Scaleway's official documentation](https://www.scaleway.com/en/docs/serverless/functions/quickstart/)
7+
This example assumes that you are familiar with some products of Scaleway's ecosystem:
8+
9+
* how serverless functions work. If needed, you can check [Scaleway official documentation](https://www.scaleway.com/en/docs/serverless/functions/quickstart/).
10+
* how Messaging and Queuing works. Please refer to scaleway's documentation [here](https://www.scaleway.com/en/docs/serverless/messaging/quickstart/).
811

912
This example uses the Scaleway Serverless Framework Plugin. Please set up your environment with the requirements stated in the [Scaleway Serverless Framework Plugin](https://github.com/scaleway/serverless-scaleway-functions) before trying out the example.
1013

11-
## Description
14+
Additionnaly it uses the [serverless-functions-go](https://github.com/scaleway/serverless-functions-go) library for local testing.
15+
16+
## Context
1217

13-
The function of the example is publishing a message to an SQS queue. The message is a JSON object with the following structure:
18+
This example shows how to create a queue and send a message to a SQS. It can be extended to retrieve or delete queues, send batch messages, delete or retrieve messages, etc...
19+
20+
The function is publishing a message to an SQS queue. The message is a JSON object with the following structure:
1421

1522
```json
1623
{
@@ -23,30 +30,43 @@ This function uses Golang 1.18 runtime.
2330

2431
## Setup
2532

26-
Once your environment is set up, you can run:
33+
To use this example, the following environment variables are needed:
2734

28-
```console
29-
go run .
35+
* `SCW_ACCESS_KEY`: Your Scaleway access key
36+
* `SCW_SECRET_KEY`: Your Scaleway secret key
37+
* `SCW_DEFAULT_PROJECT_ID`: Your Scaleway project ID
38+
* `SCW_DEFAULT_REGION`: Your Scaleway region
39+
* `SCW_DEFAULT_ZONE`: Your Scaleway zone
40+
* `SQS_ACCESS_KEY`: Your SQS access key
41+
* `SQS_SECRET_KEY`: Your SQS secret key
3042

31-
serverless deploy
43+
Once your environment is set up, you can test your function locally with:
44+
45+
```shell
46+
go run test/main.go
3247
```
3348

34-
## Needed environment variables
49+
This will launch a local server, allowing you to test the function. For that, you can run in another terminal:
3550

36-
- `SCW_ACCESS_KEY`: Your Scaleway access key
37-
- `SCW_SECRET_KEY`: Your Scaleway secret key
38-
- `SCW_DEFAULT_PROJECT_ID`: Your Scaleway project ID
39-
- `SCW_DEFAULT_REGION`: Your Scaleway region
40-
- `SCW_DEFAULT_ZONE`: Your Scaleway zone
51+
```shell
52+
curl -X POST http://localhost:8080 -d '{"username": "John Doe", "message": "Hello World!"}'
53+
```
54+
55+
The status code returned by this request should be `200`.
4156

42-
- `SQS_ACCESS_KEY`: Your SQS access key
43-
- `SQS_SECRET_KEY`: Your SQS secret key
57+
## Deploy and run
4458

45-
## Running
59+
Finally, if the test succeeded, you can deploy your function with:
60+
61+
```console
62+
serverless deploy
63+
```
4664

4765
Then, from the given URL, you can run:
4866

4967
```shell
50-
# Options request
68+
# POST request
5169
curl -i -X POST <function URL> -d '{"username": "John Doe", "message": "Hello World!"}'
5270
```
71+
72+
Once again, the expected status code for this request is `200`.

functions/go-mnq-sqs-publish/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ go 1.18
55
require (
66
github.com/aws/aws-sdk-go v1.44.189
77
github.com/go-playground/validator/v10 v10.11.1
8+
github.com/scaleway/serverless-functions-go v0.1.0
89
)
910

1011
require (
1112
github.com/go-playground/locales v0.14.0 // indirect
1213
github.com/go-playground/universal-translator v0.18.0 // indirect
14+
github.com/google/uuid v1.3.0 // indirect
1315
github.com/jmespath/go-jmespath v0.4.0 // indirect
1416
github.com/leodido/go-urn v1.2.1 // indirect
1517
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect

functions/go-mnq-sqs-publish/go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j
1212
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
1313
github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ=
1414
github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
15+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
16+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1517
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
1618
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
1719
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
@@ -30,10 +32,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
3032
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3133
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
3234
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
35+
github.com/scaleway/serverless-functions-go v0.1.0 h1:U967AmTujugxzzvLIWUNoB+/5hLmsys0Xe7n4L38XPA=
36+
github.com/scaleway/serverless-functions-go v0.1.0/go.mod h1:SKb5XA5bONwJkecQElrKLYOVrg/5kmcaduB440HQbIg=
3337
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3438
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
35-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
3639
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
40+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
3741
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
3842
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3943
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
@@ -76,5 +80,5 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
7680
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
7781
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7882
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
79-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
8083
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
84+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
scw "go-mnq-publish"
5+
6+
"github.com/scaleway/serverless-functions-go/local"
7+
)
8+
9+
func main() {
10+
local.ServeHandler(scw.Handle, local.WithPort(8080))
11+
}
Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
1-
# Go function to read file and upload to S3
1+
# Go function to read file and upload it to S3
2+
3+
This example shows how to upload a file to an S3 bucket.
4+
5+
## Requirements
6+
7+
This example assumes that you are familiar with some products of Scaleway's ecosystem:
8+
9+
* how serverless functions work. If needed, you can check [Scaleway official documentation](https://www.scaleway.com/en/docs/serverless/functions/quickstart/).
10+
* how Object Storage works. Please refer to scaleway's documentation [here](https://www.scaleway.com/en/docs/storage/object/quickstart/) for more information.
11+
12+
This example uses the Scaleway Serverless Framework Plugin. Please set up your environment with the requirements stated in the [Scaleway Serverless Framework Plugin](https://github.com/scaleway/serverless-scaleway-functions) before trying out the example.
13+
14+
Additionnaly it uses the [serverless-functions-go](https://github.com/scaleway/serverless-functions-go) library for local testing.
15+
16+
## Context
17+
18+
This example shows how to upload a file to an S3 bucket using serverless function. It also shows how you can test your function locally before deploying.
19+
20+
This function does the following steps:
221

3-
This function does the following steps :
422
* Read a file from HTTP request
523
* Save the file locally in ephemeral storage
624
* Send file to S3 bucket
725

8-
Additionnaly it uses the [serverless-functions-go](https://github.com/scaleway/serverless-functions-go) library for local testing
26+
This function uses Golang 1.20 runtime.
927

10-
## Requirements
28+
## Setup
1129

1230
If you want to enable S3 upload, ensure to create a bucket and have the following secrets variables available in your environment:
1331

14-
```
32+
```sh
1533
S3_ENABLED=true
1634
S3_ENDPOINT= # ex: sample.s3.fr-par.scw.cloud
1735
S3_ACCESSKEY=
@@ -20,17 +38,40 @@ S3_BUCKET_NAME=
2038
S3_REGION= # ex: fr-par
2139
```
2240

23-
If s3 is not enabled the file will be saved on the ephemeral storage of your function.
41+
If s3 is not enabled, the file will be saved on the ephemeral storage of your function.
42+
43+
Once your environment is set up, you can test your function locally with:
2444

2545
```sh
26-
go get
46+
go run test/main.go
47+
```
2748

28-
# for local testing :
29-
go run cmd/main.go
49+
This will launch a local server, allowing you to test the function. For that, you can run in another terminal (replace `go.sum` with the file you want to upload):
50+
51+
```sh
52+
curl -X POST http://localhost:8080 -H "Content-Type: multipart/form-data" -F "[email protected]"
3053
```
3154

32-
To call the function (replace `go.sum` with the file you want to upload):
55+
The logs should be similar to:
3356

3457
```sh
35-
curl -X POST -H "Content-Type: multipart/form-data" -F "[email protected]" http://localhost:8080
58+
2023/04/11 11:30:06 S3 upload enabled
59+
2023/04/11 11:30:08 successfully created upload-file-to-s3
60+
2023/04/11 11:30:09 Successfully uploaded /var/folders/wn/qnp2ebt54mz040bgffg35xt80000gn/T/go.sum of size 1234
3661
```
62+
63+
## Deploy and run
64+
65+
Finally, if the test succeeded, you can deploy your function with:
66+
67+
```console
68+
serverless deploy
69+
```
70+
71+
Then, from the given URL, you can run:
72+
73+
```sh
74+
curl -X POST <function URL> -H "Content-Type: multipart/form-data" -F "[email protected]"
75+
```
76+
77+
When invoking this function, the output should be similar to the one obtained when testing locally.

0 commit comments

Comments
 (0)