Skip to content

Commit

Permalink
add simple golang http endpoint example (#494)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sebito91 committed Aug 1, 2018
1 parent 5c2d4e1 commit fb79ad3
Show file tree
Hide file tree
Showing 17 changed files with 2,917 additions and 0 deletions.
25 changes: 25 additions & 0 deletions examples/aws-golang-simple-http-endpoint/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
name = "github.com/aws/aws-lambda-go"
version = "1.x"
12 changes: 12 additions & 0 deletions examples/aws-golang-simple-http-endpoint/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
build:
dep ensure -v
env GOOS=linux go build -ldflags="-s -w" -o bin/hello hello/main.go
env GOOS=linux go build -ldflags="-s -w" -o bin/world world/main.go

.PHONY: clean
clean:
rm -rf ./bin ./vendor Gopkg.lock

.PHONY: deploy
deploy: clean build
sls deploy --verbose
45 changes: 45 additions & 0 deletions examples/aws-golang-simple-http-endpoint/hello/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"bytes"
"context"
"encoding/json"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

// Response is of type APIGatewayProxyResponse since we're leveraging the
// AWS Lambda Proxy Request functionality (default behavior)
//
// https://serverless.com/framework/docs/providers/aws/events/apigateway/#lambda-proxy-integration
type Response events.APIGatewayProxyResponse

// Handler is our lambda handler invoked by the `lambda.Start` function call
func Handler(ctx context.Context) (Response, error) {
var buf bytes.Buffer

body, err := json.Marshal(map[string]interface{}{
"message": "Go Serverless v1.0! Your function executed successfully!",
})
if err != nil {
return Response{StatusCode: 404}, err
}
json.HTMLEscape(&buf, body)

resp := Response{
StatusCode: 200,
IsBase64Encoded: false,
Body: buf.String(),
Headers: map[string]string{
"Content-Type": "application/json",
"X-MyCompany-Func-Reply": "hello-handler",
},
}

return resp, nil
}

func main() {
lambda.Start(Handler)
}
Loading

0 comments on commit fb79ad3

Please sign in to comment.