NATS has no auth configured, so NATS_TOKEN was a placeholder that confused users. Made it optional in the Go agent (default empty) and removed it from Quick Setup commands. Also removed GAME_SERVER_PATH since one-click deploy handles server installation. License key already auto-populates from auth store after previous commit. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
)
|
|
|
|
// Connect establishes a connection to NATS with optional token authentication
|
|
// and automatic reconnection handling
|
|
func Connect(url, token string) (*nats.Conn, error) {
|
|
opts := []nats.Option{
|
|
nats.Name("corrosion-companion"),
|
|
nats.MaxReconnects(-1),
|
|
nats.ReconnectWait(2 * time.Second),
|
|
nats.DisconnectErrHandler(func(nc *nats.Conn, err error) {
|
|
if err != nil {
|
|
fmt.Printf("NATS disconnected: %v\n", err)
|
|
}
|
|
}),
|
|
nats.ReconnectHandler(func(nc *nats.Conn) {
|
|
fmt.Printf("NATS reconnected to %s\n", nc.ConnectedUrl())
|
|
}),
|
|
nats.ClosedHandler(func(nc *nats.Conn) {
|
|
fmt.Println("NATS connection closed")
|
|
}),
|
|
nats.ErrorHandler(func(nc *nats.Conn, sub *nats.Subscription, err error) {
|
|
fmt.Printf("NATS error: %v\n", err)
|
|
}),
|
|
}
|
|
|
|
// Only use token auth if a token is provided
|
|
if token != "" {
|
|
opts = append(opts, nats.Token(token))
|
|
}
|
|
|
|
nc, err := nats.Connect(url, opts...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to connect to NATS: %w", err)
|
|
}
|
|
|
|
return nc, nil
|
|
}
|