use allframe_core::router::{grpc_explorer_html, GrpcExplorerConfig, GrpcExplorerTheme, Router};
fn main() {
println!("🚀 AllFrame gRPC Documentation Example");
println!("========================================\n");
let _router = Router::new();
println!("✅ Router created\n");
println!("🎨 Configuring gRPC Explorer...");
let grpc_config = GrpcExplorerConfig::new()
.server_url("http://localhost:50051")
.enable_reflection(true)
.enable_tls(false)
.theme(GrpcExplorerTheme::Dark)
.timeout_seconds(30)
.add_header("Authorization", "Bearer your-token-here")
.add_header("X-API-Version", "v1")
.custom_css(
r#"
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}
.grpc-explorer {
--color-primary: 96, 165, 250;
}
"#,
);
println!(" • Server: http://localhost:50051");
println!(" • Reflection: Enabled");
println!(" • TLS: Disabled (development mode)");
println!(" • Theme: Dark mode");
println!(" • Timeout: 30 seconds");
println!(" • Custom headers: 2 configured");
println!(" • Custom CSS: Applied\n");
println!("🎭 Generating gRPC Explorer HTML documentation...");
let grpc_html = grpc_explorer_html(&grpc_config, "AllFrame gRPC API");
println!(
"✅ gRPC Explorer HTML generated ({} bytes)\n",
grpc_html.len()
);
println!("📦 Generated documentation includes:");
println!(" ✅ Interactive gRPC service explorer");
println!(" ✅ Service and method browser");
println!(" ✅ Request builder with JSON input");
println!(" ✅ Real-time request/response testing");
println!(" ✅ gRPC reflection support");
println!(" ✅ Stream testing (server/client/bidirectional)");
println!(" ✅ Dark theme by default");
println!();
println!("🚀 Next Steps:");
println!(" 1. Integrate with your web framework (Axum, Actix, etc.)");
println!(" 2. Serve the HTML at /grpc/explorer");
println!(" 3. Implement gRPC server with reflection");
println!(" 4. Start testing your gRPC services!");
println!();
println!("💡 Example Integration (Axum with Tonic):");
println!(
r#"
use axum::{{routing::get, Router, response::Html}};
use tonic::{{transport::Server, Request, Response, Status}};
use tonic_reflection::server::Builder;
// Define your gRPC service
pub mod hello {{
tonic::include_proto!("hello");
}}
use hello::greeter_server::{{Greeter, GreeterServer}};
use hello::{{HelloRequest, HelloReply}};
#[derive(Default)]
pub struct MyGreeter {{}}
#[tonic::async_trait]
impl Greeter for MyGreeter {{
async fn say_hello(
&self,
request: Request<HelloRequest>,
) -> Result<Response<HelloReply>, Status> {{
let name = request.into_inner().name;
Ok(Response::new(HelloReply {{
message: format!("Hello {{}}!", name),
}}))
}}
}}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {{
// Start gRPC server with reflection on port 50051
let grpc_addr = "0.0.0.0:50051".parse()?;
let greeter = MyGreeter::default();
let service = GreeterServer::new(greeter);
// Enable gRPC reflection
let reflection_service = Builder::configure()
.register_encoded_file_descriptor_set(
hello::FILE_DESCRIPTOR_SET
)
.build()?;
tokio::spawn(async move {{
Server::builder()
.add_service(reflection_service)
.add_service(service)
.serve(grpc_addr)
.await
.unwrap();
}});
println!("🔌 gRPC Server: http://localhost:50051");
// Start HTTP server for gRPC Explorer on port 3000
let app = Router::new()
.route("/grpc/explorer", get(|| async {{
Html(grpc_html)
}}));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
.await?;
println!("📚 gRPC Explorer: http://localhost:3000/grpc/explorer");
axum::serve(listener, app).await?;
Ok(())
}}
"#
);
println!("\n💡 Example Proto File (hello.proto):");
println!(
r#"
syntax = "proto3";
package hello;
// The greeting service definition
service Greeter {{
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {{}}
// Server streaming
rpc SayHelloStream (HelloRequest) returns (stream HelloReply) {{}}
// Client streaming
rpc SayHelloClientStream (stream HelloRequest) returns (HelloReply) {{}}
// Bidirectional streaming
rpc SayHelloBidirectional (stream HelloRequest) returns (stream HelloReply) {{}}
}}
// The request message
message HelloRequest {{
string name = 1;
}}
// The response message
message HelloReply {{
string message = 1;
}}
"#
);
println!("\n💡 Example gRPC Requests to Try:");
println!(
r#"
// Unary call
{{
"name": "World"
}}
// Server streaming - same request, multiple responses
{{
"name": "Alice"
}}
// Client streaming - multiple requests, single response
[
{{ "name": "Bob" }},
{{ "name": "Charlie" }},
{{ "name": "David" }}
]
// Bidirectional streaming - multiple requests and responses
[
{{ "name": "User1" }},
{{ "name": "User2" }}
]
"#
);
println!("\n📋 Setting up gRPC Reflection:");
println!(" 1. Add tonic-reflection to Cargo.toml:");
println!(" tonic-reflection = \"0.11\"");
println!();
println!(" 2. Include file descriptor set in build.rs:");
println!(
r#"
tonic_build::configure()
.file_descriptor_set_path("proto_descriptor.bin")
.compile(&["proto/hello.proto"], &["proto"])?;
"#
);
println!();
println!(" 3. Include descriptor set in your service:");
println!(
r#"
const FILE_DESCRIPTOR_SET: &[u8] =
include_bytes!("../proto_descriptor.bin");
"#
);
println!("\n⚙️ Available Configuration Options:");
println!(" • server_url: gRPC server URL");
println!(" • enable_reflection: Enable service discovery");
println!(" • enable_tls: Enable TLS/SSL");
println!(" • theme: Light or Dark");
println!(" • timeout_seconds: Request timeout");
println!(" • add_header: Add custom metadata headers");
println!(" • custom_css: Inject custom styling");
println!();
println!("📊 gRPC Explorer Features:");
println!(" Feature | Status");
println!(" ─────────────────────────────");
println!(" Service Discovery | ✓ (via reflection)");
println!(" Unary Calls | ✓");
println!(" Server Streaming | ✓");
println!(" Client Streaming | ✓");
println!(" Bidirectional | ✓");
println!(" Custom Metadata | ✓");
println!(" TLS Support | ✓");
println!(" Dark Mode | ✓");
println!();
println!("🔧 Troubleshooting:");
println!(" Problem: Can't connect to gRPC server");
println!(" Solution: Ensure server is running on specified URL");
println!();
println!(" Problem: No services showing up");
println!(" Solution: Enable gRPC reflection in your server");
println!();
println!(" Problem: CORS errors in browser");
println!(" Solution: Use gRPC-Web proxy (e.g., Envoy)");
println!();
println!("✨ Example complete!");
println!("\n🎯 Key Takeaways:");
println!(" 1. gRPC Explorer provides interactive gRPC documentation");
println!(" 2. Reflection enables automatic service discovery");
println!(" 3. Supports all gRPC call types (unary, streaming)");
println!(" 4. Customizable theming and styling");
println!(" 5. Framework-agnostic - works with any Rust web framework");
println!(" 6. Production-ready with TLS and authentication support");
}