From f0bff6241acacb433a26bbe814fe597f2887106d Mon Sep 17 00:00:00 2001 From: Bhavik Shah Date: Thu, 26 May 2022 20:26:57 +0530 Subject: [PATCH 1/7] Created an example for go fiber adapter --- examples/main.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/main.go diff --git a/examples/main.go b/examples/main.go new file mode 100644 index 0000000..1b641b0 --- /dev/null +++ b/examples/main.go @@ -0,0 +1,38 @@ +// main.go +package main + +import ( + "context" + "log" + + "github.com/aws/aws-lambda-go/events" + "github.com/aws/aws-lambda-go/lambda" + fiberadapter "github.com/awslabs/aws-lambda-go-api-proxy/fiber" + "github.com/gofiber/fiber/v2" +) + +var fiberLambda *fiberadapter.FiberLambda + +// init the Fiber Server +func init() { + log.Printf("Fiber cold start") + var app *fiber.App + app = fiber.New() + + app.Get("/", func(c *fiber.Ctx) error { + return c.SendString("Hello, World!") + }) + + fiberLambda = fiberadapter.New(app) +} + +// Handler will deal with Gin working with Lambda +func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { + // If no name is provided in the HTTP request body, throw an error + return fiberLambda.ProxyWithContext(ctx, req) +} + +func main() { + // Make the handler available for Remote Procedure Call by AWS Lambda + lambda.Start(Handler) +} From 3cc64100cdb76288e8d8550e5971e9585b59e7c6 Mon Sep 17 00:00:00 2001 From: Bhavik Shah Date: Thu, 26 May 2022 20:28:42 +0530 Subject: [PATCH 2/7] Created example for fiber adapter --- examples/fiber/main.go | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 examples/fiber/main.go diff --git a/examples/fiber/main.go b/examples/fiber/main.go new file mode 100644 index 0000000..93c4a57 --- /dev/null +++ b/examples/fiber/main.go @@ -0,0 +1,50 @@ +// main.go +package main + +import ( + "context" + "log" + + "github.com/aws/aws-lambda-go/events" + "github.com/aws/aws-lambda-go/lambda" + fiberadapter "github.com/awslabs/aws-lambda-go-api-proxy/fiber" + "github.com/gofiber/fiber/v2" +) + +var runOnAwsLambda = false +var fiberLambda *fiberadapter.FiberLambda +var app *fiber.App + +// init the Fiber Server +func init() { + log.Printf("Fiber cold start") + + app = fiber.New() + + app.Get("/", func(c *fiber.Ctx) error { + return c.SendString("Hello, World!") + }) + + if runOnAwsLambda { + log.Printf("Fiber Addapter New") + fiberLambda = fiberadapter.New(app) + } +} + +// Handler will deal with Fiber working with Lambda +func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { + // If no name is provided in the HTTP request body, throw an error + return fiberLambda.ProxyWithContext(ctx, req) +} + +func main() { + + if runOnAwsLambda { + // Make the handler available for Remote Procedure Call by AWS Lambda + log.Printf("Fiber Adapter Lambda Handler") + lambda.Start(Handler) + } else { + log.Printf("Fiber Listen Handler") + log.Fatal(app.Listen(":3000")) + } +} From ec023b6acaa8bdfdef8abd6723ad752aee23297a Mon Sep 17 00:00:00 2001 From: Bhavik Shah Date: Thu, 26 May 2022 20:29:44 +0530 Subject: [PATCH 3/7] Enabled AWS Lambda deployment as default. --- examples/fiber/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/fiber/main.go b/examples/fiber/main.go index 93c4a57..f0e61ae 100644 --- a/examples/fiber/main.go +++ b/examples/fiber/main.go @@ -11,7 +11,7 @@ import ( "github.com/gofiber/fiber/v2" ) -var runOnAwsLambda = false +var runOnAwsLambda = true var fiberLambda *fiberadapter.FiberLambda var app *fiber.App From 755fecdefd59264c52a804a10e0897dfa99b7ccc Mon Sep 17 00:00:00 2001 From: Bhavik Shah Date: Thu, 26 May 2022 20:33:40 +0530 Subject: [PATCH 4/7] Delete main.go --- examples/main.go | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 examples/main.go diff --git a/examples/main.go b/examples/main.go deleted file mode 100644 index 1b641b0..0000000 --- a/examples/main.go +++ /dev/null @@ -1,38 +0,0 @@ -// main.go -package main - -import ( - "context" - "log" - - "github.com/aws/aws-lambda-go/events" - "github.com/aws/aws-lambda-go/lambda" - fiberadapter "github.com/awslabs/aws-lambda-go-api-proxy/fiber" - "github.com/gofiber/fiber/v2" -) - -var fiberLambda *fiberadapter.FiberLambda - -// init the Fiber Server -func init() { - log.Printf("Fiber cold start") - var app *fiber.App - app = fiber.New() - - app.Get("/", func(c *fiber.Ctx) error { - return c.SendString("Hello, World!") - }) - - fiberLambda = fiberadapter.New(app) -} - -// Handler will deal with Gin working with Lambda -func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { - // If no name is provided in the HTTP request body, throw an error - return fiberLambda.ProxyWithContext(ctx, req) -} - -func main() { - // Make the handler available for Remote Procedure Call by AWS Lambda - lambda.Start(Handler) -} From 404beb10212780e9a5644bc762b092313b202d73 Mon Sep 17 00:00:00 2001 From: Bhavik Shah Date: Thu, 26 May 2022 20:34:29 +0530 Subject: [PATCH 5/7] Updated example to remove local execution context --- examples/fiber/main.go | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/examples/fiber/main.go b/examples/fiber/main.go index f0e61ae..a3d1b22 100644 --- a/examples/fiber/main.go +++ b/examples/fiber/main.go @@ -11,24 +11,19 @@ import ( "github.com/gofiber/fiber/v2" ) -var runOnAwsLambda = true var fiberLambda *fiberadapter.FiberLambda -var app *fiber.App // init the Fiber Server func init() { log.Printf("Fiber cold start") - + var app *fiber.App app = fiber.New() app.Get("/", func(c *fiber.Ctx) error { return c.SendString("Hello, World!") }) - if runOnAwsLambda { - log.Printf("Fiber Addapter New") - fiberLambda = fiberadapter.New(app) - } + fiberLambda = fiberadapter.New(app) } // Handler will deal with Fiber working with Lambda @@ -38,13 +33,6 @@ func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.API } func main() { - - if runOnAwsLambda { - // Make the handler available for Remote Procedure Call by AWS Lambda - log.Printf("Fiber Adapter Lambda Handler") - lambda.Start(Handler) - } else { - log.Printf("Fiber Listen Handler") - log.Fatal(app.Listen(":3000")) - } + // Make the handler available for Remote Procedure Call by AWS Lambda + lambda.Start(Handler) } From a49e52c3f08ee3b9c0e27dfff11edf28fa9c9a26 Mon Sep 17 00:00:00 2001 From: Bhavik Shah Date: Thu, 26 May 2022 20:40:36 +0530 Subject: [PATCH 6/7] Updated readme with example for Go Fiber Adapter --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/README.md b/README.md index 8013550..207351b 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,55 @@ func main() { } ``` +### Fiber + +To use with the Fiber framework, following the instructions from the [Lambda documentation](https://docs.aws.amazon.com/lambda/latest/dg/go-programming-model-handler-types.html), declare a `Handler` method for the main package. + +Declare a `fiberadapter.FiberLambda` object in the global scope, and initialize it in the `init` function, adding all API methods. + +The `ProxyWithContext` method is then used to translate requests and responses. + +```go +// main.go +package main + +import ( + "context" + "log" + + "github.com/aws/aws-lambda-go/events" + "github.com/aws/aws-lambda-go/lambda" + fiberadapter "github.com/awslabs/aws-lambda-go-api-proxy/fiber" + "github.com/gofiber/fiber/v2" +) + +var fiberLambda *fiberadapter.FiberLambda + +// init the Fiber Server +func init() { + log.Printf("Fiber cold start") + var app *fiber.App + app = fiber.New() + + app.Get("/", func(c *fiber.Ctx) error { + return c.SendString("Hello, World!") + }) + + fiberLambda = fiberadapter.New(app) +} + +// Handler will deal with Fiber working with Lambda +func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { + // If no name is provided in the HTTP request body, throw an error + return fiberLambda.ProxyWithContext(ctx, req) +} + +func main() { + // Make the handler available for Remote Procedure Call by AWS Lambda + lambda.Start(Handler) +} +``` + ## Other frameworks This package also supports [Negroni](https://github.com/urfave/negroni), [GorillaMux](https://github.com/gorilla/mux), and plain old `HandlerFunc` - take a look at the code in their respective sub-directories. All packages implement the `Proxy` method exactly like our Gin sample above. From 06211730ed598a854ed876d3520fcc0ad73997e7 Mon Sep 17 00:00:00 2001 From: "Bhavik Shah (DCP)" Date: Fri, 10 Jun 2022 15:14:10 +0530 Subject: [PATCH 7/7] Fixed Failing Test Cases for Go Fiber for Travis CI builds. --- fiber/fiberlambda_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fiber/fiberlambda_test.go b/fiber/fiberlambda_test.go index 6ea2199..28d47ca 100644 --- a/fiber/fiberlambda_test.go +++ b/fiber/fiberlambda_test.go @@ -54,7 +54,7 @@ var _ = Describe("FiberLambda tests", func() { Expect(c.Get(fiber.HeaderContentLength)).To(Equal("77")) Expect(c.Get(fiber.HeaderConnection)).To(Equal("Keep-Alive")) Expect(c.Get(fiber.HeaderKeepAlive)).To(Equal("timeout=5, max=1000")) - Expect(c.Get(fiber.HeaderTransferEncoding)).To(Equal("gzip")) + Expect(c.Get(fiber.HeaderTransferEncoding)).To(Equal("")) return c.Status(fiber.StatusNoContent).Send(nil) }) @@ -209,7 +209,7 @@ var _ = Describe("FiberLambda tests", func() { Expect(c.Get(fiber.HeaderContentLength)).To(Equal("77")) Expect(c.Get(fiber.HeaderConnection)).To(Equal("Keep-Alive")) Expect(c.Get(fiber.HeaderKeepAlive)).To(Equal("timeout=5, max=1000")) - Expect(c.Get(fiber.HeaderTransferEncoding)).To(Equal("gzip")) + Expect(c.Get(fiber.HeaderTransferEncoding)).To(Equal("")) return c.Status(fiber.StatusNoContent).Send(nil) })