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

Commit 10d7bab

Browse files
committed
Adding code to handle ALB events as well as API gateway events
1 parent 3d7a231 commit 10d7bab

File tree

10 files changed

+456
-0
lines changed

10 files changed

+456
-0
lines changed

echo/adapterALB.go

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

echo/echolambda_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,30 @@ var _ = Describe("EchoLambdaV2 tests", func() {
6464
})
6565
})
6666
})
67+
68+
var _ = Describe("EchoLambdaALB tests", func() {
69+
Context("Simple ping request", func() {
70+
It("Proxies the event correctly", func() {
71+
log.Println("Starting test")
72+
e := echo.New()
73+
e.GET("/ping", func(c echo.Context) error {
74+
log.Println("Handler!!")
75+
return c.String(200, "pong")
76+
})
77+
78+
adapter := echoadapter.NewALB(e)
79+
80+
req := events.ALBTargetGroupRequest{
81+
HTTPMethod: "GET",
82+
Path: "/ping",
83+
RequestContext: events.ALBTargetGroupRequestContext{
84+
ELB: events.ELBContext{TargetGroupArn: " ad"},
85+
}}
86+
87+
resp, err := adapter.Proxy(req)
88+
89+
Expect(err).To(BeNil())
90+
Expect(resp.StatusCode).To(Equal(200))
91+
})
92+
})
93+
})

gin/adapterALB.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Package ginadapter adds Gin support for the aws-severless-go-api library.
2+
// Uses the core package behind the scenes and exposes the New and NewV2 and ALB methods to
3+
// get a new instance and Proxy method to send request to the Gin engine.
4+
package ginadapter
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/gin-gonic/gin"
13+
)
14+
15+
// GinLambdaALB makes it easy to send ALB proxy events to a Gin
16+
// Engine. The library transforms the proxy event into an HTTP request and then
17+
// creates a proxy response object from the http.ResponseWriter
18+
type GinLambdaALB struct {
19+
core.RequestAccessorALB
20+
21+
ginEngine *gin.Engine
22+
}
23+
24+
// New creates a new instance of the GinLambdaALB object.
25+
// Receives an initialized *gin.Engine object - normally created with gin.Default().
26+
// It returns the initialized instance of the GinLambdaALB object.
27+
func NewALB(gin *gin.Engine) *GinLambdaALB {
28+
return &GinLambdaALB{ginEngine: gin}
29+
}
30+
31+
// Proxy receives an ALB proxy event, transforms it into an http.Request
32+
// object, and sends it to the gin.Engine for routing.
33+
// It returns a proxy response object generated from the http.ResponseWriter.
34+
func (g *GinLambdaALB) Proxy(req events.ALBTargetGroupRequest) (events.ALBTargetGroupResponse, error) {
35+
ginRequest, err := g.ProxyEventToHTTPRequest(req)
36+
return g.proxyInternal(ginRequest, err)
37+
}
38+
39+
// ProxyWithContext receives context and an ALB proxy event,
40+
// transforms them into an http.Request object, and sends it to the gin.Engine for routing.
41+
// It returns a proxy response object generated from the http.ResponseWriter.
42+
func (g *GinLambdaALB) ProxyWithContext(ctx context.Context, req events.ALBTargetGroupRequest) (events.ALBTargetGroupResponse, error) {
43+
ginRequest, err := g.EventToRequestWithContext(ctx, req)
44+
return g.proxyInternal(ginRequest, err)
45+
}
46+
47+
func (g *GinLambdaALB) proxyInternal(req *http.Request, err error) (events.ALBTargetGroupResponse, error) {
48+
49+
if err != nil {
50+
return core.GatewayTimeoutALB(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
51+
}
52+
53+
respWriter := core.NewProxyResponseWriterALB()
54+
g.ginEngine.ServeHTTP(http.ResponseWriter(respWriter), req)
55+
56+
proxyResponse, err := respWriter.GetProxyResponse()
57+
if err != nil {
58+
return core.GatewayTimeoutALB(), core.NewLoggedError("Error while generating proxy response: %v", err)
59+
}
60+
61+
return proxyResponse, nil
62+
}

gin/ginlambda_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,37 @@ var _ = Describe("GinLambdaV2 tests", func() {
7979
})
8080
})
8181
})
82+
83+
var _ = Describe("GinLambdaALB tests", func() {
84+
Context("Simple ping request", func() {
85+
It("Proxies the event correctly", func() {
86+
log.Println("Starting test")
87+
r := gin.Default()
88+
r.GET("/ping", func(c *gin.Context) {
89+
log.Println("Handler!!")
90+
c.JSON(200, gin.H{
91+
"message": "pong",
92+
})
93+
})
94+
95+
adapter := ginadapter.NewALB(r)
96+
97+
req := events.ALBTargetGroupRequest{
98+
HTTPMethod: "GET",
99+
Path: "/ping",
100+
RequestContext: events.ALBTargetGroupRequestContext{
101+
ELB: events.ELBContext{TargetGroupArn: " ad"},
102+
}}
103+
104+
resp, err := adapter.Proxy(req)
105+
106+
Expect(err).To(BeNil())
107+
Expect(resp.StatusCode).To(Equal(200))
108+
109+
resp, err = adapter.Proxy(req)
110+
111+
Expect(err).To(BeNil())
112+
Expect(resp.StatusCode).To(Equal(200))
113+
})
114+
})
115+
})

