MockForge gRPC
Flexible gRPC mocking and service discovery for MockForge.
Features
- Dynamic Proto Discovery: Automatically discovers and compiles
.protofiles from configurable directories - Flexible Service Registration: Register and mock any gRPC service without hardcoding
- Reflection Support: Built-in gRPC reflection for service discovery
- Environment Configuration: Configure proto directories via environment variables
Quick Start
Basic Usage
use ;
async
Flexible Configuration
use ;
async
Environment Variables
MOCKFORGE_PROTO_DIR: Directory containing.protofiles (default:proto/)MOCKFORGE_GRPC_PORT: gRPC server port (default:50051)
Proto File Discovery
The build system automatically discovers all .proto files in the configured directory and subdirectories. This means:
- No hardcoded proto files: The system finds proto files dynamically
- Recursive discovery: Searches subdirectories for proto files
- Automatic compilation: All discovered proto files are compiled to Rust code
- Change detection: Rebuilds when proto files change
Directory Structure
your-project/
├── proto/ # Default proto directory
│ ├── service1.proto # Will be discovered
│ ├── service2.proto # Will be discovered
│ └── subdir/
│ └── service3.proto # Will be discovered
├── src/
└── Cargo.toml
Custom Proto Directory
Set the MOCKFORGE_PROTO_DIR environment variable to use a different directory:
Examples
See the examples/ directory for complete examples:
flexible_grpc.rs: Demonstrates dynamic service discoveryreflection_example.rs: Shows how to use gRPC reflection
Migration from Hardcoded Approach
If you're migrating from the old hardcoded approach:
- Remove hardcoded proto references from your
build.rs - Set
MOCKFORGE_PROTO_DIRto point to your proto files - Use
DynamicGrpcConfigfor advanced configuration - Update your service registration to use the dynamic system
Advanced Configuration
use ;
// Create custom service implementations
// Register services dynamically
let mut registry = new;
registry.register;
Troubleshooting
No Proto Files Found
If you see "No .proto files found", check:
- The
MOCKFORGE_PROTO_DIRenvironment variable - That the directory exists and contains
.protofiles - File permissions on the proto directory
Compilation Errors
If proto compilation fails:
- Check that your
.protofiles are valid - Ensure all dependencies are properly imported
- Verify that the proto directory structure is correct
Service Not Found
If services aren't being discovered:
- Check the service names in your proto files
- Verify that the proto files are being compiled
- Check the logs for discovery information