假设现在有个需求,项目需要部署到aws无服务器,但是我们的项目以及是成熟项目了,总不能从头开始写吧?
所以有了github.com/awslabs/aws-lambda-go-api-proxy这个项目
只需要改造路由部分,其他框架官网有示例
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/awslabs/aws-lambda-go-api-proxy/gin"
"github.com/gin-gonic/gin"
"net/http"
"os"
)
var ginLambda *ginadapter.GinLambda
// 判断是不是lambda环境,
// 读环境变量或者配置,这是测试
func inLambda() bool {
if lambdaTaskRoot := os.Getenv("AWS_LAMBDA"); lambdaTaskRoot != "" {
return true
}
return false
}
// 设置路由
func Router() *gin.Engine {
gin.SetMode(gin.DebugMode)
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "home page"})
})
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "hello test completed successfully"})
})
return r
}
// lambda的event处理
func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return ginLambda.ProxyWithContext(ctx, req)
}
func main() {
r := Router()
if inLambda() {
fmt.Println("running in aws lambda")
ginLambda = ginadapter.New(r)
lambda.Start(Handler)
} else {
fmt.Println("running in local")
err := r.Run(":8080")
if err != nil {
panic(err)
}
}
}
标签:github,示例,aws,func,gin,com,lambda
From: https://www.cnblogs.com/qcy-blog/p/18236559