NestJS backend server cung cấp REST API, gRPC services, và WebSocket cho hệ thống mạng phân tán SAGSIN.
SAGSIN Backend là trung tâm điều phối cho toàn bộ hệ thống, xử lý:
- Node Management: CRUD operations cho network nodes
- Heartbeat Monitoring: gRPC stream nhận metrics từ agents
- Timeline Tracking: File transfer progress tracking
- Link Management: Network topology và connectivity
- Heuristic Integration: Route optimization requests
- WebSocket Events: Real-time updates cho frontend
sagsin-be/
├── src/
│ ├── main.ts # Bootstrap (REST API + gRPC + Swagger)
│ ├── app.module.ts # Root module
│ │
│ ├── node/ # Node management
│ │ ├── node.controller.ts # REST endpoints
│ │ ├── node.service.ts # Business logic
│ │ ├── entities/ # Mongoose schemas
│ │ └── repository/ # Data access layer
│ │
│ ├── heartbeat/ # Heartbeat monitoring
│ │ ├── heartbeat.controller.ts # gRPC handler
│ │ ├── heartbeat.service.ts # Metrics processing
│ │ └── heartbeat.gateway.ts # WebSocket events
│ │
│ ├── timeline/ # File transfer tracking
│ │ ├── timeline.controller.ts # REST + gRPC
│ │ ├── timeline.service.ts # Timeline management
│ │ ├── grpc/ # gRPC server
│ │ └── entities/ # Mongoose schemas
│ │
│ ├── links/ # Network links
│ │ ├── links.controller.ts # REST endpoints
│ │ ├── links.service.ts # Link logic
│ │ └── entities/ # Mongoose schemas
│ │
│ ├── heuristic/ # Route optimization
│ │ ├── heuristic.client.ts # gRPC client
│ │ └── heuristic.service.ts # Integration layer
│ │
│ ├── health/ # Health checks
│ │ └── health.controller.ts # /health endpoint
│ │
│ ├── config/ # Configuration
│ │ ├── app.config.ts # App settings
│ │ └── config.type.ts # Type definitions
│ │
│ └── database/ # Database setup
│ ├── mongoose.config.ts # MongoDB config
│ └── config/ # DB settings
│
├── proto/
│ └── monitor.proto # gRPC protocol definitions
│
├── Dockerfile # Production build
├── nest-cli.json # NestJS configuration
├── tsconfig.json # TypeScript settings
└── package.json # Dependencies
| Port | Service | Description |
|---|---|---|
| 3000 | REST API | HTTP endpoints, Swagger UI |
| 50051 | Node gRPC | Heartbeat monitoring stream |
| 50053 | Timeline gRPC | File transfer tracking |
| 27017 | MongoDB | Database (external) |
# Build image
docker build -t sagsin-be .
# View logs
docker logs -f sagsin-be# Application
NODE_ENV=production
APP_PORT=3000
# gRPC
GRPC_PACKAGE=monitor
GRPC_PROTO_PATH=proto/monitor.proto
GRPC_URL=0.0.0.0:50051
TIMELINE_GRPC_PORT=0.0.0.0:50053
# Database
DATABASE_URL=mongodb://mongodb:27017/sagsin-db
# External Services
HEURISTIC_URL=192.168.100.3:50052
# Security
THROTTLER_TTL=60
THROTTLER_LIMIT=100- Multi-Protocol Support: REST API + gRPC + WebSocket trong 1 server
- Real-time Monitoring: Heartbeat stream với bi-directional communication
- Timeline Tracking: Millisecond-precision file transfer tracking
- Auto-Discovery: Nodes tự động register qua heartbeat
- Link Management: Dynamic topology updates với metrics
- Heuristic Integration: Seamless routing optimization
- Swagger Documentation: Auto-generated API docs tại
/api - Health Checks: Monitoring endpoint cho orchestration
- Concurrent Streams: Handle 100+ simultaneous gRPC connections
- Response Time: <50ms cho REST endpoints
- Throughput: 1000+ heartbeats/second
- Memory Usage: ~200MB baseline, ~500MB under load
- Database: MongoDB với indexing và autopopulate
- Error Handling: Comprehensive validation và error responses
- NestJS framework với dependency injection pattern
- MongoDB với Mongoose ODM và schema validation
- gRPC sử dụng
@grpc/grpc-jspackage - WebSocket sử dụng Socket.IO
- Swagger UI tự động generate từ decorators
- Throttling: 100 requests/60s per IP
- CORS enabled cho tất cả origins (development)
- Health check endpoint cho Docker healthcheck
Tech Stack: NestJS, TypeScript, MongoDB, gRPC, Socket.IO, Swagger
Docker Hub: baocules/sagsin-be
Node Version: 20.x LTS