api — a public HTTP API with no listener and no config
A Go program that becomes publicly reachable by calling one function.
It opens no port, has no URL of its own, and contains no hub
configuration: pkg/expose reads ~/.holt/config.yaml, enrolls with
the hub its default profile names, and serves the handler back down
the tunnel.
This is holt expose turned inside out. The CLI
exposes a port something else is listening on; this exposes the
handler in the program itself, so there is no local hop and nothing to
firewall.
Run it
You need a hub in ~/.holt/config.yaml. Any profile will do:
default_profile: local
profiles:
local:
admin_url: http://127.0.0.1:7201
Then:
holt hub --ui # in one terminal, if you have no hub yet
go run ./examples/api # in another
It prints where it landed:
serving at https://demo-api.peers.example.com/time
...or, on a header-routed hub, the URL plus the header that names it.
Either way the address is known before the tunnel is dialed, because
expose.New asks the hub and returns; peer.Serve(ctx) is what
blocks.
The whole integration
peer, err := expose.New(ctx, api, expose.WithPeerName("demo-api"))
if err != nil {
log.Fatal(err)
}
fmt.Println("serving at", peer.URL)
log.Fatal(peer.Serve(ctx))
Or, when the address does not need printing, one line:
log.Fatal(expose.Serve(ctx, api))
Identity
With no WithPeerName the peer enrolls under a generated id
(brisk-otter-4f2a), which is fine for something short-lived and
wrong for a service: a fresh enrollment on every start means a new
hostname on every start.
For anything long-lived, mint an identity once and give it to the
program:
holt enroll demo-api
HOLT_TOKEN=<paste> go run ./examples/api
$HOLT_TOKEN (or expose.WithToken) skips enrollment entirely, so
the peer keeps its name and its URL across restarts. Mind the hub's
--token-ttl: the token is checked on every attach, not just the
first.
← examples · Library