capnweb_server/
lib.rs

1//! # Cap'n Web Server Library
2//!
3//! Server implementation for the Cap'n Web RPC protocol.
4//!
5//! This crate provides a production-ready server with:
6//! - Capability registration and lifecycle management
7//! - Automatic batching and pipelining support
8//! - Multiple transport protocols (HTTP, WebSocket, WebTransport)
9//! - Rate limiting and connection management
10//!
11//! ## Quick Start
12//!
13//! ```rust,ignore
14//! use capnweb_server::{Server, ServerConfig};
15//! use capnweb_core::{CapId, RpcError, Value};
16//! use capnweb_core::RpcTarget;  // Use RpcTarget from core, not server
17//! use async_trait::async_trait;
18//! use std::sync::Arc;
19//! use serde_json::json;
20//!
21//! #[derive(Debug)]
22//! struct HelloService;
23//!
24//! #[async_trait]
25//! impl RpcTarget for HelloService {
26//!     async fn call(&self, method: &str, args: Vec<Value>) -> Result<Value, RpcError> {
27//!         match method {
28//!             "greet" => {
29//!                 // Note: Value is from capnweb_core, not serde_json
30//!                 Ok(Value::String(format!("Hello, World!")))
31//!             }
32//!             _ => Err(RpcError::not_found("method not found"))
33//!         }
34//!     }
35//!
36//!     async fn get_property(&self, _property: &str) -> Result<Value, RpcError> {
37//!         Err(RpcError::not_found("property access not implemented"))
38//!     }
39//! }
40//!
41//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
42//! // Create server configuration
43//! let config = ServerConfig {
44//!     port: 8080,
45//!     host: "127.0.0.1".to_string(),
46//!     max_batch_size: 100,
47//! };
48//!
49//! // Create and configure server
50//! let server = Server::new(config);
51//! server.register_capability(
52//!     CapId::new(1),
53//!     Arc::new(HelloService)
54//! );
55//!
56//! // Run the server
57//! server.run().await;
58//! # Ok(())
59//! # }
60//! ```
61//!
62//! ## Capability Registration
63//!
64//! Register multiple capabilities with different IDs:
65//!
66//! ```rust,ignore
67//! # use capnweb_server::Server;
68//! # use capnweb_core::CapId;
69//! # use std::sync::Arc;
70//! # struct AuthService;
71//! # struct DataService;
72//! # struct AdminService;
73//! # let server = Server::new(Default::default()).unwrap();
74//! server.register_capability(CapId::new(1), Arc::new(AuthService));
75//! server.register_capability(CapId::new(2), Arc::new(DataService));
76//! server.register_capability(CapId::new(3), Arc::new(AdminService));
77//! ```
78//!
79//! ## Transport Configuration
80//!
81//! The server supports multiple transport protocols:
82//!
83//! - **HTTP Batch**: Default transport at `/rpc/batch`
84//! - **WebSocket**: Real-time bidirectional communication (with feature flag)
85//! - **WebTransport**: HTTP/3-based transport (with feature flag)
86
87// Official Cap'n Web wire protocol server
88pub mod server_wire_handler;
89pub mod wire_server;
90
91// Legacy servers (TO BE REMOVED - only wire protocol should be used)
92pub mod advanced_capability;
93pub mod cap_table;
94pub mod capnweb_server;
95#[cfg(feature = "h3-server")]
96pub mod h3_server;
97pub mod lifecycle;
98pub mod limits;
99pub mod logging;
100pub mod promise_table;
101pub mod runner;
102pub mod server;
103#[cfg(feature = "all-transports")]
104pub mod ws_h1;
105#[cfg(feature = "all-transports")]
106pub mod ws_wire;
107
108// Primary exports: Official Cap'n Web wire protocol
109pub use wire_server::{RpcTargetAdapter, WireCapability, WireServer, WireServerConfig};
110
111// Legacy exports
112pub use advanced_capability::{
113    AdvancedCapability, AdvancedCapabilityBuilder, AdvancedCapabilityConfig,
114};
115pub use cap_table::CapTable;
116pub use capnweb_server::{CapnWebServer as NewCapnWebServer, CapnWebServerConfig};
117pub use lifecycle::{CapabilityLifecycle, Disposable, LifecycleStats};
118pub use limits::RateLimits;
119pub use logging::{init_logging, init_test_logging};
120pub use promise_table::{PromiseTable, PromiseTableStats};
121pub use runner::PlanRunner;
122pub use server::{RpcTarget, Server, ServerConfig};