Skip to content

diginfra/client-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Diginfra Go Client

Diginfra Ecosystem Repository Incubating

GoDoc

Go client and SDK for Diginfra

Learn more about the gRPC API by reading the docs.

Install

go get -u github.com/diginfra/client-go

Usage

Network Client creation

If you are binding the Diginfra gRPC server to a network socket with mTLS (mutual TLS authentication) you need this one. Please remember that since this is enabling mTLS you will need to generate a pair of certificates for this client specifically and provide the CA certificate. If you need something simpler, go for the unix socket.

package main

imports(
    "context"
    "github.com/diginfra/client-go/pkg/client"
)

func main() {
    c, err := client.NewForConfig(context.Background(), &client.Config{
        Hostname:   "localhost",
        Port:       5060,
        CertFile:   "/etc/diginfra/certs/client.crt",
        KeyFile:    "/etc/diginfra/certs/client.key",
        CARootFile: "/etc/diginfra/certs/ca.crt",
    })
}

Unix Socket Client creation

If you are binding the Diginfra gRPC server to unix socket, this is what you need.

package main

imports(
    "context"
    "github.com/diginfra/client-go/pkg/client"
)

func main() {
    c, err := client.NewForConfig(context.Background(), &client.Config{
        UnixSocketPath:   "unix:///run/diginfra/diginfra.sock",
    })
}

Diginfra outputs API

outputsClient, err := c.Outputs()
if err != nil {
    log.Fatalf("unable to obtain an output client: %v", err)
}

ctx := context.Background()
fcs, err := outputsClient.Get(ctx, &outputs.Request{})
if err != nil {
    log.Fatalf("could not subscribe: %v", err)
}

for {
    res, err := fcs.Recv()
    if err == io.EOF {
        break
    }
    if err != nil {
        log.Fatalf("error closing stream after EOF: %v", err)
    }
    fmt.Printf("rule: %s\n", res.Rule)
}

Diginfra version API

// Set up a connection to the server.
c, err := client.NewForConfig(context.Background(), &client.Config{
    Hostname:   "localhost",
    Port:       5060,
    CertFile:   "/etc/diginfra/certs/client.crt",
    KeyFile:    "/etc/diginfra/certs/client.key",
    CARootFile: "/etc/diginfra/certs/ca.crt",
})
if err != nil {
    log.Fatalf("unable to create a Diginfra client: %v", err)
}
defer c.Close()
versionClient, err := c.Version()
if err != nil {
    log.Fatalf("unable to obtain a version client: %v", err)
}

ctx := context.Background()
res, err := versionClient.Version(ctx, &version.Request{})
if err != nil {
    log.Fatalf("error obtaining the Diginfra version: %v", err)
}
fmt.Printf("%v\n", res)

Full Examples

Update protos

Perform the following edits to the Makefile:

  1. Update the PROTOS array with the destination path of the .proto file.
  2. Update the PROTO_URLS array with the URL from which to download it.
  3. Update the PROTO_SHAS array with the SHA256 sum of the file to download.
  4. Execute the following commands:
make clean
make protos

Generate mocks for protos

  1. Follow the steps in the Update protos section
  2. Execute the following commands:
make mocks

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published