capnweb-server 0.1.0

Production-ready server for Cap'n Web RPC protocol with capability management
Documentation
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
//! Example Cap'n Web Server
//!
//! A demonstration server showing various Cap'n Web features and patterns.
//! This server implements several example services that can be used for
//! learning and testing the Cap'n Web protocol.

use anyhow::Result;
use async_trait::async_trait;
use capnweb_core::{CapId, RpcError};
use capnweb_server::{RpcTarget, Server, ServerConfig};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tracing::{info, warn};

/// Counter service - demonstrates stateful services
#[derive(Debug)]
struct CounterService {
    count: Arc<Mutex<i64>>,
}

impl CounterService {
    fn new() -> Self {
        Self {
            count: Arc::new(Mutex::new(0)),
        }
    }
}

#[async_trait]
impl RpcTarget for CounterService {
    async fn call(&self, method: &str, _args: Vec<Value>) -> Result<Value, RpcError> {
        match method {
            "increment" => {
                let mut count = self
                    .count
                    .lock()
                    .map_err(|e| RpcError::internal(format!("Counter lock poisoned: {}", e)))?;
                *count += 1;
                Ok(json!({ "count": *count }))
            }
            "decrement" => {
                let mut count = self
                    .count
                    .lock()
                    .map_err(|e| RpcError::internal(format!("Counter lock poisoned: {}", e)))?;
                *count -= 1;
                Ok(json!({ "count": *count }))
            }
            "get" => {
                let count = self
                    .count
                    .lock()
                    .map_err(|e| RpcError::internal(format!("Counter lock poisoned: {}", e)))?;
                Ok(json!({ "count": *count }))
            }
            "reset" => {
                let mut count = self
                    .count
                    .lock()
                    .map_err(|e| RpcError::internal(format!("Counter lock poisoned: {}", e)))?;
                *count = 0;
                Ok(json!({ "count": 0 }))
            }
            _ => Err(RpcError::not_found(format!("Unknown method: {}", method))),
        }
    }
}

/// Key-Value store service - demonstrates CRUD operations
#[derive(Debug)]
struct KeyValueStore {
    store: Arc<Mutex<HashMap<String, Value>>>,
}

impl KeyValueStore {
    fn new() -> Self {
        Self {
            store: Arc::new(Mutex::new(HashMap::new())),
        }
    }
}

#[async_trait]
impl RpcTarget for KeyValueStore {
    async fn call(&self, method: &str, args: Vec<Value>) -> Result<Value, RpcError> {
        match method {
            "get" => {
                if args.is_empty() {
                    return Err(RpcError::bad_request("get requires a key"));
                }
                let key = args[0]
                    .as_str()
                    .ok_or_else(|| RpcError::bad_request("Key must be a string"))?;

                let store = self
                    .store
                    .lock()
                    .map_err(|e| RpcError::internal(format!("Store lock poisoned: {}", e)))?;
                match store.get(key) {
                    Some(value) => Ok(json!({ "value": value })),
                    None => Ok(json!({ "value": null })),
                }
            }
            "set" => {
                if args.len() < 2 {
                    return Err(RpcError::bad_request("set requires key and value"));
                }
                let key = args[0]
                    .as_str()
                    .ok_or_else(|| RpcError::bad_request("Key must be a string"))?;

                let mut store = self
                    .store
                    .lock()
                    .map_err(|e| RpcError::internal(format!("Store lock poisoned: {}", e)))?;
                store.insert(key.to_string(), args[1].clone());
                Ok(json!({ "success": true }))
            }
            "delete" => {
                if args.is_empty() {
                    return Err(RpcError::bad_request("delete requires a key"));
                }
                let key = args[0]
                    .as_str()
                    .ok_or_else(|| RpcError::bad_request("Key must be a string"))?;

                let mut store = self
                    .store
                    .lock()
                    .map_err(|e| RpcError::internal(format!("Store lock poisoned: {}", e)))?;
                let existed = store.remove(key).is_some();
                Ok(json!({ "deleted": existed }))
            }
            "list" => {
                let store = self
                    .store
                    .lock()
                    .map_err(|e| RpcError::internal(format!("Store lock poisoned: {}", e)))?;
                let keys: Vec<String> = store.keys().cloned().collect();
                Ok(json!({ "keys": keys }))
            }
            "clear" => {
                let mut store = self
                    .store
                    .lock()
                    .map_err(|e| RpcError::internal(format!("Store lock poisoned: {}", e)))?;
                let count = store.len();
                store.clear();
                Ok(json!({ "cleared": count }))
            }
            _ => Err(RpcError::not_found(format!("Unknown method: {}", method))),
        }
    }
}

