Skip to content

Commit

Permalink
feat(functions): add Golang hello world example (#59)
Browse files Browse the repository at this point in the history
* Go hello world

* Add golang hello world

* Fix naming

* Add to main README

* Fix copy-paste error

* Review comments

* Remove zip
  • Loading branch information
Shillaker authored Nov 6, 2023
1 parent 9d8e498 commit 86de4e4
Show file tree
Hide file tree
Showing 13 changed files with 3,217 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/go-function-offline-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
matrix:
tests: [
{ directory: 'cors-go', go-version: '1.19' },
{ directory: 'go-hello-world', go-version: '1.20' }
{ directory: 'go-mnq-sqs-publish', go-version: '1.18' },
{ directory: 'go-upload-file-s3-multipart', go-version: '1.20' }
]
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@

# Serverless framework
node_modules/
.serverless/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Table of Contents:
| **[CORS Node](functions/cors-node/README.md)** <br/> A Node function which allows CORS requests. | node18 | [Serverless Framework] |
| **[CORS Python](functions/cors-python/README.md)** <br/> A Python function which allows CORS requests. | python310 | [Serverless Framework] |
| **[CORS Rust](functions/cors-rust/README.md)** <br/> A Rust function which allows CORS requests. | rust165 | [Serverless Framework] |
| **[Go Hello World](functions/go-hello-world/README.md)** <br/> A simple "hello world" example for Go | go118 | [Serverless Framework] |
| **[Go SQS Publish](functions/go-mnq-sqs-publish/README.md)** <br/> A Go function to publish messages to SQS. | go118 | [Serverless Framework] |
| **[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] |
| **[Image Transform](functions/image-transform-node/README.md)** <br/> A function that resizes images from an S3 bucket. | node16 | [Serverless Framework] |
Expand Down
2 changes: 2 additions & 0 deletions functions/go-hello-world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

*.zip
45 changes: 45 additions & 0 deletions functions/go-hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Golang hello world

This is a simple example of how to create a Golang function to run on Scaleway Serverless Functions.

## Requirements

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/)

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.

Additionnaly it uses the [serverless-functions-go](https://github.com/scaleway/serverless-functions-go) library for local testing.

## Setup

First set up Serverless Framework with:

```sh
npm i
```

You can run your function locally with:

```sh
go run test/main.go
```

Then in another terminal, you can make a request:

```sh
curl http://localhost:8080
```

... and run the tests:

```sh
go test ./...
```

If the tests succeed, you can deploy your function with:

```sh
serverless deploy
```

Once deployed, you can again submit a request using `curl` to the URL printed in the deployment output.
17 changes: 17 additions & 0 deletions functions/go-hello-world/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module hello-go

go 1.21

require (
github.com/scaleway/serverless-functions-go v0.1.2
github.com/stretchr/testify v1.8.2
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
27 changes: 27 additions & 0 deletions functions/go-hello-world/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/scaleway/serverless-functions-go v0.1.2 h1:UUToB+XXpLDG9l6c9c0ALhAs1jvb6rV2Lkyj2kZEYCo=
github.com/scaleway/serverless-functions-go v0.1.2/go.mod h1:23Hj74DYzTsVY3QgvsM0MGnx0+0OAAabTC06BOMJE58=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
15 changes: 15 additions & 0 deletions functions/go-hello-world/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package hello

import (
"fmt"
"io"
"net/http"
)

func Handle(w http.ResponseWriter, r *http.Request) {
_, err := io.WriteString(w, "Hello from a Go function!")

if err != nil {
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
}
}
22 changes: 22 additions & 0 deletions functions/go-hello-world/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package hello

import (
"io"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

const offlineTestingServer = "http://localhost:8080"

func TestHelloWorld(t *testing.T) {
resp, err := http.Get(offlineTestingServer)
assert.NoError(t, err)

defer resp.Body.Close()

bodyBytes, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "Hello from a Go function!", string(bodyBytes))
}
Loading

0 comments on commit 86de4e4

Please sign in to comment.