Skip to content

Latest commit

 

History

History
288 lines (231 loc) · 10.5 KB

File metadata and controls

288 lines (231 loc) · 10.5 KB

Rust Reference API

Table of Contents


FEATURE-COMPLETE IMPLEMENTATION

Production-ready Rust implementation with 100% feature parity with Python, Go, Node.js, and TypeScript reference APIs.

Purpose: Demonstrates production-quality Rust patterns with Actix-web framework, comprehensive infrastructure integration, type safety, async/await, zero-cost abstractions, and world-class error handling following Rust best practices.

What's Implemented

Core Infrastructure (100%):

  • Actix-web server with full routing and middleware
  • CORS middleware properly configured
  • Async/await patterns with Tokio runtime
  • Type-safe structs with Serde serialization/deserialization
  • Environment configuration for flexible deployment
  • Structured logging with env_logger
  • CI/CD integration (cargo fmt, cargo clippy, comprehensive tests)

Health Checks (100%):

  • ✅ Simple health check (/health/)
  • ✅ Vault health check with connectivity verification
  • ✅ PostgreSQL health with version detection
  • ✅ MySQL health with version detection
  • ✅ MongoDB health with ping verification
  • ✅ Redis health with PING command
  • ✅ RabbitMQ health with connection test
  • ✅ Aggregate health check (/health/all) for all services

Vault Integration (100%):

  • ✅ Secret retrieval by service (/examples/vault/secret/{service})
  • ✅ Secret key extraction (/examples/vault/secret/{service}/{key})
  • ✅ Credential management for all database/cache/messaging services
  • ✅ Proper error handling for Vault unavailability

Database Integration (100%):

  • PostgreSQL - Full integration with credential fetching, queries, connection management
  • MySQL - Complete async driver integration with mysql_async
  • MongoDB - Document operations with mongodb driver
  • ✅ All databases use Vault-managed credentials

Cache Integration (100%):

  • Redis - Full CRUD operations (GET, SET, DELETE)
  • ✅ TTL support with SETEX command
  • ✅ Vault-managed Redis credentials
  • ✅ Proper connection pooling with multiplexed async connections

Messaging Integration (100%):

  • RabbitMQ - Message publishing with queue declaration
  • ✅ Queue info endpoint
  • ✅ Vault-managed RabbitMQ credentials
  • ✅ Proper connection lifecycle management

Redis Cluster Support (100%):

  • ✅ Cluster nodes listing (/redis/cluster/nodes)
  • ✅ Cluster slots information (/redis/cluster/slots)
  • ✅ Cluster health/info (/redis/cluster/info)
  • ✅ Per-node information (/redis/nodes/{node_name}/info)

Metrics & Observability (100%):

  • Prometheus metrics with real instrumentation
  • ✅ HTTP request counter (by method, endpoint, status)
  • ✅ HTTP request duration histogram (by method, endpoint)
  • ✅ Prometheus text format export (/metrics)

