当前位置: > > > > 在 Go 中为 AWS Lambda 指定多个事件处理程序
在 Go 中为 AWS Lambda 指定多个事件处理程序
来源:stackoverflow
2024-05-01 10:00:35
0浏览
收藏
IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天米云给大家整理了《在 Go 中为 AWS Lambda 指定多个事件处理程序》,聊聊,我们一起来看看吧!
问题内容
通常,go 中的 aws lambda 事件处理程序代码(使用无服务器框架)编码为:
package main
import (
"fmt"
"context"
"github.com/aws/aws-lambda-go/lambda"
)
type myevent struct {
name string `json:"name"`
}
func handlerequest(ctx context.context, name myevent) (string, error) {
return fmt.sprintf("hello %s!", name.name ), nil
}
func main() {
lambda.start(handlerequest)
}
serverless.yml 文件包含如下部分:
skeleton-go-get:
name: skeleton-go-get
runtime: go1.x
handler: go-handler # <- this specifies a file, not a function.
events:
- http:
path: skeleton/go
method: get
^ 创建一个请求处理程序…但现在我希望我的一个 go 脚本/程序包含 http get 和 post 请求的事件处理程序,而不是每个无服务器函数使用一个 go 程序文件。
这在 node.js、ruby、python 等语言中是可能的,通过 serverless.yml 指定处理程序文件中的哪个函数将用于哪个无服务器函数。例如(对于 python 函数):
[...]
functions:
skeleton-python-get:
name: skeleton-python-get
handler: python-handler.handle_get # <- specifies the http get handler.
events:
- http:
path: skeleton/python
method: get
skeleton-python-post:
name: skeleton-python-post
handler: python-handler.handle_post # <- specifies the http post handler.
events:
- http:
path: skeleton/python
method: post
我无法让同样的技巧适用于 go。我尝试在 main() 中包含正确的请求,但无济于事:
func handlegetrequest(ctx context.context, name myevent) (string, error) {
return fmt.sprintf("hello %s!", name.name ), nil
}
func handlepostrequest(ctx context.context, name myevent) (string, error) {
return fmt.sprintf("hello %s!", name.name ), nil
}
func main() {
lambda.start(handlegetrequest)
lambda.start(handlepostrequest) // <- attempt to add another handler.
}
在 serverless.yml 文件中为 go 处理程序指定多个事件处理函数也不起作用:该函数不是处理程序声明的有效部分。
skeleton-go-get:
name: skeleton-go-get
runtime: go1.x
handler: go-handler.HandleGet # <- Attempt to specify a function.
events:
- http:
path: skeleton/go
method: get
skeleton-go-post:
name: skeleton-go-post
runtime: go1.x
handler: go-handler.HandlePost # <- Attempt to specify a function.
events:
- http:
path: skeleton/go
method: post
问:如何在一个 go 程序中包含多个 aws lambda 事件处理程序(使用无服务器框架)?
解决方案
您可以对 get 和 post 使用相同的函数(和处理程序):
skeleton-go:
name: skeleton-go
runtime: go1.x
handler: go-handler
events:
- http:
path: skeleton/go
method: get
- http:
path: skeleton/go
method: post
使用go的或使用第三方的,例如或,如下面的示例代码所示(因为这是我方便的)。本质上,您正在构建一个 go http 服务器,但是是在 lambda 中。因此,请按照设置 go web 服务器的详细信息进行操作,并查看 。
package main
import (
"context"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/go-chi/chi"
chiproxy "github.com/awslabs/aws-lambda-go-api-proxy/chi"
)
var adapter *chiproxy.chilambda
func getskeleton(w http.responsewriter, r *http.request) {
...
}
func postskeletontomom(w http.responsewriter, r *http.request) {
...
}
func init() {
r := chi.newrouter()
r.get("/skeleton/go", getskeleton)
r.post("/skeleton/go", postskeletontomom)
adapter = chiproxy.new(r)
}
func lambdahandler(ctx context.context, req events.apigatewayproxyrequest) (events.apigatewayproxyresponse, error) {
c, err := adapter.proxywithcontext(ctx, req)
return c, err
}
func main() {
lambda.start(lambdahandler)
}
您不需要在 lambda 中自行构建 go 服务器,因为您已经通过无服务器框架为您提供 api 网关…
我使用了 aws cloudformation + sam,并且使用了 http api 网关(不是 rest),但它应该以类似的方式运行…
首先…您需要将其变成 1 个 lambda 函数来处理 2 个事件,如下所示:
skeleton-go-get:
name: skeleton-go-get
runtime: go1.x
handler: go-handler # <- this specifies a file, not a function.
events:
- http:
path: skeleton/go
method: get
- http:
path: skeleton/go
method: post
在你的 lambda 中,你应该有:
package main
import ...
func getSkeleton(event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
// Return APIGateway Response
}
func postSkeleton(event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
// Return APIGateway Response
}
func handler(_ context.Context, event events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
// Log Events
eventJson, _ := json.Marshal(event)
log.Printf("EVENT: %s", string(eventJson))
switch event.RouteKey {
case "GET /skeleton/go":
return getSkeleton(event)
case "POST /skeleton/go":
return postSkeleton(event)
default:
return events.APIGatewayV2HTTPResponse{
StatusCode: 400
}, nil
}
}
func main() {
lambda.Start(handler)
}
终于介绍完啦!小伙伴们,这篇关于《在 Go 中为 AWS Lambda 指定多个事件处理程序》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~米云公众号也会发布Golang相关知识,快来关注吧!