/// Time service - demonstrates async operations
#[derive(Debug)]
struct TimeService;

#[async_trait]
impl RpcTarget for TimeService {
    async fn call(&self, method: &str, args: Vec<Value>) -> Result<Value, RpcError> {
        match method {
            "now" => Ok(json!({
                "timestamp": chrono::Utc::now().to_rfc3339(),
                "unix": chrono::Utc::now().timestamp(),
            })),
            "delay" => {
                // Simulate async delay
                let delay_ms = args.first().and_then(|v| v.as_u64()).unwrap_or(1000);

                tokio::time::sleep(tokio::time::Duration::from_millis(delay_ms)).await;

                Ok(json!({
                    "delayed": delay_ms,
                    "timestamp": chrono::Utc::now().to_rfc3339(),
                }))
            }
            "format" => {
                let timestamp = args
                    .first()
                    .and_then(|v| v.as_i64())
                    .ok_or_else(|| RpcError::bad_request("format requires a unix timestamp"))?;

                use chrono::DateTime;
                let dt = DateTime::from_timestamp(timestamp, 0)
                    .ok_or_else(|| RpcError::bad_request("Invalid timestamp"))?;

                Ok(json!({
                    "formatted": dt.format("%Y-%m-%d %H:%M:%S UTC").to_string(),
                    "iso": dt.to_rfc3339(),
                }))
            }
            _ => Err(RpcError::not_found(format!("Unknown method: {}", method))),
        }
    }
}

/// Math service - demonstrates computational operations
#[derive(Debug)]
struct MathService;

#[async_trait]
impl RpcTarget for MathService {
    async fn call(&self, method: &str, args: Vec<Value>) -> Result<Value, RpcError> {
        match method {
            "fibonacci" => {
                let n = args
                    .first()
                    .and_then(|v| v.as_u64())
                    .ok_or_else(|| RpcError::bad_request("fibonacci requires a number"))?;

                if n > 93 {
                    return Err(RpcError::bad_request("fibonacci input too large (max 93)"));
                }

                let result = fibonacci(n);
                Ok(json!({ "result": result, "n": n }))
            }
            "factorial" => {
                let n = args
                    .first()
                    .and_then(|v| v.as_u64())
                    .ok_or_else(|| RpcError::bad_request("factorial requires a number"))?;

                if n > 20 {
                    return Err(RpcError::bad_request("factorial input too large (max 20)"));
                }

                let result = factorial(n);
                Ok(json!({ "result": result, "n": n }))
            }
            "isPrime" => {
                let n = args
                    .first()
                    .and_then(|v| v.as_u64())
                    .ok_or_else(|| RpcError::bad_request("isPrime requires a number"))?;

                let result = is_prime(n);
                Ok(json!({ "isPrime": result, "n": n }))
            }
            "sqrt" => {
                let n = args
                    .first()
                    .and_then(|v| v.as_f64())
                    .ok_or_else(|| RpcError::bad_request("sqrt requires a number"))?;

                if n < 0.0 {
                    return Err(RpcError::bad_request("sqrt requires non-negative number"));
                }

                Ok(json!({ "result": n.sqrt() }))
            }
            _ => Err(RpcError::not_found(format!("Unknown method: {}", method))),
        }
    }
}

// Helper functions for math operations
fn fibonacci(n: u64) -> u64 {
    match n {
        0 => 0,
        1 => 1,
        _ => {
            let mut a = 0u64;
            let mut b = 1u64;
            for _ in 2..=n {
                let temp = a + b;
                a = b;
                b = temp;
            }
            b
        }
    }
}

