Giter Site home page Giter Site logo

client-native's Introduction

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))
//...

Generating interfaces

if you change arguments of Client functions, you need to regenerate interface

ifacemaker -f './configuration/*.go'  -p client_native -i IConfigurationClient -s Client -c "This file is generated, don't edit manually, see README.md for details." > client_interface.go
ifacemaker -f 'runtime/*.go' -p client_native -i IRuntimeClient -s Client -c "This file is generated, don't edit manually, see README.md for details." > runtimeclient_interface.go

You will have to make some manual editing of generated files, because this process is not perfect and generated code won't compile.

Contributing

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

client-native's People

Contributors

amelhusic avatar bedis avatar daniel-corbett avatar dupondje avatar gitforbit avatar gorangalinec avatar kashifmin avatar mjuraga avatar mo3m3n avatar oktalz avatar rmaticevic avatar rubycut avatar schegi avatar yuzhichang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.