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

Commit c6ec0ed

Browse files
authored
Merge pull request awslabs#12 from mborromeo/chi
Adding chi support
2 parents 5aaec67 + 65ea911 commit c6ec0ed

4 files changed

Lines changed: 106 additions & 0 deletions

File tree

chi/adapter.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Packge chilambda add Chi 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 Chi mux.
4+
package chiadapter
5+
6+
import (
7+
"net/http"
8+
9+
"github.com/aws/aws-lambda-go/events"
10+
"github.com/awslabs/aws-lambda-go-api-proxy/core"
11+
"github.com/go-chi/chi"
12+
)
13+
14+
// ChiLambda makes it easy to send API Gateway proxy events to a Chi
15+
// Mux. The library transforms the proxy event into an HTTP request and then
16+
// creates a proxy response object from the http.ResponseWriter
17+
type ChiLambda struct {
18+
core.RequestAccessor
19+
20+
chiMux *chi.Mux
21+
}
22+
23+
// New creates a new instance of the ChiLambda object.
24+
// Receives an initialized *chi.Mux object - normally created with chi.NewRouter().
25+
// It returns the initialized instance of the ChiLambda object.
26+
func New(chi *chi.Mux) *ChiLambda {
27+
return &ChiLambda{chiMux: chi}
28+
}
29+
30+
// Proxy receives an API Gateway proxy event, transforms it into an http.Request
31+
// object, and sends it to the chi.Mux for routing.
32+
// It returns a proxy response object gneerated from the http.ResponseWriter.
33+
func (g *ChiLambda) Proxy(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
34+
chiRequest, err := g.ProxyEventToHTTPRequest(req)
35+
36+
if err != nil {
37+
return core.GatewayTimeout(), core.NewLoggedError("Could not convert proxy event to request: %v", err)
38+
}
39+
40+
respWriter := core.NewProxyResponseWriter()
41+
g.chiMux.ServeHTTP(http.ResponseWriter(respWriter), chiRequest)
42+
43+
proxyResponse, err := respWriter.GetProxyResponse()
44+
if err != nil {
45+
return core.GatewayTimeout(), core.NewLoggedError("Error while generating proxy response: %v", err)
46+
}
47+
48+
return proxyResponse, nil
49+
}

chi/chi_suite_test.go

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

chi/chilambda_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package chiadapter_test
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/aws/aws-lambda-go/events"
8+
"github.com/awslabs/aws-lambda-go-api-proxy/chi"
9+
"github.com/go-chi/chi"
10+
11+
. "github.com/onsi/ginkgo"
12+
. "github.com/onsi/gomega"
13+
)
14+
15+
var _ = Describe("ChiLambda tests", func() {
16+
Context("Simple ping request", func() {
17+
It("Proxies the event correctly", func() {
18+
log.Println("Starting test")
19+
20+
r := chi.NewRouter()
21+
r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
22+
w.Write([]byte("pong"))
23+
})
24+
25+
adapter := chiadapter.New(r)
26+
27+
req := events.APIGatewayProxyRequest{
28+
Path: "/ping",
29+
HTTPMethod: "GET",
30+
}
31+
32+
resp, err := adapter.Proxy(req)
33+
34+
Expect(err).To(BeNil())
35+
Expect(resp.StatusCode).To(Equal(200))
36+
})
37+
})
38+
})

vendor/vendor.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656
"revision": "783c7ee9c14eac0e65b501664b4f553291556b43",
5757
"revisionTime": "2018-01-26T03:46:11Z"
5858
},
59+
{
60+
"checksumSHA1": "YODbzvhUr0Tzp2/MkXflXph2hdY=",
61+
"path": "github.com/go-chi/chi",
62+
"revision": "e223a795a06a5598451cf022558c17c779f5a7ab",
63+
"revisionTime": "2018-02-02T19:41:35Z"
64+
},
5965
{
6066
"checksumSHA1": "WX1+2gktHcBmE9MGwFSGs7oqexU=",
6167
"path": "github.com/golang/protobuf/proto",

0 commit comments

Comments
 (0)