armature-framework 0.4.0

A modern, type-safe HTTP framework for Rust inspired by Angular and NestJS. Features dependency injection, decorators, middleware, authentication (JWT/OAuth2/SAML), validation, OpenAPI/Swagger, caching, job queues, and observability.
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#![allow(
    dead_code,
    clippy::default_constructed_unit_structs,
    clippy::needless_borrow,
    clippy::unnecessary_lazy_evaluations
)]
// WebSocket chat room example
//
// This example runs two servers side by side:
//   - The Armature HTTP application (REST endpoints) on `HTTP_PORT`.
//   - A *real* WebSocket server, built on `armature-websocket`
//     (tokio-tungstenite under the hood), on `WS_PORT`.
//
// `armature_core::websocket::{WebSocketManager, WebSocketRoom}` only provide
// in-process broadcast primitives with no client transport, so they cannot
// accept a real WebSocket connection on their own. `armature-websocket`'s
// `WebSocketServer` is a genuine tokio-tungstenite based server that accepts
// TCP connections and performs the WebSocket upgrade handshake itself.
//
// Both servers share the same `armature_websocket::RoomManager`, so a chat
// message sent by a real WebSocket client is broadcast to every other
// WebSocket client in the room, and a message posted over the HTTP API lands
// in the same room too.

use armature::prelude::*;
use armature_websocket::{
    Message as WsMessage, RoomManager, WebSocketError, WebSocketHandler, WebSocketServerBuilder,
};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{SystemTime, UNIX_EPOCH};

const HTTP_PORT: u16 = 3005;
const WS_PORT: u16 = 3006;

/// Shared with the standalone WebSocket server so the HTTP handlers can
/// broadcast into (and inspect) the exact same rooms real WebSocket clients
/// are connected to. Set once, at startup, by `start_ws_server`.
static CHAT_ROOMS: OnceLock<Arc<RoomManager>> = OnceLock::new();

fn chat_rooms() -> &'static Arc<RoomManager> {
    CHAT_ROOMS
        .get()
        .expect("WebSocket server must be started before serving HTTP requests")
}

// Duplicated intentionally in examples/server_sent_events.rs to keep this
// example self-contained/copy-pasteable.
fn now_millis() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ChatMessage {
    user: String,
    message: String,
    timestamp: u64,
}

impl ChatMessage {
    fn system(text: impl Into<String>) -> Self {
        Self {
            user: "system".to_string(),
            message: text.into(),
            timestamp: now_millis(),
        }
    }
}

/// Messages a connected WebSocket client can send as JSON text frames. The
/// first message from a fresh connection must be `join`; `chat` only works
/// once a room has been joined.
#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum ClientEvent {
    Join { room: String, user: String },
    Chat { message: String },
    Leave,
}

/// Per-connection state the WebSocket handler needs to remember between
/// messages: which room the connection joined, and under which display name.
#[derive(Debug, Clone)]
struct Session {
    room: String,
    user: String,
}

/// Real WebSocket connection handler: implements the join/chat/leave
/// protocol on top of `armature_websocket`'s `Connection` and `RoomManager`
/// primitives.
#[derive(Clone, Default)]
struct ChatWsHandler {
    sessions: Arc<Mutex<HashMap<String, Session>>>,
}

impl ChatWsHandler {
    fn reply_error(&self, rooms: &RoomManager, connection_id: &str, text: &str) {
        if let Some(conn) = rooms.get_connection(connection_id) {
            let _ = conn.send_json(&serde_json::json!({ "type": "error", "message": text }));
        }
    }
}

#[async_trait]
impl WebSocketHandler for ChatWsHandler {
    async fn on_connect(&self, connection_id: &str) {
        println!("[ws] client {connection_id} connected");
    }

