Skip to content

Commit f09417b

Browse files
author
Vishal Parikh
committed
Initial commit
0 parents  commit f09417b

File tree

7 files changed

+224
-0
lines changed

7 files changed

+224
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/*
2+
example/hello

Readme.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
# Wrap
3+
A very simple wrapper to wrap your [Labstack Echo ](https://github.com/labstack/echo) API's into Lambda/API Gateway
4+
5+
### Installation
6+
```
7+
go get -u github.com/vdparikh/wrap
8+
go mod vendor
9+
```
10+
11+
### Usage
12+
Check out example/example.go for usage.
13+
14+
```go
15+
package main
16+
17+
import (
18+
"github.com/aws/aws-lambda-go/lambda"
19+
"github.com/labstack/echo"
20+
"github.com/labstack/echo/middleware"
21+
"github.com/vdparikh/wrap"
22+
)
23+
24+
func main() {
25+
e := echo.New()
26+
27+
e.Use(middleware.Logger())
28+
29+
e.GET("/hello", func(c echo.Context) error {
30+
return c.JSON(200, "HELLO")
31+
})
32+
33+
server := wrap.Route(e)
34+
35+
lambda.Start(server)
36+
}
37+
```
38+
39+
### Run sample
40+
41+
To run this locally as SAM (Serverless Application Model), then you need to install SAM CLI. SAM is an easier way to test out your serverless functions without having to deploy them first.
42+
- [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install-mac.html)
43+
44+
```sh
45+
cd example
46+
# Build Binary
47+
GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -o hello .
48+
49+
# Start SAM
50+
sam local start-api
51+
```
52+
53+
On another terminal execute the API and you should see the HELLO response
54+
```sh
55+
curl -v http://localhost:3000/hello
56+
```

example/example.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"github.com/aws/aws-lambda-go/lambda"
5+
"github.com/labstack/echo"
6+
"github.com/labstack/echo/middleware"
7+
"github.com/vdparikh/wrap"
8+
)
9+
10+
func main() {
11+
e := echo.New()
12+
13+
e.Use(middleware.Logger())
14+
15+
e.GET("/hello", func(c echo.Context) error {
16+
return c.JSON(200, "HELLO")
17+
})
18+
19+
server := wrap.Route(e)
20+
21+
lambda.Start(server)
22+
}

example/template.yaml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
AWSTemplateFormatVersion : '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: AWS Data Lake Serverless Template
4+
5+
Parameters:
6+
Environment:
7+
Type: String
8+
Default: prod
9+
10+
StageName:
11+
Type: String
12+
Default: prod
13+
Description: The Lambda Function and API Gateway Stage
14+
15+
16+
Globals:
17+
Function:
18+
Runtime: go1.x
19+
MemorySize: 512
20+
Timeout: 30
21+
Tags:
22+
23+
24+
Api:
25+
Cors:
26+
AllowMethods: "'GET,POST,DELETE,PUT,OPTIONS'"
27+
AllowHeaders: "'Authorization,authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'"
28+
AllowOrigin: "'*'"
29+
30+
Resources:
31+
ApiGateway:
32+
Type: AWS::Serverless::Api
33+
Properties:
34+
StageName: Prod
35+
36+
HelloApi:
37+
Type: AWS::Serverless::Function
38+
Properties:
39+
Handler: ./hello
40+
Events:
41+
hello:
42+
Type: Api
43+
Properties:
44+
Path: '/hello'
45+
Method: get
46+
RestApiId: !Ref ApiGateway

go.mod

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/vdparikh/wrap
2+
3+
go 1.12
4+
5+
require (
6+
github.com/aws/aws-lambda-go v1.9.0
7+
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
8+
github.com/labstack/echo v3.3.10+incompatible
9+
github.com/labstack/gommon v0.2.8 // indirect
10+
github.com/mattn/go-colorable v0.1.1 // indirect
11+
github.com/mattn/go-isatty v0.0.7 // indirect
12+
github.com/valyala/fasttemplate v1.0.1 // indirect
13+
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a // indirect
14+
)

go.sum

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
github.com/aws/aws-lambda-go v1.9.0 h1:r9TWtk8ozLYdMW+aelUeWny8z2mjghJCMx6/uUwOLNo=
2+
github.com/aws/aws-lambda-go v1.9.0/go.mod h1:zUsUQhAUjYzR8AuduJPCfhBuKWUaDbQiPOG+ouzmE1A=
3+
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
4+
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
5+
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
6+
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
7+
github.com/labstack/gommon v0.2.8 h1:JvRqmeZcfrHC5u6uVleB4NxxNbzx6gpbJiQknDbKQu0=
8+
github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4=
9+
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
10+
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
11+
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
12+
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
13+
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
14+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
15+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
16+
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
17+
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
18+
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a h1:YX8ljsm6wXlHZO+aRz9Exqr0evNhKRNe5K/gi+zKh4U=
19+
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
20+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
21+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
22+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

wrap.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package wrap
2+
3+
import (
4+
"context"
5+
"io/ioutil"
6+
"net/http"
7+
"net/http/httptest"
8+
"strings"
9+
10+
"github.com/aws/aws-lambda-go/events"
11+
"github.com/labstack/echo"
12+
)
13+
14+
func formatAPIResponse(statusCode int, headers http.Header, responseData string) (events.APIGatewayProxyResponse, error) {
15+
responseHeaders := make(map[string]string)
16+
17+
responseHeaders["Content-Type"] = "application/json"
18+
for key, value := range headers {
19+
responseHeaders[key] = ""
20+
21+
if len(value) > 0 {
22+
responseHeaders[key] = value[0]
23+
}
24+
}
25+
26+
responseHeaders["Access-Control-Allow-Origin"] = "*"
27+
responseHeaders["Access-Control-Allow-Headers"] = "origin,Accept,Authorization,Content-Type"
28+
29+
return events.APIGatewayProxyResponse{
30+
Body: responseData,
31+
Headers: responseHeaders,
32+
StatusCode: statusCode,
33+
}, nil
34+
}
35+
36+
// Route wraps echo server into Lambda Handler
37+
func Route(e *echo.Echo) func(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
38+
return func(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
39+
body := strings.NewReader(request.Body)
40+
req := httptest.NewRequest(request.HTTPMethod, request.Path, body)
41+
for k, v := range request.Headers {
42+
req.Header.Add(k, v)
43+
}
44+
45+
q := req.URL.Query()
46+
for k, v := range request.QueryStringParameters {
47+
q.Add(k, v)
48+
}
49+
req.URL.RawQuery = q.Encode()
50+
51+
rec := httptest.NewRecorder()
52+
e.ServeHTTP(rec, req)
53+
54+
res := rec.Result()
55+
responseBody, err := ioutil.ReadAll(res.Body)
56+
if err != nil {
57+
return formatAPIResponse(http.StatusInternalServerError, res.Header, err.Error())
58+
}
59+
60+
return formatAPIResponse(res.StatusCode, res.Header, string(responseBody))
61+
}
62+
}

0 commit comments

Comments
 (0)