我想通过一个lambda函数调用代理来自root的所有HTTP请求.

无服务器.yml代码段

functions:
  hello:
    handler: bin/hello
    url: true
    events:
      - httpApi:
#          path: /{proxy+}
          path: /{any+}
          method: get

main.go


import (
    "fmt"
    "github.com/apex/gateway"
    "github.com/gin-gonic/gin"
    "log"
    "net/http"
    "os"
)

func inLambda() bool {
    if lambdaTaskRoot := os.Getenv("LAMBDA_TASK_ROOT"); lambdaTaskRoot != "" {
        return true
    }
    return false
}

func setupRouter() *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("/hello-world", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{"message": "hello test completed successfully"})
    })

    return r
}

func main() {
    if inLambda() {
        fmt.Println("running aws lambda in aws")
        log.Fatal(gateway.ListenAndServe(":8080", setupRouter()))
    } else {
        fmt.Println("running aws lambda in local")
        log.Fatal(http.ListenAndServe(":8080", setupRouter()))
    }
}

推荐答案

path: /{proxy+}是正确的.

确保您使用的是正确的apex版本.apex README个国家表示,您需要将apex v2用于HTTP API.您似乎正在使用仅支持RESTAPI的v1.

Go相关问答推荐

获取作为类型参数传递给方法接收方中的类型参数的切片的基础类型

GORM Find方法中缺少字段

GO框架Echo中间件的使用

如何使用中间件更改http请求的响应代码?

如何模拟go的Elastic search SDK?

在 go 中,将接收器 struct 从值更改为指针是否向后兼容?

Go 中的 protobuf FieldMask 解组

Golang text/template中的startswith函数 - 入门教程

gopacket:IP-in-IP 数据包上的解码层

我的神经网络(从头开始)训练,让它离目标更远

如何使用 go 包设置标头键和值:shurcooL/graphql 或 hasura/go-graphql-client?

有没有办法计算枚举中定义的项目总数?

致命错误 - 所有 Goroutines 都睡着了!僵局

我在 go 中制作的递归函数有什么问题?

将 .gz 文件添加到 tar.gz 文件,但在添加之前解码 gz.输出文件被剪辑(损坏)

Go 泛型:自引用接口约束

如何在Golang中的差异函数中杀死命令Exec

如何在循环中旋转图像以便在 golang 中创建 GIF?

将 Simple Go Web 应用程序部署到 Elastic Beanstalk

有没有办法在一个 goroutine 返回后延迟后取消上下文?