    async fn on_message(&self, connection_id: &str, message: WsMessage) {
        let Some(text) = message.as_text() else {
            // Binary/other frames aren't part of this example's protocol.
            return;
        };

        let rooms = chat_rooms();

        let event: ClientEvent = match serde_json::from_str(text) {
            Ok(event) => event,
            Err(e) => {
                self.reply_error(rooms, connection_id, &format!("invalid message: {e}"));
                return;
            }
        };

        match event {
            ClientEvent::Join { room, user } => {
                let new_session = Session {
                    room: room.clone(),
                    user: user.clone(),
                };
                let previous = self
                    .sessions
                    .lock()
                    .unwrap()
                    .insert(connection_id.to_string(), new_session);

                // Only one room per connection in this example: leave the
                // previous one (if any) before joining the new one.
                if let Some(prev) = &previous {
                    let _ = rooms.leave_room(connection_id, &prev.room);
                }

                if let Err(e) = rooms.join_room(connection_id, &room) {
                    self.reply_error(rooms, connection_id, &format!("failed to join: {e}"));
                    self.sessions.lock().unwrap().remove(connection_id);
                    return;
                }

                println!("[ws] {user} ({connection_id}) joined room '{room}'");

                let announcement = ChatMessage::system(format!("{user} joined the room"));
                if let Ok(payload) = WsMessage::json(&announcement) {
                    let _ = rooms.broadcast_to_room(&room, payload);
                }
            }
            ClientEvent::Chat { message } => {
                let session = self.sessions.lock().unwrap().get(connection_id).cloned();
                let Some(session) = session else {
                    self.reply_error(rooms, connection_id, "join a room before sending messages");
                    return;
                };

                let chat_message = ChatMessage {
                    user: session.user,
                    message,
                    timestamp: now_millis(),
                };

                match WsMessage::json(&chat_message) {
                    Ok(payload) => {
                        let _ = rooms.broadcast_to_room(&session.room, payload);
                    }
                    Err(e) => {
                        self.reply_error(
                            rooms,
                            connection_id,
                            &format!("failed to encode message: {e}"),
                        );
                    }
                }
            }
            ClientEvent::Leave => {
                if let Some(session) = self.sessions.lock().unwrap().remove(connection_id) {
                    let _ = rooms.leave_room(connection_id, &session.room);
                    println!(
                        "[ws] {} ({connection_id}) left room '{}'",
                        session.user, session.room
                    );
                    let announcement =
                        ChatMessage::system(format!("{} left the room", session.user));
                    if let Ok(payload) = WsMessage::json(&announcement) {
                        let _ = rooms.broadcast_to_room(&session.room, payload);
                    }
                }
            }
        }
    }

    async fn on_disconnect(&self, connection_id: &str) {
        // `RoomManager::unregister_connection` (called by the server right
        // after this hook returns) already removes the connection from
        // every room it was a member of; we only need to forget our own
        // bookkeeping and let the other members know.
        if let Some(session) = self.sessions.lock().unwrap().remove(connection_id) {
            println!(
                "[ws] {} ({connection_id}) disconnected from room '{}'",
                session.user, session.room
            );
            let rooms = chat_rooms();
            let announcement = ChatMessage::system(format!("{} disconnected", session.user));
            if let Ok(payload) = WsMessage::json(&announcement) {
                let _ = rooms.broadcast_to_room_except(&session.room, payload, connection_id);
            }
        }
    }

    async fn on_error(&self, connection_id: &str, error: &WebSocketError) {
        eprintln!("[ws] connection {connection_id} error: {error}");
    }
}

/// Starts the real WebSocket server in the background and publishes the
/// `RoomManager` it owns to `CHAT_ROOMS`, so the HTTP layer can share it.
fn start_ws_server() {
    let bind_addr = SocketAddr::from(([0, 0, 0, 0], WS_PORT));
    let ws_server = WebSocketServerBuilder::new()
        .bind_addr(bind_addr)
        .build(ChatWsHandler::default());

    if CHAT_ROOMS
        .set(Arc::clone(ws_server.room_manager()))
        .is_err()
    {
        panic!("start_ws_server must only be called once");
    }

    tokio::spawn(async move {
        if let Err(e) = ws_server.run().await {
            eprintln!("WebSocket server error: {e}");
        }
    });
}

/// Chat service used by the HTTP controller. It talks to the exact same
/// `RoomManager` the real WebSocket server uses, so an HTTP-posted message
/// reaches connected WebSocket clients and a room's stats reflect real,
/// currently-connected clients.
#[injectable]
#[derive(Default, Clone)]
struct ChatService;

