使用Golang Gin与AWS Lambda和Serverless代理路径。

huangapple go评论97阅读模式
英文:

Using Golang Gin with AWS Lambda and Serverless with proxy path

问题

我想通过一个单一的 Lambda 函数调用来代理所有的根目录 HTTP 请求。
我尝试在 serverless.yml 中将 /{proxy+} 设置为路径。但是当我部署它时,访问根目录时出现了 "This page isn't redirecting properly" 的错误。

serverless.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()))
	}
}
英文:

I want to proxy all my HTTP requests from root through a single lambda function call.
I tried setting /{proxy+} as the path in my serverless.yml. But when I deploy it I get "This page isn't redirecting properly" when visiting the root.

serverless.yml snippet

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()))
	}
}

答案1

得分: 1

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

请确保您使用的是正确的 Apex 版本。Apex README 指出您需要使用 Apex v2 来进行 HTTP API。您似乎正在使用仅支持 REST API 的 v1 版本。

英文:

path: /{proxy+} is correct.

Make sure you are using correct apex version. apex README states that you need to use apex v2 for HTTP API. You seem to be using v1 which supports REST API only.

huangapple
  • 本文由 发表于 2022年7月27日 04:37:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/73129484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定