Skip to content

SAGSINs/sagsin-be

Repository files navigation

SAGSIN Backend

NestJS backend server cung cấp REST API, gRPC services, và WebSocket cho hệ thống mạng phân tán SAGSIN.

🎯 Giới Thiệu

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

🏗️ Kiến Trúc

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

🔌 Ports & Services

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)

🚀 Hướng Dẫn Chạy

Docker (Production)

# Build image
docker build -t sagsin-be .

# View logs
docker logs -f sagsin-be

Development

🌍 Environment Variables

# 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

📊 Kết Quả Đạt Được

✅ Core Features

  1. Multi-Protocol Support: REST API + gRPC + WebSocket trong 1 server
  2. Real-time Monitoring: Heartbeat stream với bi-directional communication
  3. Timeline Tracking: Millisecond-precision file transfer tracking
  4. Auto-Discovery: Nodes tự động register qua heartbeat
  5. Link Management: Dynamic topology updates với metrics
  6. Heuristic Integration: Seamless routing optimization
  7. Swagger Documentation: Auto-generated API docs tại /api
  8. Health Checks: Monitoring endpoint cho orchestration

📈 Technical Achievements

  • 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

📝 Notes

  • NestJS framework với dependency injection pattern
  • MongoDB với Mongoose ODM và schema validation
  • gRPC sử dụng @grpc/grpc-js package
  • 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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors