Go library to programmatically access the Varnish administration port
MIT License
This repository contains a client library that can be used to connect to the Varnish administration port (typically used by the varnishadm tool).
NOTE: Experimental. Use at own peril!
Install this library via go get
:
$ go get github.com/martin-helmich/go-varnish-client
First, connect to the administration port using the varnishclient.DialTCP
method:
client, err := varnishclient.DialTCP(context.Background(), "127.0.0.1:6082")
if err != nil {
panic(err)
}
You can then use the client.AuthenticationRequired()
method to check if the Varnish server requires authentication.
Then, use the client.Authenticate()
method to perform the authentication:
secret, _ := ioutil.ReadFile("/etc/varnish/secret")
if client.AuthenticationRequired() {
err := client.Authenticate(context.Background(), secret)
if err != nil {
panic(err)
}
}
ctx := context.Background()
err := client.DefineInlineVCL(ctx, "my-new-config", vclCode, "warm")
if err != nil {
panic(err)
}
err = client.UseVCL(ctx, "my-new-config")
if err != nil {
panic(err)
}
All operations accept a context.Context
parameter that can be used for timeouts and/or cancellations:
ctx, cancel := context.WithTimeout(context.Background(), 1 * time.Second)
defer cancel()
err := client.DefineInlineVCL(ctx, "my-new-config", vclCode, "warm")
if err != nil {
panic(err)
}