fn factorial(n: u64) -> u64 {
    (1..=n).product()
}

fn is_prime(n: u64) -> bool {
    if n < 2 {
        return false;
    }
    for i in 2..=((n as f64).sqrt() as u64) {
        if n % i == 0 {
            return false;
        }
    }
    true
}

/// Main/Bootstrap service
#[derive(Debug)]
struct MainService;

#[async_trait]
impl RpcTarget for MainService {
    async fn call(&self, method: &str, args: Vec<Value>) -> Result<Value, RpcError> {
        match method {
            "getCapability" => {
                // Extract and validate capability ID from args
                let id_value = args.first().ok_or_else(|| {
                    RpcError::bad_request("getCapability requires a capability ID argument")
                })?;

                // Ensure it's a JSON number
                let id_number = id_value
                    .as_number()
                    .ok_or_else(|| RpcError::bad_request("Capability ID must be a number"))?;

                // Validate it's an integer (no fractional part)
                if !id_number.is_i64() && !id_number.is_u64() {
                    return Err(RpcError::bad_request("Capability ID must be an integer"));
                }

                // Try to get as i64 first to check for negative numbers
                let cap_id = if let Some(i64_val) = id_number.as_i64() {
                    if i64_val < 0 {
                        return Err(RpcError::bad_request("Capability ID must be non-negative"));
                    }
                    // Safe to convert to u64 since we checked it's non-negative
                    i64_val as u64
                } else if let Some(u64_val) = id_number.as_u64() {
                    // Direct u64 value (already non-negative by type)
                    u64_val
                } else {
                    return Err(RpcError::bad_request(
                        "Capability ID value is out of valid range",
                    ));
                };

                // Check if capability exists (for this example, we support 0-4)
                match cap_id {
                    0..=4 => {
                        // Return capability reference in Cap'n Web wire format
                        Ok(json!({
                            "$capnweb": {
                                "import_id": cap_id
                            }
                        }))
                    }
                    _ => Err(RpcError::not_found(format!(
                        "Capability {} not found",
                        cap_id
                    ))),
                }
            }
            "listServices" => Ok(json!({
                "services": [
                    { "id": 1, "name": "counter", "description": "Stateful counter service" },
                    { "id": 2, "name": "keyvalue", "description": "Key-value store" },
                    { "id": 3, "name": "time", "description": "Time and delay operations" },
                    { "id": 4, "name": "math", "description": "Mathematical operations" },
                ]
            })),
            "health" => Ok(json!({
                "status": "healthy",
                "timestamp": chrono::Utc::now().to_rfc3339(),
            })),
            _ => Err(RpcError::not_found(format!("Unknown method: {}", method))),
        }
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize logging
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .with_target(false)
        .init();

    info!("Starting Cap'n Web Example Server");

    // Configure server
    let config = ServerConfig {
        port: 8080,
        host: "127.0.0.1".to_string(),
        max_batch_size: 100,
    };

    // Create server
    let server = Server::new(config);

    // Register services
    server.register_capability(CapId::new(0), Arc::new(MainService));
    server.register_capability(CapId::new(1), Arc::new(CounterService::new()));
    server.register_capability(CapId::new(2), Arc::new(KeyValueStore::new()));
    server.register_capability(CapId::new(3), Arc::new(TimeService));
    server.register_capability(CapId::new(4), Arc::new(MathService));

    info!("Server configured with example services:");
    info!("  - CapId(0): Main Service (bootstrap)");
    info!("  - CapId(1): Counter Service");
    info!("  - CapId(2): Key-Value Store");
    info!("  - CapId(3): Time Service");
    info!("  - CapId(4): Math Service");

    // Start server
    info!("Starting server on http://127.0.0.1:8080");
    info!("Endpoints:");
    info!("  - HTTP Batch: http://127.0.0.1:8080/rpc/batch");
    info!("  - WebSocket:  ws://127.0.0.1:8080/rpc/ws");

    if let Err(e) = server.run().await {
        warn!("Server error: {}", e);
        std::process::exit(1);
    }

    Ok(())
}