gorillamux/adapterALB.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package gorillamux
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/aws/aws-lambda-go/events"
8+
"github.com/awslabs/aws-lambda-go-api-proxy/core"
9+
"github.com/gorilla/mux"
10+
)
11+
12+
type GorillaMuxAdapterALB struct {
13+
core.RequestAccessorALB
14+
router *mux.Router
15+
}
16+
17+
func NewALB(router *mux.Router) *GorillaMuxAdapterALB {
18+
return &GorillaMuxAdapterALB{
19+
router: router,
20+
}
21+
}
22+
23+
// Proxy receives an API Gateway proxy event, transforms it into an http.Request
24+
// object, and sends it to the mux.Router for routing.
25+
// It returns a proxy response object generated from the http.ResponseWriter.
26+
func (h *GorillaMuxAdapterALB) Proxy(event events.ALBTargetGroupRequest) (events.ALBTargetGroupResponse, error) {
27+
req, err := h.ProxyEventToHTTPRequest(event)
28+
return h.proxyInternal(req, err)
29+
}
30+
31+
// ProxyWithContext receives context and an API Gateway proxy event,
32+
// transforms them into an http.Request object, and sends it to the mux.Router for routing.
33+
// It returns a proxy response object generated from the http.ResponseWriter.
34+
func (h *GorillaMuxAdapterALB) ProxyWithContext(ctx context.Context, event events.ALBTargetGroupRequest) (events.ALBTargetGroupResponse, error) {
35+
req, err := h.EventToRequestWithContext(ctx, event)
36+
return h.proxyInternal(req, err)
37+
}
38+
39+
func (h *GorillaMuxAdapterALB) proxyInternal(req *http.Request, err error) (events.ALBTargetGroupResponse, error) {
40+
if err != nil {
41+
return core.GatewayTimeoutALB(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
42+
}
43+
44+
w := core.NewProxyResponseWriterALB()
45+
h.router.ServeHTTP(http.ResponseWriter(w), req)
46+
47+
resp, err := w.GetProxyResponse()
48+
if err != nil {
49+
return core.GatewayTimeoutALB(), core.NewLoggedError("Error while generating proxy response: %v", err)
50+
}
51+
52+
return resp, nil
53+
}

gorillamux/adapterALB_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package gorillamux_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/aws/aws-lambda-go/events"
9+
"github.com/awslabs/aws-lambda-go-api-proxy/gorillamux"
10+
"github.com/gorilla/mux"
11+
12+
. "github.com/onsi/ginkgo"
13+
. "github.com/onsi/gomega"
14+
)
15+
16+
var _ = Describe("GorillaMuxAdapterALB tests", func() {
17+
Context("Simple ping request", func() {
18+
It("Proxies the event correctly", func() {
19+
homeHandler := func(w http.ResponseWriter, req *http.Request) {
20+
w.Header().Add("unfortunately-required-header", "")
21+
fmt.Fprintf(w, "Home Page")
22+
}
23+
24+
productsHandler := func(w http.ResponseWriter, req *http.Request) {
25+
w.Header().Add("unfortunately-required-header", "")
26+
fmt.Fprintf(w, "Products Page")
27+
}
28+
29+
r := mux.NewRouter()
30+
r.HandleFunc("/", homeHandler)
31+
r.HandleFunc("/products", productsHandler)
32+
33+
adapter := gorillamux.NewALB(r)
34+
35+
homePageReq := events.ALBTargetGroupRequest{
36+
HTTPMethod: http.MethodGet,
37+
Path: "/",
38+
RequestContext: events.ALBTargetGroupRequestContext{
39+
ELB: events.ELBContext{TargetGroupArn: " ad"},
40+
}}
41+
42+
homePageResp, homePageReqErr := adapter.ProxyWithContext(context.Background(), homePageReq)
43+
44+
Expect(homePageReqErr).To(BeNil())
45+
Expect(homePageResp.StatusCode).To(Equal(200))
46+
Expect(homePageResp.Body).To(Equal("Home Page"))
47+
48+
productsPageReq := events.ALBTargetGroupRequest{
49+
HTTPMethod: http.MethodGet,
50+
Path: "/products",
51+
RequestContext: events.ALBTargetGroupRequestContext{
52+
ELB: events.ELBContext{TargetGroupArn: " ad"},
53+
}}
54+
55+
productsPageResp, productsPageReqErr := adapter.Proxy(productsPageReq)
56+
57+
Expect(productsPageReqErr).To(BeNil())
58+
Expect(productsPageResp.StatusCode).To(Equal(200))
59+
Expect(productsPageResp.Body).To(Equal("Products Page"))
60+
})
61+
})
62+
})

