【问题标题】:Serverless architecture services pattern path parameters无服务器架构服务模式路径参数
【发布时间】:2018-11-12 23:16:48
【问题描述】:

我正在阅读来自 Serverless 的这篇博文,内容是关于无服务器架构存在的不同模式。

我对@9​​87654322@ 很感兴趣,我想试试看。

在我的serverless.yml 文件中使用此配置。

functions:
  apps:
    handler: handler.apps
    events:
      - http: post apps
        cors: true
      - http: patch users
      - http: get users
        cors: true
      - http: delete users

以下输出来自运行serverless deploy。

POST - https://x7lpwa04.execute-api.us-west-2.amazonaws.com/staging/users
PATCH - https://x7lpwa04.execute-api.us-west-2.amazonaws.com/staging/users
GET - https://x7lpwa04.execute-api.us-west-2.amazonaws.com/staging/users
DELETE - https://x7lpwa04.execute-api.us-west-2.amazonaws.com/staging/users

现在在 CRUD 服务中,如果我想获取单个资源,我可能会为 get 端点 /staging/users/{id} 提供类似的东西。使用上述模式,是否由用户来传递像 /staging/users?id={id} 这样的查询字符串参数,而不是像 /staging/users/{id} 这样的路径参数?是否可以让端点有路径参数?

路径好像不能这样覆盖。

【问题讨论】:

    标签: web serverless-framework serverless


    【解决方案1】:

    您已经可以使用路径参数,例如:

    {
      "myFunction": {
        "handler": "src/myFunction/index.index",
        "description": "Does awesome things",
        "events": [
          {
            "http": {
              "path": "apiPath/{parameterOne}",
              "method": "GET",
              "integration": "lambda",
              "request": {
                "parameters": {
                  "parameterOne": true
                },
                "template": {
                  "application/json": "{ \"parameterOne\": \"$input.params(\"parameterOne\")\" }"
                }
              },
              "response": {
                "statusCodes": {
                  "200": {
                    "pattern": ""
                  },
                  "400": {
                    "pattern": "[\\s\\S]*\\[400\\][\\s\\S]*",
                    "template": "$input.path('$.errorMessage')"
                  },
                  "500": {
                    "pattern": "[\\s\\S]*(Process\\s?exited\\s?before\\s?completing\\s?request|\\[500\\])[\\s\\S]*",
                    "template": "$input.path('$.errorMessage')"
                  }
                },
                "headers": {
                  "Cache-Control": "'no-cache, no-store'",
                  "Pragma": "'no-cache'",
                  "Expires": "'0'",
                  "Strict-Transport-Security": "'max-age=31536000; includeSubdomains; preload'"
                }
              },
              "cors": {
                "origin": "*",
                "headers": [
                  "Content-Type",
                  "Pragma",
                  "Cache-Control",
                  "X-Amz-Date",
                  "Authorization",
                  "X-Api-Key",
                  "X-Amz-Security-Token",
                  "X-Amz-User-Agent"
                ],
                "allowCredentials": false
              }
            }
          }
        ]
      }
    }
    

    参数parameterOne将被映射到Lambda事件中。

    【讨论】:

      【解决方案2】:

      我可能解释错了,但至少在 aws 的情况下,您可以将多个资源添加到 lambda 中。您将负责处理正确的行为,您可以通过解析将填充路径参数的事件(以防它们存在)来完成。

      如博文所述

      您可以通过解析代码中的事件主体来检查传入的 HTTP 请求的路径和方法,然后执行正确的操作作为响应。这就像在您的 Lambda 代码的开头有一个小型路由器。

      您可以使用

      指定更完整的事件
        - http:
            path: users/{id}
            method: delete
      

      【讨论】:

        猜你喜欢
        • 2020-04-25
        • 2016-12-30
        • 2021-04-21
        • 1970-01-01
        • 2020-09-08
        • 1970-01-01
        • 1970-01-01
        • 2011-10-02
        相关资源
        最近更新 更多