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

Commit eb4965e

Browse files
authored
Merge pull request #131 from bhavik1st/master
Create a working example for Go Fiber Adapter
2 parents 3b512ab + a5b661f commit eb4965e

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-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

examples/fiber/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// main.go
2+
package main
3+
4+
import (
5+
"context"
6+
"log"
7+
8+
"github.com/aws/aws-lambda-go/events"
9+
"github.com/aws/aws-lambda-go/lambda"
10+
fiberadapter "github.com/awslabs/aws-lambda-go-api-proxy/fiber"
11+
"github.com/gofiber/fiber/v2"
12+
)
13+
14+
var fiberLambda *fiberadapter.FiberLambda
15+
16+
// init the Fiber Server
17+
func init() {
18+
log.Printf("Fiber cold start")
19+
var app *fiber.App
20+
app = fiber.New()
21+
22+
app.Get("/", func(c *fiber.Ctx) error {
23+
return c.SendString("Hello, World!")
24+
})
25+
26+
fiberLambda = fiberadapter.New(app)
27+
}
28+
29+
// Handler will deal with Fiber working with Lambda
30+
func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
31+
// If no name is provided in the HTTP request body, throw an error
32+
return fiberLambda.ProxyWithContext(ctx, req)
33+
}
34+
35+
func main() {
36+
// Make the handler available for Remote Procedure Call by AWS Lambda
37+
lambda.Start(Handler)
38+
}

0 commit comments

Comments
 (0)