Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

Commit 7cc3115

Browse files
watermelosapessi
authored andcommitted
Add echo support (awslabs#36)
* support for [echo framework](https://github.com/labstack/echo)
1 parent 5a7c178 commit 7cc3115

File tree

5 files changed

+132
-2
lines changed

5 files changed

+132
-2
lines changed

echo/adapter.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Packge echolambda add Echo support for the aws-severless-go-api library.
2+
// Uses the core package behind the scenes and exposes the New method to
3+
// get a new instance and Proxy method to send request to the echo.Echo
4+
package echoadapter
5+
6+
import (
7+
"context"
8+
"net/http"
9+
10+
"github.com/aws/aws-lambda-go/events"
11+
"github.com/awslabs/aws-lambda-go-api-proxy/core"
12+
"github.com/labstack/echo"
13+
)
14+
15+
// EchoLambda makes it easy to send API Gateway proxy events to a echo.Echo.
16+
// The library transforms the proxy event into an HTTP request and then
17+
// creates a proxy response object from the http.ResponseWriter
18+
type EchoLambda struct {
19+
core.RequestAccessor
20+
21+
Echo *echo.Echo
22+
}
23+
24+
// New creates a new instance of the EchoLambda object.
25+
// Receives an initialized *echo.Echo object - normally created with echo.New().
26+
// It returns the initialized instance of the EchoLambda object.
27+
func New(e *echo.Echo) *EchoLambda {
28+
return &EchoLambda{Echo: e}
29+
}
30+
31+
// Proxy receives an API Gateway proxy event, transforms it into an http.Request
32+
// object, and sends it to the echo.Echo for routing.
33+
// It returns a proxy response object generated from the http.ResponseWriter.
34+
func (e *EchoLambda) Proxy(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
35+
echoRequest, err := e.ProxyEventToHTTPRequest(req)
36+
return e.proxyInternal(echoRequest, err)
37+
}
38+
39+
// ProxyWithContext receives context and an API Gateway proxy event,
40+
// transforms them into an http.Request object, and sends it to the echo.Echo for routing.
41+
// It returns a proxy response object generated from the http.ResponseWriter.
42+
func (e *EchoLambda) ProxyWithContext(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
43+
echoRequest, err := e.EventToRequestWithContext(ctx, req)
44+
return e.proxyInternal(echoRequest, err)
45+
}
46+
47+
func (e *EchoLambda) proxyInternal(req *http.Request, err error) (events.APIGatewayProxyResponse, error) {
48+
49+
if err != nil {
50+
return core.GatewayTimeout(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
51+
}
52+
53+
respWriter := core.NewProxyResponseWriter()
54+
e.Echo.ServeHTTP(http.ResponseWriter(respWriter), req)
55+
56+
proxyResponse, err := respWriter.GetProxyResponse()
57+
if err != nil {
58+
return core.GatewayTimeout(), core.NewLoggedError("Error while generating proxy response: %v", err)
59+
}
60+
61+
return proxyResponse, nil
62+
}

echo/echo_suite_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package echoadapter_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestEcho(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Echo Suite")
13+
}

echo/echolambda_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package echoadapter_test
2+
3+
import (
4+
"log"
5+
6+
"github.com/aws/aws-lambda-go/events"
7+
"github.com/awslabs/aws-lambda-go-api-proxy/echo"
8+
"github.com/labstack/echo"
9+
10+
. "github.com/onsi/ginkgo"
11+
. "github.com/onsi/gomega"
12+
)
13+
14+
var _ = Describe("EchoLambda tests", func() {
15+
Context("Simple ping request", func() {
16+
It("Proxies the event correctly", func() {
17+
log.Println("Starting test")
18+
e := echo.New()
19+
e.GET("/ping", func(c echo.Context) error {
20+
log.Println("Handler!!")
21+
return c.String(200, "pong")
22+
})
23+
24+
adapter := echoadapter.New(e)
25+
26+
req := events.APIGatewayProxyRequest{
27+
Path: "/ping",
28+
HTTPMethod: "GET",
29+
}
30+
31+
resp, err := adapter.Proxy(req)
32+
33+
Expect(err).To(BeNil())
34+
Expect(resp.StatusCode).To(Equal(200))
35+
})
36+
})
37+
})

go.mod

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ require (
1616
github.com/gorilla/mux v0.0.0-20180120075819-c0091a029979
1717
github.com/json-iterator/go v0.0.0-20180128142709-bca911dae073
1818
github.com/kardianos/govendor v1.0.9 // indirect
19-
github.com/mattn/go-isatty v0.0.4
19+
github.com/labstack/echo v3.3.10+incompatible
20+
github.com/labstack/gommon v0.2.8 // indirect
21+
github.com/mattn/go-colorable v0.1.1 // indirect
22+
github.com/mattn/go-isatty v0.0.5
2023
github.com/onsi/ginkgo v0.0.0-20180119174237-747514b53ddd
2124
github.com/onsi/gomega v1.3.0
2225
github.com/pkg/errors v0.8.1 // indirect
2326
github.com/stretchr/testify v1.3.0 // indirect
2427
github.com/ugorji/go v0.0.0-20180129160544-d2b24cf3d3b4
2528
github.com/urfave/negroni v0.0.0-20180130044549-22c5532ea862
29+
github.com/valyala/fasttemplate v1.0.1 // indirect
2630
golang.org/x/net v0.0.0-20190311183353-d8887717615a
27-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
31+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223
2832
golang.org/x/text v0.3.0
2933
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c // indirect
3034
gopkg.in/go-playground/validator.v8 v8.18.2

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@ github.com/gorilla/mux v0.0.0-20180120075819-c0091a029979/go.mod h1:1lud6UwP+6or
2222
github.com/json-iterator/go v0.0.0-20180128142709-bca911dae073/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
2323
github.com/kardianos/govendor v1.0.9 h1:WOH3FcVI9eOgnIZYg96iwUwrL4eOVx+aQ66oyX2R8Yc=
2424
github.com/kardianos/govendor v1.0.9/go.mod h1:yvmR6q9ZZ7nSF5Wvh40v0wfP+3TwwL8zYQp+itoZSVM=
25+
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
26+
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
27+
github.com/labstack/gommon v0.2.8 h1:JvRqmeZcfrHC5u6uVleB4NxxNbzx6gpbJiQknDbKQu0=
28+
github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4=
29+
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
30+
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
2531
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
2632
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
33+
github.com/mattn/go-isatty v0.0.5 h1:tHXDdz1cpzGaovsTB+TVB8q90WEokoVmfMqoVcrLUgw=
34+
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
2735
github.com/onsi/ginkgo v0.0.0-20180119174237-747514b53ddd h1:b2wg8HW/u55DT7Y/vamdEn/jdvtsGkxzl+0+iHa5YmE=
2836
github.com/onsi/ginkgo v0.0.0-20180119174237-747514b53ddd/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
2937
github.com/onsi/gomega v1.3.0 h1:yPHEatyQC4jN3vdfvqJXG7O9vfC6LhaAV1NEdYpP+h0=
@@ -37,6 +45,11 @@ github.com/ugorji/go v0.0.0-20180129160544-d2b24cf3d3b4 h1:euf5tLM++W5h5uyfs6NSM
3745
github.com/ugorji/go v0.0.0-20180129160544-d2b24cf3d3b4/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
3846
github.com/urfave/negroni v0.0.0-20180130044549-22c5532ea862 h1:eg5xqGZGatsyRpVnFJkdeUWSFk46lDgkXLvOryv5ySg=
3947
github.com/urfave/negroni v0.0.0-20180130044549-22c5532ea862/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
48+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
49+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
50+
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
51+
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
52+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
4053
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4154
golang.org/x/net v0.0.0-20180124060956-0ed95abb35c4/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
4255
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
@@ -45,6 +58,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
4558
golang.org/x/sys v0.0.0-20180126165840-ff2a66f350ce/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4659
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
4760
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
61+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4862
golang.org/x/text v0.0.0-20171227012246-e19ae1496984/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
4963
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
5064
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)