Skip to content

Commit

Permalink
Add golang hello world
Browse files Browse the repository at this point in the history
  • Loading branch information
Shillaker committed Nov 6, 2023
1 parent 17a5b46 commit a7884d3
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 75 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
Binary file added functions/go-hello-world/.serverless/cors-go.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 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/)
Expand All @@ -8,29 +10,36 @@ This example uses the Scaleway Serverless Framework Plugin. Please set up your e

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

## Context

This example shows a simple "hello world" function written in Go.

## Setup

Once your environment is set up, you can test your function locally with:
First set up Serverless Framework with:

```sh
npm install
npm i
```

You can run your function locally with:

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

This will launch a local server, allowing you to test the function. For that, you can run in another terminal:
Then in another terminal, you can make a request:

```sh
curl -i -X GET http://localhost:8080
curl http://localhost:8080
```

If the test succeeded, you can deploy your function with:
... and run the tests:

```console
```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.
File renamed without changes.
File renamed without changes.
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 cors

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 cors

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))
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
35 changes: 0 additions & 35 deletions functions/golang-hello-world/handler.go

This file was deleted.

30 changes: 0 additions & 30 deletions functions/golang-hello-world/handler_test.go

This file was deleted.

0 comments on commit a7884d3

Please sign in to comment.