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

Commit a49e52c

Browse files
authored
Updated readme with example for Go Fiber Adapter
1 parent 404beb1 commit a49e52c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,55 @@ func main() {
8484
}
8585
```
8686

87+
### Fiber
88+
89+
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.
90+
91+
Declare a `fiberadapter.FiberLambda` object in the global scope, and initialize it in the `init` function, adding all API methods.
92+
93+
The `ProxyWithContext` method is then used to translate requests and responses.
94+
95+
```go
96+
// main.go
97+
package main
98+
99+
import (
100+
"context"
101+
"log"
102+
103+
"github.com/aws/aws-lambda-go/events"
104+
"github.com/aws/aws-lambda-go/lambda"
105+
fiberadapter "github.com/awslabs/aws-lambda-go-api-proxy/fiber"
106+
"github.com/gofiber/fiber/v2"
107+
)
108+
109+
var fiberLambda *fiberadapter.FiberLambda
110+
111+
// init the Fiber Server
112+
func init() {
113+
log.Printf("Fiber cold start")
114+
var app *fiber.App
115+
app = fiber.New()
116+
117+
app.Get("/", func(c *fiber.Ctx) error {
118+
return c.SendString("Hello, World!")
119+
})
120+
121+
fiberLambda = fiberadapter.New(app)
122+
}
123+
124+
// Handler will deal with Fiber working with Lambda
125+
func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
126+
// If no name is provided in the HTTP request body, throw an error
127+
return fiberLambda.ProxyWithContext(ctx, req)
128+
}
129+
130+
func main() {
131+
// Make the handler available for Remote Procedure Call by AWS Lambda
132+
lambda.Start(Handler)
133+
}
134+
```
135+
87136
## Other frameworks
88137
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.
89138

0 commit comments

Comments
 (0)