Candle Documentation

RestAPI Router

This example shows how to use the REST router to define a REST API that delegates implementations to arbitrary Wick component's.

This app configuration starts off with the common Application kind and a name field.

kind: wick/app@v1
name: rest_api_app

Note: All HTTP routers must be configured with a path that acts as a simple filter to determine which router to execute. Routers are executed in top-down order. A router configured with path: "/" will match all traffic so should be defined at the end of the router configuration.

Rest Routers use common configuration patterns to define complex JSON (or other) RestAPIs in a simple configuration.

A Rest Router takes any number of Routes that define a uri and methods to match and the operation that will handle the request.

A Route uri can be defined as a simple string or can have embedded path and query string parameters which will be automatically parsed and delivered as inputs to the configured operation. E.g.

routes:
- description: Echoes inputs first, second, third, and fourth back as JSON
  operation: sample::echo
  sub_path: /this/{first:string}/some/{second:u32}?third:string[]&fourth:bool

Note: The request body will be parsed as JSON and delivered to the configured operation as the input parameter.

The full configuration is located at examples/http/rest-router.wick. Run this application locally by cloning the wick repository and using wick run (or cargo run -p wick-cli -- run to run from source)

git clone https://github.com/candlecorp/wick && cd wick
wick run examples/http/rest-router.wick

Complete Example

Note: Examples may require environment variables and execution from within the wick project’s root to access any local files.

The full example configuration is included below,

---
kind: wick/app@v1
name: rest_api_app
metadata:
  description: |
    This example shows how to use the REST router to define a REST API that
    delegates implementations to arbitrary Wick component's.    
  version: 0.0.1
  authors:
    - 'Wick Maintainers'
  vendors:
    - 'Candle Corporation'
  licenses:
    - Apache-2.0
resources:
  - name: http
    resource:
      kind: wick/resource/tcpport@v1
      port: '{{ ctx.env.HTTP_PORT | default: 8999 }}'
      address: 0.0.0.0
import:
  - name: sample
    component:
      kind: wick/component/manifest@v1
      ref: ./rest-router/component.wick
  - name: openapi
    component:
      kind: wick/component/manifest@v1
      ref: registry.candle.dev/common/openapi-ui:0.4.0
      with:
        schema_url: /api/openapi.json
triggers:
  - kind: wick/trigger/http@v1
    resource: http
    routers:
      - kind: wick/router/raw@v1
        path: /openapi-ui
        codec: Raw
        operation: openapi::serve
      - kind: wick/router/rest@v1
        path: /api
        tools:
          openapi: true
        info:
          title: 'Sample REST API'
          description: 'A sample REST API'
          version: '0.0.1'
        routes:
          - sub_path: '/this/{first:string}/some/{second:u32}?third:string[]&fourth:bool'
            operation: sample::echo
            description: 'Echoes inputs first, second, third, and fourth back as JSON'