Infrastructure files generation #
One of the go-asyncapi
core features is generating the
infrastructure-as-code (IaC) files from an AsyncAPI document.
The generated files are useful for setting up the development environment quickly or as the starting point
for the deployment configurations.
They contain the AsyncAPI entities, that are relevant to the infrastructure setup. For example, servers
, channels
, etc.
The result of generation can also be customized in templates. See templating guide for more details.
Example #
go-asyncapi
accepts an AsyncAPI document in YAML or JSON format and produces the result depending on selected IaC technology.
Repeated tool runs produce exactly the same result.
Default technology is docker-compose
, so the result is saved to the docker-compose.yaml
file in the current directory.
Download the example document
and save it as streetlights-mqtt-asyncapi.yml
. Then, run the following command to generate the code:
go-asyncapi infra streetlights-mqtt-asyncapi.yml
Console output
INFO Logging to stderr INFO
INFO Compilation 🔨: Compile a document url=streetlights-mqtt-asyncapi.yml
INFO Locating 📡: Reading document from filesystem path=streetlights-mqtt-asyncapi.yml
INFO Linking 🔗: Linking complete refs=22
INFO Rendering 🎨: Infra code rendered file=./docker-compose.yaml
INFO Output file saved to "./docker-compose.yaml"
INFO Done
Open the generated docker-compose.yaml
file and it turns out that it is empty:
# This file is generated by go-asyncapi tool
# NOTE: Server production is not rendered due to missing server variables in the go-asyncapi configuration
# NOTE: Server production is not rendered due to missing server variables in the go-asyncapi configuration
# NOTE: Server production is not rendered due to missing server variables in the go-asyncapi configuration
# NOTE: Server production is not rendered due to missing server variables in the go-asyncapi configuration
# NOTE: Server production is not rendered due to missing server variables in the go-asyncapi configuration
The reason is that the single server production
requires a server variable port to get the correct hostname.
go-asyncapi
can’t generate a server without the server variables set.
To make it work, create a configuration file go-asyncapi.yaml
in with the following content:
infra:
serverOpts:
- serverName: production
variables:
port: 1883
Now run the command again with the -c
option to specify the configuration file:
go-asyncapi infra streetlights-mqtt-asyncapi.yml -c go-asyncapi.yaml
docker-compose.yaml
# This file is generated by go-asyncapi tool
services:
"production_1883":
image: emqx/emqx:latest
ports:
- "1883:1883"
- "18083:18083" # Management UI, creds:admin/public
volumes:
- "production-data_:/opt/emqx/data"
- "production-log_:/opt/emqx/log"
hostname: "test.mosquitto.org"
restart: on-failure
volumes:
"production-data_":
"production-log_":