1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! # MockForge gRPC
//!
//! Flexible gRPC mocking library with dynamic service discovery and HTTP bridge.
//!
//! This crate provides comprehensive gRPC mocking capabilities with:
//!
//! - **Dynamic Service Discovery**: Auto-discover and mock services from `.proto` files
//! - **HTTP Bridge**: Expose gRPC services as REST APIs with OpenAPI documentation
//! - **gRPC Reflection**: Built-in server reflection for service discovery
//! - **Streaming Support**: Full support for unary, server, client, and bidirectional streaming
//! - **Protocol Buffer Parsing**: Runtime parsing of `.proto` files without code generation
//!
//! ## Overview
//!
//! MockForge gRPC eliminates the need to hardcode service implementations. Simply provide
//! `.proto` files, and MockForge will automatically:
//!
//! 1. Parse protobuf definitions
//! 2. Generate mock service implementations
//! 3. Handle all RPC methods (unary and streaming)
//! 4. Optionally expose as REST APIs via HTTP Bridge
//!
//! ## Quick Start
//!
//! ### Basic gRPC Server
//!
//! ```rust,no_run
//! use mockforge_grpc::start;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! // Start gRPC server on port 50051
//! // Automatically discovers .proto files in ./proto directory
//! start(50051).await?;
//! Ok(())
//! }
//! ```
//!
//! ### With Custom Configuration
//!
//! ```rust,no_run
//! use mockforge_grpc::{start_with_config, DynamicGrpcConfig};
//! use mockforge_grpc::dynamic::http_bridge::HttpBridgeConfig;
//! use mockforge_core::LatencyProfile;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! let config = DynamicGrpcConfig {
//! proto_dir: "./my-protos".to_string(),
//! enable_reflection: true,
//! excluded_services: vec!["ExperimentalService".to_string()],
//! http_bridge: Some(HttpBridgeConfig {
//! enabled: true,
//! base_path: "/api".to_string(),
//! ..Default::default()
//! }),
//! ..Default::default()
//! };
//!
//! let latency = Some(LatencyProfile::with_normal_distribution(120, 35.0));
//! start_with_config(50051, latency, config).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### HTTP Bridge Mode
//!
//! Expose gRPC services as REST APIs:
//!
//! ```rust,no_run
//! use mockforge_grpc::{start_with_config, DynamicGrpcConfig};
//! use mockforge_grpc::dynamic::http_bridge::HttpBridgeConfig;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! let config = DynamicGrpcConfig {
//! proto_dir: "./proto".to_string(),
//! http_bridge: Some(HttpBridgeConfig {
//! enabled: true,
//! base_path: "/api".to_string(),
//! ..Default::default()
//! }),
//! ..Default::default()
//! };
//!
//! start_with_config(50051, None, config).await?;
//! // Now accessible via:
//! // - gRPC: localhost:50051
//! // - REST: http://localhost:8080/api/{service}/{method}
//! // - OpenAPI: http://localhost:8080/api/docs
//! # Ok(())
//! # }
//! ```
//!
//! ### TLS/mTLS Configuration
//!
//! Enable TLS encryption for secure gRPC connections:
//!
//! ```rust,no_run
//! use mockforge_grpc::{start_with_config, DynamicGrpcConfig, GrpcTlsConfig};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! // Basic TLS
//! let config = DynamicGrpcConfig {
//! proto_dir: "./proto".to_string(),
//! tls: Some(GrpcTlsConfig::new("server.crt", "server.key")),
//! ..Default::default()
//! };
//!
//! // Or with mutual TLS (client certificate verification)
//! let mtls_config = DynamicGrpcConfig {
//! proto_dir: "./proto".to_string(),
//! tls: Some(GrpcTlsConfig::with_mtls("server.crt", "server.key", "client-ca.crt")),
//! ..Default::default()
//! };
//!
//! start_with_config(50051, None, config).await?;
//! # Ok(())
//! # }
//! ```
//!
//! TLS can also be configured via environment variables:
//! - `GRPC_TLS_CERT`: Path to server certificate (PEM format)
//! - `GRPC_TLS_KEY`: Path to server private key (PEM format)
//! - `GRPC_TLS_CLIENT_CA`: Optional path to client CA for mTLS
//!
//! ## Dynamic Service Discovery
//!
//! MockForge automatically discovers and mocks all services defined in your `.proto` files:
//!
//! ```protobuf
//! // user.proto
//! service UserService {
//! rpc GetUser(GetUserRequest) returns (GetUserResponse);
//! rpc ListUsers(ListUsersRequest) returns (stream User);
//! rpc CreateUser(stream CreateUserRequest) returns (CreateUserResponse);
//! rpc Chat(stream ChatMessage) returns (stream ChatMessage);
//! }
//! ```
//!
//! All four method types (unary, server streaming, client streaming, bidirectional) are
//! automatically supported without any code generation or manual implementation.
//!
//! ## gRPC Reflection
//!
//! Enable reflection for service discovery by gRPC clients:
//!
//! ```bash
//! # List services
//! grpcurl -plaintext localhost:50051 list
//!
//! # Describe a service
//! grpcurl -plaintext localhost:50051 describe UserService
//!
//! # Call a method
//! grpcurl -plaintext -d '{"user_id": "123"}' localhost:50051 UserService/GetUser
//! ```
//!
//! ## HTTP Bridge
//!
//! The HTTP Bridge automatically converts gRPC services to REST endpoints:
//!
//! ```bash
//! # gRPC call
//! grpcurl -d '{"user_id": "123"}' localhost:50051 UserService/GetUser
//!
//! # Equivalent HTTP call
//! curl -X POST http://localhost:8080/api/userservice/getuser \
//! -H "Content-Type: application/json" \
//! -d '{"user_id": "123"}'
//!
//! # OpenAPI documentation
//! curl http://localhost:8080/api/docs
//! ```
//!
//! ## Advanced Data Synthesis
//!
//! Generate realistic mock data using intelligent field inference:
//!
//! - Detects field types from names (`email`, `phone`, `id`, etc.)
//! - Maintains referential integrity across related messages
//! - Supports deterministic seeding for reproducible tests
//!
//! ## Key Modules
//!
//! - [`dynamic`]: Dynamic service discovery and mocking
//! - [`reflection`]: gRPC reflection protocol implementation
//! - [`registry`]: Service and method registry
//!
//! ## Examples
//!
//! See the [examples directory](https://github.com/SaaSy-Solutions/mockforge/tree/main/examples)
//! for complete working examples.
//!
//! ## Related Crates
//!
//! - [`mockforge-core`](https://docs.rs/mockforge-core): Core mocking functionality
//! - [`mockforge-data`](https://docs.rs/mockforge-data): Synthetic data generation
//!
//! ## Documentation
//!
//! - [MockForge Book](https://docs.mockforge.dev/)
//! - [gRPC Mocking Guide](https://docs.mockforge.dev/user-guide/grpc-mocking.html)
//! - [API Reference](https://docs.rs/mockforge-grpc)
use LatencyProfile;
/// Unified protocol server lifecycle implementation
/// Generated Protocol Buffer code from .proto files
///
/// This module contains auto-generated Rust code from Protocol Buffer definitions.
/// The generated code provides message types and service stubs for gRPC operations.
///
/// # Note
///
/// The generated code in this module is excluded from missing documentation checks
/// as it is automatically generated from `.proto` files and documentation should
/// be added to the source `.proto` files instead.
pub use ProtoService;
pub use MockResponse;
pub use ;
pub use ;
pub use GrpcProtoRegistry;
/// Start gRPC server with default configuration
pub async
/// Start gRPC server with latency configuration
pub async
/// Start gRPC server with custom configuration
pub async