handlerfunc/adapterALB.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package handlerfunc
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"
7+
)
8+
9+
type HandlerFuncAdapterALB = httpadapter.HandlerAdapterALB
10+
11+
func NewALB(handlerFunc http.HandlerFunc) *HandlerFuncAdapterALB {
12+
return httpadapter.NewALB(handlerFunc)
13+
}

handlerfunc/adapterALB_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package handlerfunc_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
9+
"github.com/aws/aws-lambda-go/events"
10+
"github.com/awslabs/aws-lambda-go-api-proxy/handlerfunc"
11+
12+
. "github.com/onsi/ginkgo"
13+
. "github.com/onsi/gomega"
14+
)
15+
16+
var _ = Describe("HandlerFuncAdapter ALB tests", func() {
17+
Context("Simple ping request", func() {
18+
It("Proxies the event correctly", func() {
19+
log.Println("Starting test")
20+
21+
handler := func(w http.ResponseWriter, req *http.Request) {
22+
w.Header().Add("unfortunately-required-header", "")
23+
fmt.Fprintf(w, "Go Lambda!!")
24+
}
25+
26+
adapter := handlerfunc.NewALB(handler)
27+
28+
req := events.ALBTargetGroupRequest{
29+
HTTPMethod: http.MethodGet,
30+
Path: "/",
31+
RequestContext: events.ALBTargetGroupRequestContext{
32+
ELB: events.ELBContext{TargetGroupArn: " ad"},
33+
}}
34+
35+
resp, err := adapter.ProxyWithContext(context.Background(), req)
36+
37+
Expect(err).To(BeNil())
38+
Expect(resp.StatusCode).To(Equal(200))
39+
40+
resp, err = adapter.Proxy(req)
41+
42+
Expect(err).To(BeNil())
43+
Expect(resp.StatusCode).To(Equal(200))
44+
})
45+
})
46+
})

0 commit comments

Comments
 (0)