5

I'm using SAM. The following setup works:

  Function:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./code/
      Runtime: go1.x
      MemorySize: 64
      Handler: main
      Events:
        TesstApi:
          Type: Api
          Properties:
            Path: /{proxy+}
            Method: any

Now I have a main method (in go)

It looks like this:

package main

import (
    "context"
    "log"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
    ginadapter "github.com/awslabs/aws-lambda-go-api-proxy/gin"
    "github.com/gin-gonic/gin"
)

var ginLambda *ginadapter.GinLambda

func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    if ginLambda == nil {
        // stdout and stderr are sent to AWS CloudWatch Logs
        log.Printf("Gin cold start")
        r := gin.Default()
        r.GET("/", Index)
        r.GET("/test/hello", xxx)
        r.GET("/test/hi", xxx)

        ginLambda = ginadapter.New(r)
    }

    return ginLambda.ProxyWithContext(ctx, req)
}

func main() {
    lambda.Start(Handler)
}

This works fine. So I can start my local api-gateway with SAM and curl 127.0.0.1:3000/test/hello works well

Now I try to update the following sentence in my template:

  Properties:
    Path: /{proxy+}
    Method: any

to

  Properties:
    Path: /test/{proxy+}
    Method: any

I want to catch everything after the test path.

I tried the following:

    r.GET("/hello", xxx)
    r.GET("hello", xxx)
    r.GET("/test/hello", xxx)

but none of them worked.

Is it possible what I want to do? If yes, How?

mealesbia
  • 845
  • 2
  • 12
  • 28

1 Answers1

0

The reason this doesn't work is that gin receives the path from API Gateway RawPath which in your instance would include "/test". So your first router would handle the requests:

 r := gin.Default()
  r.GET("/", Index)
  r.GET("/test/hello", xxx)
  r.GET("/test/hi", xxx)

Curious if you found a solution around this, I am having this issue myself

Lucas A
  • 45
  • 6
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32704517) – user16217248 Sep 18 '22 at 04:31