expose

package
v0.48.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: MIT Imports: 20 Imported by: ? 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.

Documentation

Overview

Package expose serves an http.Handler through a holt hub with no configuration in the program. It is `holt expose` as a library, for the case where the thing being exposed is the Go program itself rather than a port it listens on.

http.HandleFunc("/hello", hello)
log.Fatal(expose.Serve(ctx, http.DefaultServeMux))

That call reads ~/.holt/config.yaml, takes its default profile, enrolls itself with that hub, and serves the handler back down the tunnel until ctx ends, redialing on its own. The program never listens on a port and needs no URL, token, or credentials of its own: the profile already describes the hub, including whatever headers get a request past the proxy in front of it.

New returns before dialing when the caller wants the address first:

peer, err := expose.New(ctx, handler)
fmt.Println(peer.URL)     // https://<peer>.<proxy domain>/
err = peer.Serve(ctx)

Everything is overridable (WithProfile, WithToken, WithPeerName, …), but nothing has to be: the zero-option call is the point.

Index

Constants

View Source
const TokenEnv = "HOLT_TOKEN"

TokenEnv is the environment variable read when no token is given in code, the same one the CLI honours. It skips enrollment, so a peer deployed with a long-lived identity keeps it across restarts.

Variables

This section is empty.

Functions

func Serve

func Serve(ctx context.Context, handler http.Handler, opts ...Option) error

Serve exposes handler through the hub in the local holt config and blocks until ctx ends, redialing on its own if the tunnel drops. It is New followed by Peer.Serve, for the common case that does not need the address first. A cancelled ctx is a clean stop, not an error.

Types

type Option

type Option func(*options)

Option overrides one piece of what the local config already decides. None is required: Serve with no options is the intended call.

func WithAdminURL

func WithAdminURL(url string) Option

WithAdminURL points enrollment at a specific hub, overriding the profile's admin_url. The profile's headers are then dropped unless the URL is the same hub, so credentials meant for one endpoint are not handed to another.

func WithConfigFile

func WithConfigFile(path string) Option

WithConfigFile reads profiles from path instead of ~/.holt/config.yaml.

func WithDialHeader

func WithDialHeader(name, value string) Option

WithDialHeader adds a header to the tunnel's WebSocket upgrade request, for an edge that gates the tunnel hostname too. Repeatable.

The profile's headers are deliberately not sent here: they authenticate the admin endpoint, and the tunnel is usually a different hostname (commonly bypassed, since a headless peer cannot complete an interactive login). Name what the tunnel needs.

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger reports attaches, detaches, and redials. Default: no logging, so a library that is not asked to speak stays quiet.

func WithPeerName

func WithPeerName(name string) Option

WithPeerName enrolls under a chosen id rather than a generated one. The id must be a DNS label, since it doubles as a hostname wherever the hub routes by subdomain.

func WithProfile

func WithProfile(name string) Option

WithProfile picks a named profile instead of the file's default_profile: the library equivalent of --profile.

func WithRequestHook

func WithRequestHook(hook reqlog.Hook) Option

WithRequestHook reports every request this peer serves over the tunnel, once the handler is done. Nothing is stored; it runs on the request's goroutine, so keep it cheap.

func WithToken

func WithToken(token string) Option

WithToken attaches with an existing join token instead of enrolling. Use it for a peer whose identity should outlive the process: an enrollment on every start is a new peer id every start.

func WithVersion

func WithVersion(version string) Option

WithVersion sets the build version reported to the hub, which the console shows per peer and uses to flag version skew. Default names this library.

type Peer

type Peer struct {
	// Name is the peer id on the hub, generated at enrollment unless
	// WithPeerName said otherwise.
	Name string
	// URL is where the handler answers, when the hub could be asked and
	// routes by subdomain. Empty when neither holds, in which case
	// RouteHeader is how a caller names this peer.
	URL string
	// RouteHeader is the header that picks this peer, set when the URL
	// alone does not route (header routing). Empty under subdomain
	// routing, where the hostname is enough.
	RouteHeader string
	// TunnelURL is the hub endpoint this peer dials, from the token.
	TunnelURL string
	// contains filtered or unexported fields
}

Peer is an enrolled identity with a handler to serve. Its fields describe where the handler answers, known before anything is dialed so a program can log its own public address at startup.

func New

func New(ctx context.Context, handler http.Handler, opts ...Option) (*Peer, error)

New resolves the hub from the local config, obtains an identity for this program, and asks the hub where the handler will answer. It dials nothing: call Peer.Serve for that.

The identity is the first of: WithToken, $HOLT_TOKEN, a fresh enrollment against the configured hub.

func (*Peer) Serve

func (p *Peer) Serve(ctx context.Context) error

Serve attaches to the hub and serves the handler over the tunnel until ctx ends, redialing with backoff in between. A cancelled ctx returns nil: stopping on purpose is not a failure.

Jump to

Keyboard shortcuts

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