Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit fb79ad3

Browse files
authored
add simple golang http endpoint example (#494)
* initial commit, serverless golang example * moving to aws folder for proper provider * updating naming convention * moving folders to match examples repo structure * moving into alignment with existing example repo
1 parent 5c2d4e1 commit fb79ad3

File tree

17 files changed

+2917
-0
lines changed

17 files changed

+2917
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
22+
23+
[[constraint]]
24+
name = "github.com/aws/aws-lambda-go"
25+
version = "1.x"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
build:
2+
dep ensure -v
3+
env GOOS=linux go build -ldflags="-s -w" -o bin/hello hello/main.go
4+
env GOOS=linux go build -ldflags="-s -w" -o bin/world world/main.go
5+
6+
.PHONY: clean
7+
clean:
8+
rm -rf ./bin ./vendor Gopkg.lock
9+
10+
.PHONY: deploy
11+
deploy: clean build
12+
sls deploy --verbose
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
8+
"github.com/aws/aws-lambda-go/events"
9+
"github.com/aws/aws-lambda-go/lambda"
10+
)
11+
12+
// Response is of type APIGatewayProxyResponse since we're leveraging the
13+
// AWS Lambda Proxy Request functionality (default behavior)
14+
//
15+
// https://serverless.com/framework/docs/providers/aws/events/apigateway/#lambda-proxy-integration
16+
type Response events.APIGatewayProxyResponse
17+
18+
// Handler is our lambda handler invoked by the `lambda.Start` function call
19+
func Handler(ctx context.Context) (Response, error) {
20+
var buf bytes.Buffer
21+
22+
body, err := json.Marshal(map[string]interface{}{
23+
"message": "Go Serverless v1.0! Your function executed successfully!",
24+
})
25+
if err != nil {
26+
return Response{StatusCode: 404}, err
27+
}
28+
json.HTMLEscape(&buf, body)
29+
30+
resp := Response{
31+
StatusCode: 200,
32+
IsBase64Encoded: false,
33+
Body: buf.String(),
34+
Headers: map[string]string{
35+
"Content-Type": "application/json",
36+
"X-MyCompany-Func-Reply": "hello-handler",
37+
},
38+
}
39+
40+
return resp, nil
41+
}
42+
43+
func main() {
44+
lambda.Start(Handler)
45+
}

0 commit comments

Comments
 (0)