Testing (100%):

  • 44 comprehensive unit tests in src/tests.rs
  • ✅ Positive test cases for all endpoints
  • ✅ Negative test cases (404, 400, 503 scenarios)
  • ✅ Edge cases (empty values, special characters, long inputs)
  • ✅ All tests follow Rust best practices
  • ✅ No unwrap() calls in production code (100% compliance with PR #28)

Error Handling (100%):

  • ✅ Zero unwrap() calls in production code
  • ✅ Proper use of Result<T, E> throughout
  • ✅ Graceful error responses with appropriate HTTP status codes
  • ✅ Error context preservation with descriptive messages
  • ✅ Safe fallbacks with unwrap_or_else(), unwrap_or(), expect() (initialization only)

Implementation Highlights

  • 1,667 lines of production-ready Rust code in src/main.rs
  • 647 lines of comprehensive tests in src/tests.rs
  • Zero unsafe code - 100% safe Rust
  • Zero unwrap() calls in production paths (per CLAUDE.md guidelines)
  • Type-safe - Compile-time guarantees prevent entire classes of bugs
  • High performance - Zero-cost abstractions with async I/O
  • Memory safe - No null pointer dereferences, no buffer overflows
  • Concurrent - Safe multi-threading with Rust's ownership system

Core Features

  • Actix-web: High-performance async web framework with full middleware support
  • Complete Infrastructure Integration: PostgreSQL, MySQL, MongoDB, Redis, RabbitMQ
  • Health Monitoring: Comprehensive health checks for all services
  • Vault Integration: Credential management with HashiCorp Vault
  • Type Safety: Rust's compile-time guarantees eliminating runtime errors
  • Redis Cluster: Full support for Redis cluster operations
  • Prometheus Metrics: Real instrumentation for observability
  • Performance: Zero-cost abstractions with async I/O for maximum efficiency
  • Testing: 44 comprehensive tests covering positive, negative, and edge cases
  • Error Handling: Zero unwrap() calls, production-grade error patterns
  • Memory Safety: Guaranteed by Rust's ownership system
  • CORS: Properly configured cross-origin resource sharing

Quick Start

# Start the Rust reference API
docker compose up -d rust-api

# Test root endpoint
curl http://localhost:8004/

# Health checks
curl http://localhost:8004/health/
curl http://localhost:8004/health/all
curl http://localhost:8004/health/vault
curl http://localhost:8004/health/postgres
curl http://localhost:8004/health/mysql
curl http://localhost:8004/health/mongodb
curl http://localhost:8004/health/redis
curl http://localhost:8004/health/rabbitmq

# Vault examples
curl http://localhost:8004/examples/vault/secret/postgres
curl http://localhost:8004/examples/vault/secret/postgres/user

# Database examples
curl http://localhost:8004/examples/database/postgres/query
curl http://localhost:8004/examples/database/mysql/query
curl http://localhost:8004/examples/database/mongodb/query

# Cache examples
curl http://localhost:8004/examples/cache/mykey
curl -X POST http://localhost:8004/examples/cache/mykey \
  -H "Content-Type: application/json" \
  -d '{"value": "myvalue", "ttl": 60}'
curl -X DELETE http://localhost:8004/examples/cache/mykey

# Messaging examples
curl -X POST http://localhost:8004/examples/messaging/publish/myqueue \
  -H "Content-Type: application/json" \
  -d '{"message": "Hello from Rust!"}'
curl http://localhost:8004/examples/messaging/queue/myqueue/info

# Redis cluster
curl http://localhost:8004/redis/cluster/nodes
curl http://localhost:8004/redis/cluster/slots
curl http://localhost:8004/redis/cluster/info
curl http://localhost:8004/redis/nodes/redis-1/info

# Metrics
curl http://localhost:8004/metrics

API Endpoints

Core Endpoints

  • GET / - API information and endpoint directory
  • GET /metrics - Prometheus metrics (text format)

Health Checks

  • GET /health/ - Simple health check
  • GET /health/all - Aggregate health status for all services
  • GET /health/vault - Vault connectivity and health
  • GET /health/postgres - PostgreSQL connection and version
  • GET /health/mysql - MySQL connection and version
  • GET /health/mongodb - MongoDB connection and ping
  • GET /health/redis - Redis connection and PING
  • GET /health/rabbitmq - RabbitMQ connection test

Vault Integration

  • GET /examples/vault/secret/{service} - Retrieve all secrets for a service
  • GET /examples/vault/secret/{service}/{key} - Retrieve specific secret key

Database Examples

  • GET /examples/database/postgres/query - Execute PostgreSQL test query
  • GET /examples/database/mysql/query - Execute MySQL test query
  • GET /examples/database/mongodb/query - Execute MongoDB test operation

Cache Examples

  • GET /examples/cache/{key} - Get cached value
  • POST /examples/cache/{key} - Set cached value (with optional TTL)
    • Body: {"value": "string", "ttl": 60} (ttl is optional)
  • DELETE /examples/cache/{key} - Delete cached value

Messaging Examples

  • POST /examples/messaging/publish/{queue} - Publish message to queue
    • Body: {"message": "string"}
  • GET /examples/messaging/queue/{queue_name}/info - Get queue information

Redis Cluster

  • GET /redis/cluster/nodes - List all cluster nodes
  • GET /redis/cluster/slots - Show cluster slot distribution
  • GET /redis/cluster/info - Cluster information and health
  • GET /redis/nodes/{node_name}/info - Information for specific node

Port

  • HTTP: 8004
  • HTTPS: 8447 (when TLS enabled)

Build

Development Build

cd reference-apps/rust
cargo build
./target/debug/devstack-core-rust-api

Release Build (Optimized)

cd reference-apps/rust
cargo build --release
./target/release/devstack-core-rust-api

With Docker

# Build image
docker compose build rust-api

# Run container
docker compose up -d rust-api

# View logs
docker compose logs -f rust-api

Testing

Run All Tests

cd reference-apps/rust
cargo test

Run Tests with Output

cargo test -- --nocapture

Run Tests Serially (for integration tests that share state)

cargo test -- --test-threads=1

Run Specific Test

cargo test test_health_simple_returns_200

Test Coverage

  • 44 unit tests covering all endpoints
  • Positive tests - Happy path validation
  • Negative tests - Error handling (404, 400, 503)
  • Edge cases - Empty values, special characters, long inputs
  • 100% unwrap() elimination - Production-safe error handling

Note

This implementation demonstrates production-quality Rust/Actix-web patterns with comprehensive infrastructure integration matching the feature set of the Python, Go, Node.js, and TypeScript reference APIs. It showcases:

  • Type Safety: Compile-time guarantees preventing null pointers, race conditions, and memory safety issues
  • Performance: Zero-cost abstractions with async I/O for high throughput
  • Reliability: No unwrap() calls in production code, proper error handling throughout
  • Testability: Comprehensive test coverage with positive, negative, and edge case validation
  • Production Readiness: Real Prometheus metrics, structured logging, complete infrastructure integration

The Rust implementation serves as both a reference for building production Rust APIs and a demonstration of how Rust's unique features (ownership, borrowing, lifetimes, zero-cost abstractions) enable building high-performance, memory-safe services.