Documentation ¶
Overview ¶
Package httpvar provides a runtimevar implementation with variables backed by http endpoint. Use OpenVariable to construct a *runtimevar.Variable.
URLs ¶
For runtimevar.OpenVariable, httpvar registers for the schemes "http" and "https". The default URL opener will use http.DefaultClient. To customize the URL opener, or for more details on the URL format, see URLOpener. See https://github.com/eliben/gocdkx/concepts/urls/ for background information.
As ¶
httpvar exposes the following types for As:
- Snapshot: *http.Response
- Error: httpvar.RequestError, url.Error
Example ¶
package main import ( "context" "log" "net/http" "github.com/eliben/gocdkx/runtimevar" "github.com/eliben/gocdkx/runtimevar/httpvar" ) // MyConfig is a sample configuration struct. type MyConfig struct { Server string Port int } func main() { httpClient := http.DefaultClient decoder := runtimevar.NewDecoder(MyConfig{}, runtimevar.JSONDecode) // Construct a *runtimevar.Variable that watches the variable. v, err := httpvar.OpenVariable(httpClient, "http://example.com", decoder, nil) if err != nil { log.Fatal(err) } defer v.Close() // We can now read the current value of the variable from v. snapshot, err := v.Watch(context.Background()) if err != nil { log.Fatal(err) } cfg := snapshot.Value.(MyConfig) _ = cfg }
Output:
Example (OpenVariable) ¶
package main import ( "context" "log" "github.com/eliben/gocdkx/runtimevar" ) func main() { // OpenVariable creates a *runtimevar.Variable from a URL. // The "decoder" query parameter is optional, and is removed before fetching // the URL. ctx := context.Background() v, err := runtimevar.OpenVariable(ctx, "http://myserver.com/foo.txt?decoder=string") if err != nil { log.Fatal(err) } snapshot, err := v.Latest(ctx) _, _ = snapshot, err }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Schemes = []string{"http", "https"}
Schemes are the URL schemes httpvar registers its URLOpener under on runtimevar.DefaultMux.
Functions ¶
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.
Types ¶
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 ¶
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 "http://myserver.com/foo.txt".
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.
func (*URLOpener) OpenVariableURL ¶
OpenVariableURL opens a httpvar Variable for u.