#
# Usage
#
# Save this config file locally
# From the directory containing the config file run the following command:
# 
# docker run --rm --env 'HOOK_SECRET=<your_hook_secret_here>' -p 4196:4196 -v $(pwd)/config.yaml:/benthos.yaml jeffail/benthos:4.23.0
#

http:
  enabled: true
  address: 0.0.0.0:4195
  root_path: /benthos
  debug_endpoints: true

input:
  broker:
    copies: 1
    inputs:
      - type: http_server
        http_server:
          address: 0.0.0.0:4196
          path: /v1/hooks
          allowed_verbs:
            - POST
          timeout: 5s
          sync_response:
            status: 200 # Always return a 200-ok

pipeline:
  processors:
    # Signature verification
    - bloblang: |
        # Delete the message if the HOOK_SECRET env variable is set and the signatures don't match
        root = if "${HOOK_SECRET:''}" != "" && meta("X-Hub-Signature") != "sha1=%s".format(content().hash("hmac_sha1","${HOOK_SECRET:''}").encode("hex")) {
            deleted()
        }

    # Split the JSON array up to process each document separately 
    - bloblang: |
        root = if this.type() != "array" {
            deleted()
        }
    - unarchive:
        format: "json_array"
    # In-memory dedupe documents by Hook ID and Document ID
    - dedupe:
        cache: dedupe_cache
        key: ${! json("metadata.hook_id") }:::${! json("document_id") }

output:
  broker:
    pattern: fan_out
    outputs:
      # Output to terminal
      - type: stdout
      #
      # Put your own output here
      #

cache_resources:
  - label: dedupe_cache
    memory:
      default_ttl: 300s

logger:
  level: TRACE
  format: json
  add_timestamp: true
  static_fields:
    '@service': benthos_meltwater_api_documents

metrics:
  prometheus: {}