impl ChatService {
    async fn broadcast_to_room(
        &self,
        room_name: &str,
        message: ChatMessage,
    ) -> Result<usize, Error> {
        let payload = WsMessage::json(&message).map_err(|e| Error::Serialization(e.to_string()))?;
        match chat_rooms().broadcast_to_room(room_name, payload) {
            Ok(sent) => Ok(sent),
            // No WebSocket client has joined this room yet -- not an error,
            // just zero recipients.
            Err(WebSocketError::RoomNotFound(_)) => Ok(0),
            Err(e) => Err(Error::Internal(e.to_string())),
        }
    }

    fn room_stats(&self, room_name: &str) -> usize {
        chat_rooms()
            .get_room(room_name)
            .map(|room| room.len())
            .unwrap_or(0)
    }
}

/// Chat controller
#[controller("/chat")]
#[derive(Default, Clone)]
struct ChatController;

#[routes]
impl ChatController {
    #[post("/:room/message")]
    async fn send_message(req: HttpRequest) -> Result<HttpResponse, Error> {
        let room_name = req
            .param("room")
            .ok_or_else(|| Error::Validation("Missing room parameter".to_string()))?;

        let mut message: ChatMessage = req.json()?;
        if message.timestamp == 0 {
            message.timestamp = now_millis();
        }

        let service = ChatService::default();
        let delivered = service.broadcast_to_room(room_name, message).await?;

        HttpResponse::json(&serde_json::json!({
            "status": "sent",
            "delivered_to": delivered
        }))
    }

    #[get("/:room/stats")]
    async fn get_stats(req: HttpRequest) -> Result<HttpResponse, Error> {
        let room_name = req
            .param("room")
            .ok_or_else(|| Error::Validation("Missing room parameter".to_string()))?;

        let service = ChatService::default();
        let count = service.room_stats(room_name);

        HttpResponse::json(&serde_json::json!({
            "room": room_name,
            "connections": count
        }))
    }
}

#[module(
    providers: [ChatService],
    controllers: [ChatController]
)]
#[derive(Default, Clone)]
struct AppModule;

