v0.37.0 Latest Latest

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

Go to latest
Published: Mar 12, 2024 License: Apache-2.0 Imports: 12 Imported by: 1



Package httpvar provides a runtimevar implementation with variables backed by http endpoint. Use OpenVariable to construct a *runtimevar.Variable.


For runtimevar.OpenVariable, httpvar registers for the schemes "http" and "https". The default URL opener will use http.DefaultClient. To use HTTP Basic Auth for the requests, set the environment variables "HTTPVAR_AUTH_USERNAME" and "HTTPVAR_AUTH_PASSWORD". To customize the URL opener, or for more details on the URL format, see URLOpener. See for background information.


httpvar exposes the following types for As:

  • Snapshot: *http.Response
  • Error: httpvar.RequestError, url.Error
Example (OpenVariableFromURL)
package main

import (


func main() {
	// PRAGMA: This example is used on; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On, add a blank import: _ ""
	// PRAGMA: On, hide lines until the next blank line.
	ctx := context.Background()

	// runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
	// The default opener connects to an etcd server based on the environment
	// variable ETCD_SERVER_URL.

	v, err := runtimevar.OpenVariable(ctx, "")
	if err != nil {
	defer v.Close()




This section is empty.


View Source
var Schemes = []string{"http", "https"}

Schemes are the URL schemes httpvar registers its URLOpener under on runtimevar.DefaultMux.


func OpenVariable

func OpenVariable(client *http.Client, urlStr string, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)

OpenVariable constructs a *runtimevar.Variable that uses client to retrieve the variable contents from the URL urlStr.

package main

import (


func main() {
	// PRAGMA: This example is used on; PRAGMA comments adjust how it is shown and can be ignored.

	// Create an HTTP.Client
	httpClient := http.DefaultClient

	// Construct a *runtimevar.Variable that watches the page.
	v, err := httpvar.OpenVariable(httpClient, "", runtimevar.StringDecoder, nil)
	if err != nil {
	defer v.Close()


type Options

type Options struct {
	// WaitDuration controls the rate at which the HTTP endpoint is called to check for changes.
	// Defaults to 30 seconds.
	WaitDuration time.Duration

Options sets options.

type RequestError

type RequestError struct {
	Response *http.Response

RequestError represents an HTTP error that occurred during endpoint call.

func (*RequestError) Error

func (e *RequestError) Error() string

type URLOpener

type URLOpener struct {
	// The Client to use; required.
	Client *http.Client

	// Decoder specifies the decoder to use if one is not specified in the URL.
	// Defaults to runtimevar.BytesDecoder.
	Decoder *runtimevar.Decoder

	// Options specifies the options to pass to OpenVariable.
	Options Options

URLOpener opens HTTP URLs like "".

The full URL, including scheme, is used as the endpoint, except that the the following URL parameters are removed if present:

  • decoder: The decoder to use. Defaults to runtimevar.BytesDecoder. See runtimevar.DecoderByName for supported values.
  • wait: The poll interval, in time.ParseDuration formats. Defaults to 30s.

func (*URLOpener) OpenVariableURL

func (o *URLOpener) OpenVariableURL(ctx context.Context, u *url.URL) (*runtimevar.Variable, error)

OpenVariableURL opens a httpvar Variable for u.

Jump to

Keyboard shortcuts

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