Welcome to the new Golem Cloud Docs! 👋
Update API definitions and deployments

Update API definitions and deployments

We will update the API definition by adding more routes to the existing API definition.

# Schema for IDEA:
# $schema: https://schema.golem.cloud/app/golem/1.2.2-dev.1/golem.schema.json
# Schema for vscode-yaml
# yaml-language-server: $schema=https://schema.golem.cloud/app/golem/1.2.2-dev.1/golem.schema.json
 
# See https://learn.golem.cloud/docs/app-manifest#field-reference for field reference
 
components:
  amazon:shopping-cart:
    template: rust
 
httpApi:
  definitions:
    shopping-cart:
      version: '0.0.2'
      routes:
        - method: POST
          path: /v1/{user}/add-to-cart
          binding:
            type: default
            componentName: "amazon:shopping-cart"
            response: |
              let user: u32 = request.path.user;
              let worker = instance("cart-${user}");
              worker.add-item(request.body);
              "successfully added item to the cart"
        - method: GET
          path: /v1/{user}/contents
          binding:
            type: default
            componentName: "amazon:shopping-cart"
            response: |
              let user: u32 = request.path.user;
              let worker = instance("cart-${user}");
              worker.get-cart-contents()
        - method: POST
          path: /v1/{user}/checkout
          binding:
            type: default
            componentName: "amazon:shopping-cart"
            response: |
              let user: u32 = request.path.user;
              let worker = instance("cart-${user}");
              worker.checkout()
 
  # Uncomment if you want to deploy your api
  deployments:
    local:
    - host: localhost:9006
      definitions:
       - shopping-cart
 

Here we added more routes into the same API definition. Now simply redeploy using

golem api deploy
 

The interactive shell will force you to update the version of the API definition to not lose track of the changes, and keep a note that you can have advanced management of the APIs using golem api definition and golem api deploy commands allowing you to switch back to the previous API if needed.

Try out the updated APIs

Route 1

curl -X POST http://localhost:9006/v1/100/add-to-cart -d '{"product-id": "foo", "name": "foo", "price": 42, "quantity": 42}' -H "accept: application/json"
"successfully added item to the cart

Route 2

curl -X GET http://localhost:9006/v1/100/contents -H "accept: Application/json"
[{"name":"foo","price":42.0,"product-id":"foo","quantity":42},{"name":"foo","price":42.0,"product-id":"foo","quantity":42},{"name":"foo","price":42.0,"product-id":"foo","quantity":42},{"name":"foo","price":42.0,"product-id":"foo","quantity":42},{"name":"foo","price":42.

Route 3

curl -X POST http://localhost:9006/v1/100/checkout
{"success":{"order-id":"238738674"}}⏎

Deploying multiple definitions to the same site and route conflicts

Multiple API definitions can be deployed to a specific site (subdomain.domain), as far their routes don't conflict.

 
# Schema for IDEA:
# $schema: https://schema.golem.cloud/app/golem/1.2.2-dev.1/golem.schema.json
# Schema for vscode-yaml
# yaml-language-server: $schema=https://schema.golem.cloud/app/golem/1.2.2-dev.1/golem.schema.json
 
# See https://learn.golem.cloud/docs/app-manifest#field-reference for field reference
 
components:
  amazon:shopping-cart:
    template: rust
 
httpApi:
  definitions:
    shopping-cart:
      version: '0.0.2'
      routes:
        - method: POST
          path: /v1/{user}/add-to-cart
          binding:
            type: default
            componentName: "amazon:shopping-cart"
            response: |
              let user: u32 = request.path.user;
              let worker = instance("cart-${user}");
              worker.add-item(request.body);
              "successfully added item to the cart"
        - method: GET
          path: /v1/{user}/contents
          binding:
            type: default
            componentName: "amazon:shopping-cart"
            response: |
              let user: u32 = request.path.user;
              let worker = instance("cart-${user}");
              worker.get-cart-contents()
        - method: POST
          path: /v1/{user}/checkout
          binding:
            type: default
            componentName: "amazon:shopping-cart"
            response: |
              let user: u32 = request.path.user;
              let worker = instance("cart-${user}");
              worker.checkout()
 
    shopping-cart2:
      version: '0.0.2'
      routes:
        - method: POST
          path: /v2/{user}/add-to-cart
          binding:
            type: default
            componentName: "amazon:shopping-cart"
            response: |
              let user: u32 = request.path.user;
              let worker = instance("cart-${user}");
              worker.add-item(request.body);
              "successfully added item to the cart"
 
  # Uncomment if you want to deploy your api
  deployments:
    local:
    - host: localhost:9006
      definitions:
       - shopping-cart
       - shopping-cart2
 
 
golem app deploy
 

This will result in

  Updating HTTP API deployment localhost:9006
    API shopping-cart/0.0.2 deployed at localhost:9006
    API shopping-cart2/0.0.2 deployed at localhost:9006

However, if we routes conflict between API definitions, (example: both have /v1/{user}/add-to-cart), then you will get a conflict error as given below

error: API Deployment Service - Error: 400 Bad Request, API deployment definitions conflict error: /v1/{user}/
     add-to-cart
 !  ~/p/shopping-c