#[tokio::main]
async fn main() {
    println!("🔌 Armature WebSocket Chat Example");
    println!("===================================\n");

    start_ws_server();

    println!("Available endpoints:");
    println!("  POST /chat/:room/message - Send a message to a room (also reaches WS clients)");
    println!("  GET  /chat/:room/stats   - Get live room connection count");
    println!(
        "  WS   ws://localhost:{WS_PORT}       - Real-time chat (armature-websocket, tokio-tungstenite)"
    );
    println!("\nWebSocket protocol (JSON text frames):");
    println!("  Join a room:  {{\"type\":\"join\",\"room\":\"general\",\"user\":\"Alice\"}}");
    println!("  Send chat:    {{\"type\":\"chat\",\"message\":\"Hello!\"}}");
    println!("  Leave a room: {{\"type\":\"leave\"}}");
    println!("\nExample usage:");
    println!("  curl -X POST http://localhost:{HTTP_PORT}/chat/general/message \\");
    println!("    -H 'Content-Type: application/json' \\");
    println!("    -d '{{\"user\":\"Alice\",\"message\":\"Hello!\",\"timestamp\":1234567890}}'");
    println!();

    let app = Application::create::<AppModule>().await;

    if let Err(e) = app.listen(HTTP_PORT).await {
        eprintln!("Server error: {}", e);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use armature_websocket::WebSocketClient;
    use std::time::Duration;

    /// Reads messages from `client` until one satisfies `predicate`,
    /// returning it as parsed JSON. System announcements (room joins) and
    /// out-of-order broadcasts are skipped rather than causing a mismatch.
    ///
    /// Panics (via `expect`) if `timeout` elapses or the connection closes
    /// before a matching message arrives -- both are genuine test failures.
    async fn recv_matching(
        client: &mut WebSocketClient,
        timeout: Duration,
        mut predicate: impl FnMut(&serde_json::Value) -> bool,
    ) -> serde_json::Value {
        let deadline = tokio::time::Instant::now() + timeout;
        loop {
            let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
            let message = tokio::time::timeout(remaining, client.recv())
                .await
                .expect("timed out waiting for a matching WebSocket message")
                .expect("connection closed before a matching message arrived");

            let Some(text) = message.as_text() else {
                continue;
            };
            let Ok(value) = serde_json::from_str::<serde_json::Value>(text) else {
                continue;
            };
            if predicate(&value) {
                return value;
            }
        }
    }

    /// Connects to `url`, retrying for a bit: `start_ws_server` spawns the
    /// listener's bind + accept loop onto a background task, so it may not
    /// be listening yet the instant `start_ws_server()` returns.
    async fn connect_with_retry(url: &str, timeout: Duration) -> WebSocketClient {
        let deadline = tokio::time::Instant::now() + timeout;
        loop {
            match WebSocketClient::connect(url).await {
                Ok(client) => return client,
                Err(e) => {
                    if tokio::time::Instant::now() >= deadline {
                        panic!("failed to connect to {url} within {timeout:?}: {e}");
                    }
                    tokio::time::sleep(Duration::from_millis(20)).await;
                }
            }
        }
    }

    /// End-to-end coverage for the doc comment's central claim: a chat
    /// message sent by one real WebSocket client is broadcast to every other
    /// WebSocket client in the same room (via `start_ws_server`'s listener
    /// and `ChatWsHandler`'s join/chat protocol), and a message posted
    /// through the HTTP-facing `ChatService` (the same service
    /// `ChatController::send_message` delegates to) reaches those same
    /// WebSocket clients through the shared `CHAT_ROOMS` `RoomManager`.
    ///
    /// Both scenarios live in one test (rather than two) because
    /// `start_ws_server` publishes to the process-global `CHAT_ROOMS`
    /// `OnceLock` and panics if called more than once; splitting this across
    /// multiple `#[tokio::test]` functions (which run concurrently in the
    /// same process) would race on that global.
    #[tokio::test]
    async fn ws_broadcast_and_http_bridge_share_the_same_room() {
        start_ws_server();

        let url = format!("ws://127.0.0.1:{WS_PORT}");
        let timeout = Duration::from_secs(5);
        let room = "integration-test-room";

        let mut alice = connect_with_retry(&url, timeout).await;
        let mut bob = connect_with_retry(&url, timeout).await;

        alice
            .send_json(&serde_json::json!({"type": "join", "room": room, "user": "alice"}))
            .expect("alice failed to send join");
        // Alice is the only member so far: this is her own join announcement.
        recv_matching(&mut alice, timeout, |v| v["user"] == "system").await;

        bob.send_json(&serde_json::json!({"type": "join", "room": room, "user": "bob"}))
            .expect("bob failed to send join");
        // `ChatWsHandler` calls `join_room` before broadcasting the "bob
        // joined" announcement, so bob observing this message proves the
        // server has already registered him as a room member -- which in
        // turn guarantees the chat message sent right after this will reach
        // him too.
        recv_matching(&mut bob, timeout, |v| v["user"] == "system").await;

        // --- WS-to-WS broadcast ---
        alice
            .send_json(&serde_json::json!({"type": "chat", "message": "hello from alice"}))
            .expect("alice failed to send chat message");

        let received = recv_matching(&mut bob, timeout, |v| {
            v["user"] == "alice" && v["message"] == "hello from alice"
        })
        .await;
        assert_eq!(received["user"], "alice");
        assert_eq!(received["message"], "hello from alice");

        // --- HTTP-to-WS bridge ---
        // Exercises the exact same code path `ChatController::send_message`
        // delegates to, without needing a full HTTP client.
        let service = ChatService::default();
        let delivered = service
            .broadcast_to_room(
                room,
                ChatMessage {
                    user: "http-poster".to_string(),
                    message: "hello via http".to_string(),
                    timestamp: now_millis(),
                },
            )
            .await
            .expect("broadcast_to_room failed");
        assert_eq!(
            delivered, 2,
            "message posted via the HTTP-facing service should reach both connected WS clients"
        );

        let received = recv_matching(&mut bob, timeout, |v| {
            v["user"] == "http-poster" && v["message"] == "hello via http"
        })
        .await;
        assert_eq!(received["user"], "http-poster");
        assert_eq!(received["message"], "hello via http");
    }
}