feat: add K2V_ENDPOINT env var

This commit is contained in:
Milas Bowman 2025-03-09 20:51:43 -04:00
parent 97bedfe1aa
commit 231423cd0b
Signed by: milas
SSH key fingerprint: SHA256:ek2D5l1HA34B3wbdErz0QOXm1E46CVvyf/nM4Fwfx/U
3 changed files with 20 additions and 6 deletions

View file

@ -11,11 +11,14 @@ import k2v "code.notaphish.fyi/milas/garage-k2v-go"
## Create API client ## Create API client
```go ```go
endpoint := "http://localhost:3904" // Read K2V_ENDPOINT from OS environment variable, e.g. http://localhost:3904.
endpoint := k2v.EndpointFromEnv()
// Read K2V_KEY_ID and K2V_SECRET_KEY from OS environment variables. // Read K2V_KEY_ID and K2V_KEY_SECRET from OS environment variables.
key := k2v.KeyFromEnv() key := k2v.KeyFromEnv()
// key := k2v.Key{ID: "GK49e661847883e4813993a0db", Secret: "..."}
// Alternatively, construct a key by initializing the ID and secret fields on a k2v.Key.
// key := k2v.Key{ID: "GK...", Secret: "..."}
client := k2v.NewClient(endpoint, key) client := k2v.NewClient(endpoint, key)
``` ```
@ -36,6 +39,14 @@ type Client
func (c *Client) ReadItemSingle(ctx context.Context, b Bucket, pk string, sk string) (Item, CausalityToken, error) func (c *Client) ReadItemSingle(ctx context.Context, b Bucket, pk string, sk string) (Item, CausalityToken, error)
``` ```
## Integration Tests
```shell
K2V_ENDPOINT="http://[::1]:3904" \
K2V_KEY_ID="GK..." \
K2V_KEY_SECRET="..." \
go test ./...
```
## Usage ## Usage
Review the [K2V API spec][k2v-api] and the integration tests in this module for complete examples. Review the [K2V API spec][k2v-api] and the integration tests in this module for complete examples.

View file

@ -4,6 +4,7 @@ import "os"
const EnvVarKeyID = "K2V_KEY_ID" const EnvVarKeyID = "K2V_KEY_ID"
const EnvVarKeySecret = "K2V_KEY_SECRET" const EnvVarKeySecret = "K2V_KEY_SECRET"
const EnvVarEndpoint = "K2V_ENDPOINT"
func KeyFromEnv() Key { func KeyFromEnv() Key {
return Key{ return Key{
@ -11,3 +12,7 @@ func KeyFromEnv() Key {
Secret: os.Getenv(EnvVarKeySecret), Secret: os.Getenv(EnvVarKeySecret),
} }
} }
func EndpointFromEnv() string {
return os.Getenv(EnvVarEndpoint)
}

View file

@ -13,8 +13,6 @@ import (
"go.uber.org/goleak" "go.uber.org/goleak"
) )
const endpoint = "http://127.0.0.1:3904"
type fixture struct { type fixture struct {
t testing.TB t testing.TB
ctx context.Context ctx context.Context
@ -31,7 +29,7 @@ func newFixture(t testing.TB) (*fixture, context.Context) {
ctx := testContext(t) ctx := testContext(t)
cli := k2v.NewClient(endpoint, k2v.KeyFromEnv()) cli := k2v.NewClient(k2v.EndpointFromEnv(), k2v.KeyFromEnv())
t.Cleanup(cli.Close) t.Cleanup(cli.Close)
f := &fixture{ f := &fixture{