Expand description
§Cap’n Web Server Library
Server implementation for the Cap’n Web RPC protocol.
This crate provides a production-ready server with:
- Capability registration and lifecycle management
- Automatic batching and pipelining support
- Multiple transport protocols (HTTP, WebSocket, WebTransport)
- Rate limiting and connection management
§Quick Start
ⓘ
use capnweb_server::{Server, ServerConfig};
use capnweb_core::{CapId, RpcError, Value};
use capnweb_core::RpcTarget; // Use RpcTarget from core, not server
use async_trait::async_trait;
use std::sync::Arc;
use serde_json::json;
#[derive(Debug)]
struct HelloService;
#[async_trait]
impl RpcTarget for HelloService {
async fn call(&self, method: &str, args: Vec<Value>) -> Result<Value, RpcError> {
match method {
"greet" => {
// Note: Value is from capnweb_core, not serde_json
Ok(Value::String(format!("Hello, World!")))
}
_ => Err(RpcError::not_found("method not found"))
}
}
async fn get_property(&self, _property: &str) -> Result<Value, RpcError> {
Err(RpcError::not_found("property access not implemented"))
}
}
// Create server configuration
let config = ServerConfig {
port: 8080,
host: "127.0.0.1".to_string(),
max_batch_size: 100,
};
// Create and configure server
let server = Server::new(config);
server.register_capability(
CapId::new(1),
Arc::new(HelloService)
);
// Run the server
server.run().await;
§Capability Registration
Register multiple capabilities with different IDs:
ⓘ
server.register_capability(CapId::new(1), Arc::new(AuthService));
server.register_capability(CapId::new(2), Arc::new(DataService));
server.register_capability(CapId::new(3), Arc::new(AdminService));
§Transport Configuration
The server supports multiple transport protocols:
- HTTP Batch: Default transport at
/rpc/batch
- WebSocket: Real-time bidirectional communication (with feature flag)
- WebTransport: HTTP/3-based transport (with feature flag)
Re-exports§
pub use wire_server::RpcTargetAdapter;
pub use wire_server::WireCapability;
pub use wire_server::WireServer;
pub use wire_server::WireServerConfig;
pub use advanced_capability::AdvancedCapability;
pub use advanced_capability::AdvancedCapabilityBuilder;
pub use advanced_capability::AdvancedCapabilityConfig;
pub use cap_table::CapTable;
pub use capnweb_server::CapnWebServer as NewCapnWebServer;
pub use capnweb_server::CapnWebServerConfig;
pub use lifecycle::CapabilityLifecycle;
pub use lifecycle::Disposable;
pub use lifecycle::LifecycleStats;
pub use limits::RateLimits;
pub use logging::init_logging;
pub use logging::init_test_logging;
pub use promise_table::PromiseTable;
pub use promise_table::PromiseTableStats;
pub use runner::PlanRunner;
pub use server::RpcTarget;
pub use server::Server;
pub use server::ServerConfig;