如何整理 serverless.yml 文件?

How do I de-clutter my serverless.yml file?

我目前正在从事无服务器后端项目。当我不断向我的 serverless.yml 文件中添加函数时,我不禁想知道,肯定有更好的方法在其他单独的 .yaml 文件中编写函数配置,然后在 serverless.yml 中引用它们。不幸的是,我不知道该怎么做。

我的项目结构是这样的:

我的职能是:

functions:
  create:   
    handler: users/create.create
    events:
      - http:
          path: users/create
          method: post
          cors: true

  login:   
    handler: users/login.login
    events:
      - http:
          path: users/login
          method: post
          cors: true

  list:
    handler: users/list.list
    events:
      - http:
          path: users
          method: get
          cors: true

  get:
    handler: users/get.get
    events:
      - http:
          path: users/{id}
          method: get
          cors: true

  update:
    handler: users/update.update
    events:
      - http:
          path: users/{id}
          method: put
          cors: true

  delete:
    handler: users/delete.delete
    events:
      - http:
          path: users/{id}
          method: delete
          cors: true

  create_addr:
    handler: address/create.create
    events:
      - http:
          path: address
          method: post
          cors: true

  delete_addr:
    handler: address/delete.delete
    events:
      - http:
          path: address/{id}
          method: delete
          cors: true

  get_addr:
    handler: address/get.get
    events:
      - http:
          path: address/{id}
          method: get
          cors: true

  update_addr:
    handler: address/update.update
    events:
      - http:
          path: address/{id}
          method: put
          cors: true


  create_order:
    handler: order/create.create
    events:
      - http:
          path: order
          method: post
          cors: true

  delete_order:
    handler: order/delete.delete
    events:
      - http:
          path: order/{id}
          method: delete
          cors: true

  get_order:
    handler: order/get.get
    events:
      - http:
          path: order/{id}
          method: get
          cors: true

我该如何继续这件事?

根据the docs,你可以这样做:

# serverless.yml
functions:
  create: ${file(../create.yml)}
  login: ${file(../login.yml)}
  list: ${file(../list.yml)}
  get: ${file(../get.yml)}
  # ...
# create.yml
handler: users/create.create
events:
  - http:
      path: users/create
      method: post
      cors: true

以此类推