go-multiport-validate — one program, several output streams
Question: stdin and stdout are one input and one output — how does a
program write three streams? It keeps stdout for the main one and POSTs
the others to loopback endpoints the runner hands it. No SDK: under 300 lines of
Go, standard library only.
Needs: Go (to build the program). Commands run from the repository root.
go build -o examples/go-multiport-validate/bin/validate-email ./examples/go-multiport-validate && printf '%s\n' '{"id":1,"email":"alice@example.com"}' '{"id":2,"email":"not-an-address"}' '{"id":3,"email":"bob@example.org"}' | openflange run ./examples/go-multiport-validate --grant subprocess --output valid=- --output rejected=rejected.ndjson --output stats=stats.ndjson
stdout (valid):
{"id":1,"email":"alice@example.com"}
{"id":3,"email":"bob@example.org"}
rejected.ndjson:
{"record":{"id":2,"email":"not-an-address"},"reason":"expected exactly one @"}
stats.ndjson (optional output):
{"rejected":1,"total":3,"valid":2}
How the program finds its ports
The runner writes a JSON file (mode 0600) and passes its path in
OPENFLANGE_CONFIG — never on the command line. For the run above it looks
like this (shape from the CLI specification; port number and token differ
per run):
{"spec": "openflange.org/cli/v0.1",
"pipe": {"id": "validate-email", "version": "0.1.0"},
"parameters": {},
"inputs": {"records": {"enabled": true, "transport": "stdin", "mediaType": "application/x-ndjson"}},
"outputs": {"valid": {"enabled": true, "transport": "stdout", "mediaType": "application/x-ndjson"},
"rejected": {"enabled": true, "transport": "http", "url": "http://127.0.0.1:41873/outputs/rejected", "mediaType": "application/x-ndjson"},
"stats": {"enabled": true, "transport": "http", "url": "http://127.0.0.1:41873/outputs/stats", "mediaType": "application/x-ndjson"}},
"token": "<opaque, scoped to this execution>"}
main.go does three things with it:
- reads
records from stdin and writes valid to stdout, as any filter would;
- for each other enabled output, opens one
POST <url> with
Authorization: Bearer <token> and streams NDJSON as the request body —
the end of the body is the end of the stream, and the runner answers 204;
- skips ports that are
{"enabled": false} (leave stats unconnected and
it is never computed into a request).
The direction is the mirror image of the HTTP Pipe Protocol
(../python-http-enrich): here the executable is
the HTTP client, and the runner owns the endpoints.
Without a runner
With no OPENFLANGE_CONFIG it is a plain filter — and because nobody is
listening for rejects, an invalid record stops it instead of vanishing:
printf '%s\n' '{"id":1,"email":"alice@example.com"}' '{"id":2,"email":"not-an-address"}' | examples/go-multiport-validate/bin/validate-email
{"id":1,"email":"alice@example.com"}
validate-email: record 2 is invalid (expected exactly one @) and the rejected output is not connected
(exit status 1)
Good to know
rejected is a required output: openflange run refuses to start
until it is bound, and a pipeline must connect it or say discard: true.
- Valid records are passed through byte for byte: a 30-digit
id keeps
every digit (tested).
- A line that is not JSON fails the run (the runner reports
E_FORMAT).
- The token is read from the config file and sent only to the loopback
endpoints (proxy settings are ignored); it is never logged.
- Permissions:
subprocess. A subprocess is not a sandbox.
Test
openflange test ./examples/go-multiport-validate
ok 6 tests passed in 1 file