go-multiport-validate

command
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

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.

Input → command → output

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:

  1. reads records from stdin and writes valid to stdout, as any filter would;
  2. 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;
  3. 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

Documentation

Overview

Command validate-email is a multi-port OpenFlange pipe written against the CLI protocol (openflange.org/cli/v0.1) with the Go standard library only.

input   records   NDJSON records
output  valid     records whose `email` is a plausible address, unchanged
output  rejected  the others, as {"record": ..., "reason": "..."}
output  stats     (optional) one {"total","valid","rejected"} record at the end

The runner describes the execution in the JSON file named by OPENFLANGE_CONFIG: which ports are enabled and where each one lives — stdin/stdout for the primary streams, an authenticated loopback HTTP endpoint for every other port (the executable GETs extra inputs and POSTs extra outputs).

Without OPENFLANGE_CONFIG it is an ordinary Unix filter: records on stdin, valid records on stdout; because nobody is listening for rejects, the first invalid record stops it with an error instead of being dropped.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL