Skip to content

Commit

Permalink
chore: use mustache
Browse files Browse the repository at this point in the history
  • Loading branch information
rorlic committed Apr 18, 2023
1 parent ce1062e commit eeacb4a
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 233 deletions.
51 changes: 22 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Test Message Generator
This small tool helps to generate (test) messages based on a template. It can act as a simple replacement for message source systems and allows to send on regular time intervals some data, based on a template that gets altered before sending based on a mapping.
This small tool helps to generate (test) messages based on a [Mustache](https://mustache.github.io/) template. It can act as a simple replacement for message source systems and allows to send on regular time intervals some data, based on a template that gets altered before sending.

## Docker
The generator can be run as a Docker container, after creating a Docker image for it. The Docker container will keep running until stopped.
Expand All @@ -11,14 +11,14 @@ docker build --tag vsds/test-message-generator .

To run the generator, you can use:
```bash
docker run -v $(pwd)/data:/tmp/data -e TEMPLATEFILE=/tmp/data/other.template.json -e MAPPINGFILE=/tmp/data/other.mapping.json vsds/test-message-generator
docker run -v $(pwd)/data:/tmp/data -e TEMPLATEFILE=/tmp/data/template.json vsds/test-message-generator
```
You can also pass the following arguments when running the container:
* `TARGETURL=<target-uri>` to POST the output to the target URI instead of to the console
* `SILENT=true` to display no logging to the console
* `MIMETYPE=<mime-type>` to use a different mime-type
* `TARGETURL=<target-uri>` to POST the output to the target URI instead of to the console
* `MIMETYPE=<mime-type>` to specify a mime-type when POSTing

Alternatively, you can also pass the template and mapping as string instead of as files, use `TEMPLATE` respectively `MAPPING`.
Alternatively, you can also pass the template as string instead of as file, use `TEMPLATE`.

## Build the Generator
The generator is implemented as a [Node.js](https://nodejs.org/en/) application.
Expand All @@ -29,11 +29,11 @@ npm run build
```

## Run the Generator
The generator works based on a JSON template, defining the structure to use for each generated item, and a JSON mapping file, defining the transformations that need to be performed on the template. It can send the generated JSON data to a target URL or simply send it to the console.
The generator works based on a template, defining the structure to use for each generated item. It can send the generated data to a target URL or simply send it to the console.

The generator takes the following command line arguments:
* `--silent=<true|false>` prevents any console debug output if true, defaults to false (not silent, logging all debug info)
* `--targetUrl` defines the target URL to where the generated JSON is POST'ed as `application/json`, no default (if not provided, sends output to console independant of `--silent`)
* `--targetUrl` defines the target URL to where the generated message is POST'ed as the configured mime-type, no default (if not provided, sends output to console independant of `--silent`)
> **Note**: alternatively, you can provide the target URL as a plain text in a file named `TARGETURL` (located in the current working directory) allowing to change the target URL at runtime as the file is read at cron schedule time (see below), e.g.:
> ```bash
> echo http://example.org/my-ingest-endpoint > ./TARGETURL
Expand All @@ -43,35 +43,28 @@ The generator takes the following command line arguments:
> ```bash
> echo https://webhook.site/f140204a-9514-4bfa-8d3e-fd18ba325ee3 > ./TARGETURL
> ```
* `--mimeType=<mime-type>` mime-type of message send to target URL, defaults to `application/json`
* `--mimeType=<mime-type>` mime-type of message send to target URL, no default
* `--cron` defines the time schedule, defaults to `* * * * * * ` (every second)
* `--template='<json-content>'` allows to provide the JSON template on the command line, no default (if not provided, you MUST provide `--templateFile`)
* `--templateFile=<partial-or-full-pathname>` allows to provide the JSON template in a file, no default (if not provided, you MUST provide `--template`)
* `--mapping='<json-content>'` allows to provide the JSON mapping on the command line, no default (if not provided, you MUST provide `--mappingFile`)
* `--mappingFile=<partial-or-full-pathname>` allows to provide the JSON mapping in a file, no default (if not provided, you MUST provide `--mapping`)
* `--template='<content>'` allows to provide the template on the command line, no default (if not provided, you MUST provide `--templateFile`)
* `--templateFile=<partial-or-full-pathname>` allows to provide the template in a file, no default (if not provided, you MUST provide `--template`)
The template or template file should simply contain a valid JSON structure (with one or more JSON objects). E.g.:
The template or template file should simply contain a message with mustache variables (between `{{` and `}}`). E.g.:
```json
[
{ "id": "my-id", "type": "Something", "modifiedAt": "2022-09-09T09:10:00.000Z" },
{ "id": "my-other-id", "type": "SomethingElse", "modifiedAt": "2022-09-09T09:10:00.000Z" }
{ "id": "my-id-{{index}}", "type": "Something", "modifiedAt": "{{timestamp}}" },
{ "id": "my-other-id-{{index}}", "type": "SomethingElse", "modifiedAt": "{{timestamp}}" }
]
```
The mapping file is also a JSON file but uses a key/value mapping where the key conforms the [JSON path specifications](https://datatracker.ietf.org/doc/id/draft-goessner-dispatch-jsonpath-00.html) and the value conforms a syntax allowing to change the value matched by the JSON path to a new value obtained by replacing the variables specified in the value part, e.g.:
```json
{ "$.id": "${@}-${nextCounter}", "$.modifiedAt": "${currentTimestamp}" }
```
The `${@}` will be replaced by the currently match value of the JSON path `$.id` (e.g. `my-id`) while any other `${<property>}` will use a property of the generator itself. Currently the only allowed properties are:
* `nextCounter`: increasing integer value, starting from 1
* `currentTimestamp`: current date and time formatted as [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) in UTC (e.g. `2007-04-05T14:30:00.000Z`)
Currently the only allowed variables are:
* `index`: increasing integer value, starting from 1
* `timestamp`: current date and time formatted as [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) in UTC (e.g. `2007-04-05T14:30:00.000Z`)
You can run the generator after building it, e.g.:
Using this [template](./data/other.template.json) and this [mapping](./data//other.mapping.json) and with silent output to console:
Using this [template](./data/template.json) and with silent output to console:
```bash
node ./dist/index.js --templateFile ./data/other.template.json --mappingFile ./data/other.mapping.json --silent
node ./dist/index.js --templateFile ./data/template.json --silent
```
This results in something like the following:
```
Expand All @@ -80,9 +73,10 @@ This results in something like the following:
{"id":"my-id-3","type":"Something","modifiedAt":"2022-09-12T13:15:44.003Z"}
...
```
By specifying the template (containing multiple objects) and mapping on the command file:
```bash
node ./dist/index.js --template '[{"id": "my-id", "type": "Something", "modifiedAt": "2022-09-09T09:10:00.000Z" },{ "id": "my-other-id", "type": "SomethingElse", "modifiedAt": "2022-09-09T09:10:00.000Z" }]' --mapping '{ "$..id": "${@}-${nextCounter}", "$..modifiedAt": "${currentTimestamp}" }' --silent
node ./dist/index.js --template '[{"id": "my-id-{{index}}", "type": "Something", "modifiedAt": "{{timestamp}}" },{ "id": "my-other-id-{{index}}", "type": "SomethingElse", "modifiedAt": "{{timestamp}}" }]' --silent
```
This results in something like:
```json
Expand All @@ -94,14 +88,13 @@ This results in something like:
Alternatively you can generate the output using a different time schedule (e.g. every 2 seconds) to a [dummy HTTP server](https://docs.webhook.site/) (including debugging to the console):
```bash
node ./dist/index.js --templateFile ./data/other.template.json --mappingFile ./data/other.mapping.json --cron '*/2 * * * * *' --targetUrl https://webhook.site/ce3065f5-2f0b-49d8-8856-330ae3c6e737
node ./dist/index.js --templateFile ./data/template.json --cron '*/2 * * * * *' --targetUrl https://webhook.site/ce3065f5-2f0b-49d8-8856-330ae3c6e737 --mimeType application/json
```
This results in:
```
Arguments: {
_: [],
templateFile: './data/other.template.json',
mappingFile: './data/other.mapping.json',
templateFile: './data/template.json',
cron: '*/2 * * * * *',
targetUrl: 'https://webhook.site/ce3065f5-2f0b-49d8-8856-330ae3c6e737'
}
Expand Down
1 change: 0 additions & 1 deletion data/other.mapping.json

This file was deleted.

1 change: 0 additions & 1 deletion data/other.template.json

This file was deleted.

1 change: 1 addition & 0 deletions data/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "id": "my-id-{{index}}", "type": "Something", "modifiedAt": "{{timestamp}}" }
182 changes: 16 additions & 166 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eeacb4a

Please sign in to comment.