42 lines
1.4 KiB
Go
42 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
"nats-tools/internal/busconn"
|
|
)
|
|
|
|
// LiveBus is the bus as the module's own credential reaches it. Nothing connects until a tool that changes the
|
|
// bus is called, so the tools that read (through the monitoring endpoint) answer as they did with no credential.
|
|
// MESH_BROKER_FILE names the credential (the process nats-tools's own secret `broker-nats-tools`); MESH_NATS_SERVER where the bus is as
|
|
// seen from this machine (default: its own port on loopback).
|
|
func LiveBus() Bus {
|
|
return func(ctx context.Context) (jetstream.JetStream, func(), error) {
|
|
file := env("MESH_BROKER_FILE", "")
|
|
if file == "" {
|
|
return nil, nil, errors.New("MESH_BROKER_FILE is not set, so this tool has no credential to reach the bus with (the module's manifest sets it)")
|
|
}
|
|
c, err := busconn.Read(file)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
nc, err := busconn.Connect(env("MESH_NATS_SERVER", "tls://127.0.0.1:4222"), c, "nats-tools")
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
js, err := jetstream.New(nc)
|
|
if err != nil {
|
|
nc.Close()
|
|
return nil, nil, fmt.Errorf("JetStream on the bus: %w", err)
|
|
}
|
|
return js, nc.Close, nil
|
|
}
|
|
}
|
|
|
|
// SaidByTheRuntime is the audit line: an event of this module, published through the runtime that launched it.
|
|
func SaidByTheRuntime(event string, body any) error { return stdio.Emit(event, body) }
|