Skip to content

rqlite/rqlite-go-http

Repository files navigation

rqlite-go-http

Circle CI Go Report Card Go Reference

rqlite-go-http is a Go client for rqlite that interacts directly with the rqlite HTTP API, providing minimal abstraction. Robust and easy-to-use, it can be used on its own or as a foundation for higher-level libraries. The client is safe for concurrent use by multiple goroutines.

This library offers support for:

  • Executing SQL statements (INSERT, UPDATE, DELETE)
  • Running queries (SELECT)
  • Handling both read and write statements in a single request via the Unified Endpoint.
  • Backing up and restoring data to your rqlite system
  • Booting a rqlite node from a SQLite database file
  • Checking node status, diagnostic info, cluster membership, and readiness
  • Ability to customize HTTP communications for control over TLS, mutual TLS, timeouts, etc.

Check out the documentation for more details.

Installation

go get github.com/rqlite/rqlite-go-http

Versioning

This library is under active development and is subject to breaking changes. Be sure to use go mod to pin any import of this package to a specific commit.

Example use

package main import ( "context" "fmt"	rqlitehttp "github.com/rqlite/rqlite-go-http" ) func main() { // Create a client pointing to a rqlite node client, err := rqlitehttp.NewClient("http://localhost:4001", nil) if err != nil { panic(err)	} // Optionally set Basic Auth client.SetBasicAuth("user", "password") // Create a table. resp, err := client.ExecuteSingle(context.Background(), "CREATE TABLE foo (id INTEGER PRIMARY KEY, name TEXT)") if err != nil { panic(err)	} // Insert a record. resp, err = client.Execute( context.Background(),	rqlitehttp.SQLStatements{	{ SQL: "INSERT INTO foo(name) VALUES(?)", PositionalParams: []any{"fiona"},	},	},	) if err != nil { panic(err)	} fmt.Printf("ExecuteResponse: %+v\n", resp) // Query the newly created table qResp, err := client.QuerySingle(context.Background(), "SELECT * FROM foo") if err != nil { panic(err)	} fmt.Printf("QueryResponse: %+v\n", qResp) }

Handling numbers

When handling a JSON response from rqlite which a number, this library stores it as a json.Number. This avoids precision loss. You can then convert it to the type your schema expects. For example, if you expect an int64:

i, err := row[0].(json.Number).Int64() if err != nil { panic("number is not an int64") }

The JSON specification limits the size of numbers, and the Go standard library JSON encoder represents any number larger than the limit as a string. When working with a JSON response containing such values-as-strings, use the math/big package:

n := &big.Int{} v, ok := n.SetString(row[0].(json.Number).String(), 10) if !ok { panic("failed to parse big int") } // n now holds the parsed big integer.

About

A Go client for rqlite, useful on its own or as a foundation for higher-level libraries.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages