Skip to content

Commit 17a65a5

Browse files
committed
Initial commit.
0 parents  commit 17a65a5

File tree

8 files changed

+153
-0
lines changed

8 files changed

+153
-0
lines changed

.github/[email protected]

93.9 KB
Loading

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
vendor/
16+
17+
# Etc.
18+
.idea
19+
.DS_Store
20+
21+
# Binary
22+
netlify-golang-function-tutorial

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Hackers and Slackers
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Netlify Golang Function Tutorial
2+
3+
![Go](https://img.shields.io/badge/Go-1.14-blue.svg?logo=go&longCache=true&logoColor=white&colorB=88C0D0&style=flat-square&colorA=4c566a)
4+
![AWS Lambda](https://img.shields.io/badge/AWS--Lambda-1.17.0-blue.svg?logo=go&longCache=true&logoColor=white&colorB=88C0D0&style=flat-square&colorA=4c566a)
5+
![Testify](https://img.shields.io/badge/Testify-1.4.0-blue.svg?logo=go&longCache=true&logoColor=white&colorB=88C0D0&style=flat-square&colorA=4c566a)
6+
![GitHub Last Commit](https://img.shields.io/github/last-commit/google/skia.svg?style=flat-square&colorA=4c566a&colorB=a3be8c&logo=GitHub)
7+
[![GitHub Issues](https://img.shields.io/github/issues/hackersandslackers/netlify-golang-function-tutorial.svg?style=flat-square&colorA=4c566a&colorB=ebcb8b&logo=GitHub)](https://github.com/hackersandslackers/lambda-metadata-scraper/issues)
8+
[![GitHub Stars](https://img.shields.io/github/stars/hackersandslackers/netlify-golang-function-tutorial.svg?style=flat-square&colorB=ebcb8b&colorA=4c566a&logo=GitHub)](https://github.com/hackersandslackers/lambda-metadata-scraper/stargazers)
9+
[![GitHub Forks](https://img.shields.io/github/forks/hackersandslackers/netlify-golang-function-tutorial.svg?style=flat-square&colorA=4c566a&colorB=ebcb8b&logo=GitHub)](https://github.com/hackersandslackers/lambda-metadata-scraper/network)
10+
11+
Simple "Hello world" Lambda function which accepts a `?name` parameter for extra personalization. Source code for the accompanying tutorial found here: https://hackersandslackers.com/deploy-serverless-golang-functions-with-netlify/
12+
13+
![Netlify Function Tutorial](./.github/[email protected])
14+
15+
### Example Usage
16+
17+
**Request:**
18+
```shell
19+
$ curl https://example.com/.netlify/functions/name?todd
20+
```
21+
22+
**Response:**
23+
```shell
24+
Hello todd!
25+
```

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/hackersandslackers/netlify-golang-function-tutorial
2+
3+
go 1.14
4+
5+
require (
6+
github.com/aws/aws-lambda-go v1.18.0
7+
github.com/stretchr/testify v1.4.0
8+
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/aws/aws-lambda-go v1.18.0 h1:13AfxzFoPlFjOzXHbRnKuTbteCzHbu4YQgKONNhWcmo=
3+
github.com/aws/aws-lambda-go v1.18.0/go.mod h1:FEwgPLE6+8wcGBTe5cJN3JWurd1Ztm9zN4jsXsjzKKw=
4+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
5+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
6+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
10+
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
11+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
13+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
14+
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
15+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
16+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
17+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/aws/aws-lambda-go/events"
6+
"github.com/aws/aws-lambda-go/lambda"
7+
)
8+
9+
func Handler(request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
10+
name := request.QueryStringParameters["name"]
11+
response := fmt.Sprintf("Hello %s!", name)
12+
13+
return &events.APIGatewayProxyResponse{
14+
StatusCode: 200,
15+
Headers: map[string]string{"Content-Type": "text/html; charset=UTF-8"},
16+
Body: response,
17+
}, nil
18+
}
19+
20+
func main() {
21+
// Initiate AWS Lambda handler
22+
lambda.Start(Handler)
23+
}

main_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main_test
2+
3+
import (
4+
"github.com/aws/aws-lambda-go/events"
5+
main "github.com/hackersandslackers/netlify-golang-function-tutorial"
6+
"github.com/stretchr/testify/assert"
7+
"log"
8+
"testing"
9+
)
10+
11+
func TestHandler(t *testing.T) {
12+
tests := []struct {
13+
request events.APIGatewayProxyRequest
14+
expect string
15+
err error
16+
}{
17+
{
18+
// Test name has value
19+
request: events.APIGatewayProxyRequest{QueryStringParameters: map[string]string{"name": "Paul"}},
20+
expect: "Hello Paul!",
21+
err: nil,
22+
},
23+
{
24+
// Test name is null
25+
request: events.APIGatewayProxyRequest{},
26+
expect: "Hello !",
27+
err: nil,
28+
},
29+
}
30+
31+
for i, test := range tests {
32+
response, err := main.Handler(test.request)
33+
assert.IsType(t, test.err, err)
34+
assert.Equal(t, test.expect, response.Body)
35+
log.Printf("Test %d: %s", i, response.Body)
36+
}
37+
}

0 commit comments

Comments
 (0)