f7b694854d97812fc00dc45dbd798fa8d93e791a
Reviewed-on: #3
SPORE Gateway
A gateway service providing UDP-based node discovery, cluster management, and HTTP API endpoints for SPORE devices.
Features
- UDP Node Discovery: Listens for heartbeat messages from SPORE nodes on port 4210
- Cluster Management: Tracks node status, manages primary node selection, and handles failover
- HTTP API Server: Provides REST endpoints for cluster information, node management, and proxy calls
- WebSocket Server: Real-time cluster updates via WebSocket connections
- Failover Logic: Automatic switching between SPORE nodes when primary fails
- Proxy Functionality: Generic proxy calls to SPORE node capabilities
Installation
go mod tidy
go build
Usage
./spore-gateway [options]
Options:
-port string
HTTP server port (default "3001")
-udp-port string
UDP discovery port (default "4210")
-log-level string
Log level (debug, info, warn, error) (default "info")
Integration
The spore-gateway works together with the SPORE UI frontend:
- spore-gateway: Runs on port 3001, handles UDP discovery, API endpoints, and WebSocket connections
- spore-ui: Runs on port 3000, serves the frontend interface and connects to spore-gateway for all backend functionality
Start spore-gateway first, then start the frontend:
# Terminal 1 - Start backend
./spore-gateway
# Terminal 2 - Start frontend
cd ../spore-ui && npm start
Access the UI at: http://localhost:3000
API Endpoints
Discovery
GET /api/discovery/nodes- Get discovered nodes and cluster statusPOST /api/discovery/refresh- Refresh cluster statePOST /api/discovery/random-primary- Select random primary nodePOST /api/discovery/primary/{ip}- Set specific node as primary
Cluster
GET /api/cluster/members- Get cluster member informationPOST /api/cluster/refresh- Trigger cluster refresh
Tasks
GET /api/tasks/status- Get task status (optionally for specific node)
Nodes
GET /api/node/status- Get system statusGET /api/node/status/{ip}- Get system status for specific nodeGET /api/node/endpoints- Get available API endpointsPOST /api/node/update- Upload firmware to node
Proxy
POST /api/proxy-call- Make generic proxy call to SPORE node
Testing
POST /api/test/websocket- Test WebSocket broadcasting
Health
GET /api/health- Health check endpoint
WebSocket
Connect to /ws for real-time cluster updates.
Development
The application follows the same patterns as the original Node.js spore-ui server but is implemented in Go with:
- Structured logging using logrus
- Graceful shutdown handling
- Concurrent-safe node management
- HTTP middleware for CORS and logging
- WebSocket support for real-time updates
Architecture
main.go- Application entry pointinternal/discovery/- UDP-based node discoveryinternal/server/- HTTP API serverinternal/websocket/- WebSocket server for real-time updatespkg/client/- SPORE API clientpkg/config/- Configuration management
Description
Languages
Go
94.7%
Shell
4%
Makefile
0.9%
Dockerfile
0.4%