Files
mesh-networking/modules/route-proxy/cmd/route-provider/main.go
T

57 lines
2.0 KiB
Go

// route-provider: route-proxy's Go half, one binary the node's runtime launches and speaks MCP to over stdio
// through the Go SDK. It runs the SDK's provider helper that answers the consumers of `route` (issue 792), and serves
// the module's tools, which read what the proxy keeps on the bus (issue 774, tools.go).
//
// stdout is the MCP channel, so everything this module says, it says on stderr.
package main
import (
"context"
"fmt"
"os"
"time"
"git.novox.be/novox/mesh-contracts/provisioning/go/clients/meshprovisionerclient/provider"
meshprovisioner "git.novox.be/novox/mesh-contracts/provisioning/go/seats/meshprovisioner"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/busretry"
"git.novox.be/novox/mesh-sdk/go/seatlist"
"route-proxy/contract"
)
func say(format string, args ...any) {
fmt.Fprintf(os.Stderr, "[route-proxy] "+format+"\n", args...)
}
func main() {
node := os.Getenv("MESH_NODE")
if node == "" {
say("MESH_NODE is not set; answering no consumers")
} else {
go func() {
// The helper's watches go through the runtime's channel, which Serve holds: one asked before it does is
// refused ("before Serve"), Run returns and the helper stays stopped until the process restarts.
<-stdio.Served()
// The holder of the seat mesh-provisioner is read from the seat that places modules, not named here (A-116).
holder, err := busretry.DoRepeatable("who holds "+meshprovisioner.Seat, func() (string, error) {
return seatlist.HolderOf(stdio.Ask, meshprovisioner.Seat)
}, nil)
if err != nil {
say("the provider helper cannot start: %v", err)
return
}
p := newProvider(node, holder, provider.StateBus(ModuleName, holder), say)
if err := p.Run(context.Background()); err != nil {
say("the provider helper stopped: %v", err)
}
}()
}
tools := front{routes: stdio.State(contract.RoutesState), unrouted: stdio.State(contract.UnroutedState),
now: time.Now}.tools()
if err := stdio.Serve("", tools); err != nil {
say("%v", err)
os.Exit(1)
}
}