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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! # WsForge - High-Performance WebSocket Framework for Rust
//!
//! WsForge is a complete, production-ready WebSocket framework that combines exceptional
//! performance with an intuitive, type-safe API. Built on `tokio-tungstenite`, it provides
//! everything needed to build real-time applications, from simple echo servers to complex
//! multiplayer games and chat platforms.
//!
//! ## Overview
//!
//! WsForge brings together the best practices from modern web frameworks like Axum and
//! combines them with the performance of Rust's async ecosystem. Whether you're building
//! a chat application, real-time dashboard, collaborative editor, or multiplayer game,
//! WsForge provides the tools you need.
//!
//! ## 🌟 Key Features
//!
//! - **🚀 High Performance**: Built on tokio-tungstenite with zero-copy optimizations
//! - **🔧 Type-Safe Extractors**: Automatic extraction of JSON, State, Connection info
//! - **🎯 Flexible Handlers**: Return String, Message, Result, JsonResponse, or ()
//! - **📡 Broadcasting**: Built-in broadcast, broadcast_except, and targeted messaging
//! - **⚡ Concurrent**: Lock-free connection management using DashMap
//! - **🔄 Lifecycle Hooks**: on_connect and on_disconnect callbacks
//! - **🌐 Hybrid Server**: Serve static files and WebSocket on the same port
//! - **🛡️ Type Safety**: Compile-time guarantees prevent common errors
//! - **🎨 Developer Friendly**: Intuitive API similar to popular Rust web frameworks
//! - **📦 Batteries Included**: Macros, examples, and documentation
//!
//! ## Quick Start
//!
//! Add WsForge to your `Cargo.toml`:
//!
//! ```
//! [dependencies]
//! wsforge = "0.1.0"
//! tokio = { version = "1.40", features = ["full"] }
//! serde = { version = "1.0", features = ["derive"] }
//! serde_json = "1.0"
//! ```
//!
//! ### Echo Server Example
//!
//! Create a simple echo server in just a few lines:
//!
//! ```
//! use wsforge::prelude::*;
//!
//! async fn echo(msg: Message) -> Result<Message> {
//! Ok(msg)
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! let router = Router::new()
//! .default_handler(handler(echo));
//!
//! println!("WebSocket server running on ws://127.0.0.1:8080");
//! router.listen("127.0.0.1:8080").await?;
//! Ok(())
//! }
//! ```
//!
//! ### Chat Server Example
//!
//! Build a real-time chat server with broadcasting:
//!
//! ```
//! use wsforge::prelude::*;
//! use std::sync::Arc;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Deserialize, Serialize)]
//! struct ChatMessage {
//! username: String,
//! text: String,
//! }
//!
//! async fn chat_handler(
//! Json(msg): Json<ChatMessage>,
//! conn: Connection,
//! State(manager): State<Arc<ConnectionManager>>,
//! ) -> Result<()> {
//! println!("{}: {}", msg.username, msg.text);
//!
//! // Broadcast to everyone except sender
//! let response = serde_json::to_string(&msg)?;
//! manager.broadcast_except(conn.id(), Message::text(response));
//!
//! Ok(())
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! let router = Router::new()
//! .default_handler(handler(chat_handler))
//! .on_connect(|manager, conn_id| {
//! println!("✅ {} joined (Total: {})", conn_id, manager.count());
//! })
//! .on_disconnect(|manager, conn_id| {
//! println!("❌ {} left (Total: {})", conn_id, manager.count());
//! });
//!
//! router.listen("127.0.0.1:8080").await?;
//! Ok(())
//! }
//! ```
//!
//! ### Web Application Example
//!
//! Create a hybrid server that serves static files and handles WebSocket connections:
//!
//! ```
//! use wsforge::prelude::*;
//! use std::sync::Arc;
//!
//! async fn ws_handler(
//! msg: Message,
//! State(manager): State<Arc<ConnectionManager>>,
//! ) -> Result<()> {
//! manager.broadcast(msg);
//! Ok(())
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! let router = Router::new()
//! .serve_static("public") // Serve HTML/CSS/JS from ./public
//! .default_handler(handler(ws_handler));
//!
//! println!("Server running on http://127.0.0.1:8080");
//! router.listen("127.0.0.1:8080").await?;
//! Ok(())
//! }
//! ```
//!
//! ## 📚 Core Concepts
//!
//! ### Handlers
//!
//! Handlers are async functions that process WebSocket messages. They can extract
//! data using type-safe extractors and return various response types:
//!
//! ```
//! use wsforge::prelude::*;
//! use std::sync::Arc;
//!
//! // Simple handler
//! async fn simple() -> Result<String> {
//! Ok("Hello!".to_string())
//! }
//!
//! // Handler with extractors
//! async fn with_extractors(
//! msg: Message,
//! conn: Connection,
//! State(manager): State<Arc<ConnectionManager>>,
//! ) -> Result<()> {
//! println!("Received from {}: {:?}", conn.id(), msg);
//! Ok(())
//! }
//! ```
//!
//! ### Extractors
//!
//! Extractors automatically parse and validate data from messages and context:
//!
//! | Extractor | Description |
//! |-----------|-------------|
//! | `Message` | Raw WebSocket message |
//! | `Json<T>` | Deserialize JSON automatically |
//! | `Connection` | Access to the active connection |
//! | `State<T>` | Shared application state |
//! | `ConnectInfo` | Connection metadata |
//! | `Data` | Raw binary data |
//!
//! ### Broadcasting
//!
//! Send messages to multiple connections efficiently:
//!
//! ```
//! use wsforge::prelude::*;
//! use std::sync::Arc;
//!
//! async fn broadcast_example(
//! msg: Message,
//! conn: Connection,
//! State(manager): State<Arc<ConnectionManager>>,
//! ) -> Result<()> {
//! // Broadcast to all
//! manager.broadcast(msg.clone());
//!
//! // Broadcast except sender
//! manager.broadcast_except(conn.id(), msg.clone());
//!
//! // Broadcast to specific connections
//! let targets = vec!["conn_1".to_string(), "conn_2".to_string()];
//! manager.broadcast_to(&targets, msg);
//!
//! Ok(())
//! }
//! ```
//!
//! ### State Management
//!
//! Share data across all connections:
//!
//! ```
//! use wsforge::prelude::*;
//! use std::sync::Arc;
//!
//! struct Database {
//! // Database connection pool
//! }
//!
//! struct Config {
//! max_connections: usize,
//! }
//!
//! async fn handler(
//! State(db): State<Arc<Database>>,
//! State(config): State<Arc<Config>>,
//! ) -> Result<String> {
//! Ok(format!("Max connections: {}", config.max_connections))
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! let router = Router::new()
//! .with_state(Arc::new(Database {}))
//! .with_state(Arc::new(Config { max_connections: 100 }))
//! .default_handler(handler(handler));
//!
//! router.listen("127.0.0.1:8080").await?;
//! Ok(())
//! }
//! ```
//!
//! ## 🎮 Complete Examples
//!
//! ### Real-Time Game Server
//!
//! ```
//! use wsforge::prelude::*;
//! use std::sync::Arc;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Deserialize, Serialize)]
//! struct GameMove {
//! player_id: u64,
//! x: f32,
//! y: f32,
//! }
//!
//! async fn game_handler(
//! Json(game_move): Json<GameMove>,
//! conn: Connection,
//! State(manager): State<Arc<ConnectionManager>>,
//! ) -> Result<()> {
//! // Broadcast move to all other players
//! let json = serde_json::to_string(&game_move)?;
//! manager.broadcast_except(conn.id(), Message::text(json));
//! Ok(())
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! let router = Router::new()
//! .default_handler(handler(game_handler))
//! .on_connect(|manager, conn_id| {
//! println!("🎮 Player {} joined", conn_id);
//! });
//!
//! router.listen("0.0.0.0:8080").await?;
//! Ok(())
//! }
//! ```
//!
//! ### Multi-Room Chat
//!
//! ```
//! use wsforge::prelude::*;
//! use std::sync::Arc;
//! use std::collections::HashMap;
//! use tokio::sync::RwLock;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Clone)]
//! struct RoomManager {
//! rooms: Arc<RwLock<HashMap<String, Vec<String>>>>,
//! }
//!
//! #[derive(Deserialize)]
//! struct RoomMessage {
//! room: String,
//! text: String,
//! }
//!
//! async fn room_handler(
//! Json(msg): Json<RoomMessage>,
//! conn: Connection,
//! State(room_mgr): State<Arc<RoomManager>>,
//! State(conn_mgr): State<Arc<ConnectionManager>>,
//! ) -> Result<()> {
//! // Get room members and broadcast
//! let rooms = room_mgr.rooms.read().await;
//! if let Some(members) = rooms.get(&msg.room) {
//! let json = serde_json::to_string(&msg)?;
//! conn_mgr.broadcast_to(members, Message::text(json));
//! }
//! Ok(())
//! }
//! ```
//!
//! ## 🔧 Advanced Features
//!
//! ### Custom Middleware
//!
//! ```
//! use wsforge::prelude::*;
//!
//! async fn auth_middleware(
//! msg: Message,
//! extensions: &Extensions,
//! ) -> Result<()> {
//! // Verify authentication token
//! let token = msg.as_text().ok_or(Error::custom("Invalid token"))?;
//!
//! // Store user info in extensions
//! extensions.insert("user_id", 123_u64);
//!
//! Ok(())
//! }
//! ```
//!
//! ### Graceful Shutdown
//!
//! ```
//! use wsforge::prelude::*;
//! use tokio::signal;
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! let router = Router::new();
//!
//! tokio::select! {
//! result = router.listen("127.0.0.1:8080") => {
//! if let Err(e) = result {
//! eprintln!("Server error: {}", e);
//! }
//! }
//! _ = signal::ctrl_c() => {
//! println!("Shutting down gracefully...");
//! }
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## 📊 Performance
//!
//! WsForge is designed for high performance:
//!
//! - **Concurrent Connections**: Handles thousands of connections efficiently
//! - **Lock-Free**: DashMap-based connection management eliminates lock contention
//! - **Zero-Copy**: Minimizes allocations and copies where possible
//! - **Async Native**: Built on tokio for maximum async performance
//! - **Benchmarked**: 47K+ requests/second for simple echo operations
//!
//! ## 🛡️ Security
//!
//! - **Path Traversal Protection**: Static file handler prevents directory escapes
//! - **Type Safety**: Rust's type system prevents common errors
//! - **Input Validation**: JSON parsing with serde provides automatic validation
//! - **Connection Limits**: Easy to implement rate limiting and connection caps
//!
//! ## 📖 Documentation
//!
//! - [GitHub Repository](https://github.com/aarambhdevhub/wsforge)
//! - [API Documentation](https://docs.rs/wsforge)
//! - [Examples](https://github.com/aarambhdevhub/wsforge/tree/main/examples)
//! - [Tutorial Series](https://youtube.com/@AarambhDevHub)
//!
//! ## 🤝 Contributing
//!
//! Contributions are welcome! Please see our [Contributing Guide](https://github.com/aarambhdevhub/wsforge/blob/main/CONTRIBUTING.md).
//!
//! ## 📝 License
//!
//! Licensed under the MIT License. See [LICENSE](https://github.com/aarambhdevhub/wsforge/blob/main/LICENSE) for details.
//!
//! ## 🙏 Acknowledgments
//!
//! - Built with [tokio-tungstenite](https://github.com/snapview/tokio-tungstenite)
//! - Inspired by [Axum](https://github.com/tokio-rs/axum)
//! - Thanks to the Rust community
//!
//! ## 🔗 Links
//!
//! - **Author**: [Aarambh Dev Hub](https://github.com/AarambhDevHub)
//! - **YouTube**: [@AarambhDevHub](https://youtube.com/@AarambhDevHub)
//! - **Support**: [Issues](https://github.com/aarambhdevhub/wsforge/issues)
// Enable documentation features for docs.rs
// Deny missing docs to ensure comprehensive documentation
// Enable additional documentation lint rules
// Re-export everything from wsforge-core
pub use *;
// Re-export macros if enabled
pub use *;
/// Prelude module for convenient imports.
///
/// Import this module to bring all commonly used types and traits into scope:
///
/// ```
/// use wsforge::prelude::*;
///
/// async fn handler(msg: Message) -> Result<String> {
/// Ok("response".to_string())
/// }
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// let router = Router::new()
/// .default_handler(handler(handler));
///
/// router.listen("127.0.0.1:8080").await?;
/// Ok(())
/// }
/// ```
///
/// ## Included Types
///
/// - **Core**: `Router`, `Message`, `Connection`, `ConnectionManager`
/// - **Handlers**: `handler()`, `Handler`, `IntoResponse`, `JsonResponse`
/// - **Extractors**: `Json`, `State`, `ConnectInfo`, `Data`, `Extension`
/// - **Errors**: `Error`, `Result`
/// - **State**: `AppState`, `Extensions`
/// - **Types**: `MessageType`, `ConnectionId`
/// - **Static Files**: `StaticFileHandler`