zadmin/cmd/client/main.go

57 lines
1.2 KiB
Go

package main
import (
"log"
"sync"
"time"
"git.mziesel.nl/mans/zadmin/internal/agent"
"git.mziesel.nl/mans/zadmin/internal/models"
"github.com/nats-io/nats.go"
)
var DefaultAgentConfig models.MachineAgentConfig = models.MachineAgentConfig{
RESTServerHostname: "localhost",
RESTServerPort: 443,
NATSServerUrl: nats.DefaultURL,
HelloInterval: 30,
MetricsInterval: 30,
GetIPv4Endpoint: "https://ip4.mziesel.nl/json",
GetIPv6Endpoint: "https://ip6.mziesel.nl/json",
}
func main() {
agentConfig := DefaultAgentConfig
nc, err := nats.Connect(agentConfig.NATSServerUrl)
if err != nil {
log.Fatalf("failed to connect to nats-server: %s\n", err)
}
log.Println("connected to nats-server")
defer func() {
nc.Publish("client/disconnected", []byte("Goodbye!"))
nc.Close()
}()
nc.Publish("client/connected", []byte("Hello!"))
nc.Publish("client/hello", []byte("Hello!"))
var wg sync.WaitGroup
shouldExit := false
wg.Add(1)
go func() {
defer wg.Done()
for !shouldExit {
agent.PublishMachineData(agentConfig, nc)
time.Sleep(30 * time.Second)
}
}()
wg.Wait()
nc.Publish("client/disconnected", []byte("Goodbye!"))
nc.Close()
}