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
- func Serve(ctx context.Context, handler http.Handler, opts ...Option) error
- type Option
- func WithAdminURL(url string) Option
- func WithConfigFile(path string) Option
- func WithDialHeader(name, value string) Option
- func WithLogger(logger *zap.Logger) Option
- func WithPeerName(name string) Option
- func WithProfile(name string) Option
- func WithRequestHook(hook reqlog.Hook) Option
- func WithToken(token string) Option
- func WithVersion(version string) Option
- type Peer
Constants ¶
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 ¶
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 ¶
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 ¶
WithConfigFile reads profiles from path instead of ~/.holt/config.yaml.
func WithDialHeader ¶
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 ¶
WithLogger reports attaches, detaches, and redials. Default: no logging, so a library that is not asked to speak stays quiet.
func WithPeerName ¶
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 ¶
WithProfile picks a named profile instead of the file's default_profile: the library equivalent of --profile.
func WithRequestHook ¶
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 ¶
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 ¶
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.