client_native

package module
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2020 License: Apache-2.0 Imports: 3 Imported by: 2

README

HAProxy

HAProxy Native Golang Client

HAProxy Native Client is a client that exposes methods for reading and changing HAProxy configuration files, and executing commands and parsing the output of the HAProxy Runtime API (via unix socket, AKA stats socket in HAProxy). It uses the models package to shift data around.

Usage Example

// Initialize HAProxy native client
confClient := &configuration.Client{}
confParams := configuration.ClientParams{
    ConfigurationFile:      "/etc/haproxy/haproxy.cfg",
    Haproxy:                "/usr/sbin/haproxy",
    UseValidation:          true,
    PersistentTransactions: true,
    TransactionDir:         "/tmp/haproxy",
}
err := confClient.Init(confParams)
if err != nil {
    fmt.Println("Error setting up configuration client, using default one")
    confClient, err = configuration.DefaultClient()
    if err != nil {
        fmt.Println("Error setting up default configuration client, exiting...")
        api.ServerShutdown()
    }
}

runtimeClient := &runtime_api.Client{}
globalConf, err := confClient.GetGlobalConfiguration("")
if err == nil {
    socketList := make([]string, 0, 1)
    runtimeAPIs := globalConf.Data.RuntimeApis

    if len(runtimeAPIs) != 0 {
        for _, r := range runtimeAPIs {
            socketList = append(socketList, *r.Address)
        }
        if err := runtimeClient.Init(socketList, "", 0); err != nil {
            fmt.Println("Error setting up runtime client, not using one")
            return nil
		}
    } else {
		fmt.Println("Runtime API not configured, not using it")
		runtimeClient = nil
	}
} else {
    fmt.Println("Cannot read runtime API configuration, not using it")
    runtimeClient = nil
}

client := &client_native.HAProxyClient{}
client.Init(confClient, runtimeClient)

bcks, err := h.Client.Configuration.GetBackends(t)
if err != nil {
    fmt.Println(err.Error())
}
//...

backendsJSON, err := bcks.MarshallBinary()

if err != nil {
    fmt.Println(err.Error())
}

fmt.Println(string(backendsJSON))
//...

Contributing

For commit messages and general style please follow the haproxy project's CONTRIBUTING guide and use that where applicable.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LogFunc func(string, ...interface{}) = log.Printf

LogFunc - default log function is from the stdlib

Functions

This section is empty.

Types

type HAProxyClient

type HAProxyClient struct {
	Configuration *configuration.Client
	Runtime       *runtime.Client
}

HAProxyClient Native client for managing configuration and spitting out HAProxy stats

func DefaultClient

func DefaultClient() (*HAProxyClient, error)

DefaultClient with sane defaults

func (*HAProxyClient) Init

func (c *HAProxyClient) Init(configurationClient *configuration.Client, runtimeClient *runtime.Client) error

Init HAProxyClient

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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