ixa/
web_api.rs

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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use crate::context::{run_with_plugin, Context};
use crate::define_data_plugin;
use crate::error::IxaError;
use crate::external_api::{
    breakpoint, global_properties, halt, next, people, population, r#continue, run_ext_api, time,
    EmptyArgs,
};
use crate::{HashMap, HashMapExt};
use axum::extract::{Json, Path, State};
use axum::response::Redirect;
use axum::routing::get;
use axum::{http::StatusCode, routing::post, Router};
use rand::RngCore;
use serde_json::json;
use std::thread;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
use tower_http::services::{ServeDir, ServeFile};

pub type WebApiHandler =
    dyn Fn(&mut Context, serde_json::Value) -> Result<serde_json::Value, IxaError>;

fn register_api_handler<
    T: crate::external_api::ExtApi<Args = A>,
    A: serde::de::DeserializeOwned,
>(
    dc: &mut ApiData,
    name: &str,
) {
    dc.handlers.insert(
        name.to_string(),
        Box::new(
            |context, args_json| -> Result<serde_json::Value, IxaError> {
                let args: A = serde_json::from_value(args_json)?;
                let retval: T::Retval = run_ext_api::<T>(context, &args)?;
                Ok(serde_json::to_value(retval)?)
            },
        ),
    );
}

struct ApiData {
    receiver: mpsc::UnboundedReceiver<ApiRequest>,
    handlers: HashMap<String, Box<WebApiHandler>>,
}

pub(crate) fn enter_web_debugger(context: &mut Context) {
    run_with_plugin::<ApiPlugin>(context, |context, data_container| {
        handle_web_api(context, data_container.as_mut().unwrap());
    });
}

define_data_plugin!(ApiPlugin, Option<ApiData>, None);

// Input to the API handler.
struct ApiRequest {
    cmd: String,
    arguments: serde_json::Value,
    // This channel is used to send the response.
    rx: oneshot::Sender<ApiResponse>,
}

// Output of the API handler.
struct ApiResponse {
    code: StatusCode,
    response: serde_json::Value,
}

#[derive(Clone)]
struct ApiEndpointServer {
    sender: mpsc::UnboundedSender<ApiRequest>,
}

async fn process_cmd(
    State(state): State<ApiEndpointServer>,
    Path(path): Path<String>,
    Json(payload): Json<serde_json::Value>,
) -> (StatusCode, Json<serde_json::Value>) {
    let (tx, rx) = oneshot::channel::<ApiResponse>();
    let _ = state.sender.send(ApiRequest {
        cmd: path,
        arguments: payload,
        rx: tx,
    });

    match rx.await {
        Ok(response) => (response.code, Json(response.response)),
        _ => (StatusCode::INTERNAL_SERVER_ERROR, Json(json!({}))),
    }
}

#[tokio::main]
async fn serve(
    sender: mpsc::UnboundedSender<ApiRequest>,
    port: u16,
    prefix: &str,
    ready: oneshot::Sender<Result<String, IxaError>>,
) {
    let state = ApiEndpointServer { sender };

    // run our app with Axum, listening on `port`
    let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{port}")).await;
    if listener.is_err() {
        ready
            .send(Err(IxaError::IxaError(format!("Could not bind to {port}"))))
            .unwrap();
        return;
    }

    // build our application with a route
    let path = format!("{}/{}", env!("CARGO_MANIFEST_DIR"), "static/");
    let static_assets_path = std::path::Path::new(&path);
    let home_path = format!("/{prefix}/static/index.html");
    let app = Router::new()
        .route(&format!("/{prefix}/cmd/{{command}}"), post(process_cmd))
        .route(
            &format!("/{prefix}/"),
            get(|| async move { Redirect::temporary(&home_path) }),
        )
        .nest_service(
            &format!("/{prefix}/static/"),
            ServeDir::new(static_assets_path),
        )
        .nest_service(
            "/favicon.ico",
            ServeFile::new_with_mime(
                static_assets_path.join(std::path::Path::new("favicon.ico")),
                &mime::IMAGE_PNG,
            ),
        )
        .with_state(state);

    // Notify the caller that we are ready.
    ready
        .send(Ok(format!("http://127.0.0.1:{port}/{prefix}/")))
        .unwrap();
    axum::serve(listener.unwrap(), app).await.unwrap();
}

/// Starts the Web API, pausing execution until instructed
/// to continue.
fn handle_web_api(context: &mut Context, api: &mut ApiData) {
    while let Some(req) = api.receiver.blocking_recv() {
        if req.cmd == "continue" {
            let _ = req.rx.send(ApiResponse {
                code: StatusCode::OK,
                response: json!({}),
            });
            break;
        }

        let handler = api.handlers.get(&req.cmd);
        if handler.is_none() {
            let _ = req.rx.send(ApiResponse {
                code: StatusCode::NOT_FOUND,
                response: json!({
                    "error" : format!("No command {}", req.cmd)
                }),
            });
            continue;
        }

        let handler = handler.unwrap();
        match handler(context, req.arguments.clone()) {
            Err(err) => {
                let _ = req.rx.send(ApiResponse {
                    code: StatusCode::BAD_REQUEST,
                    response: json!({
                        "error" : err.to_string()
                    }),
                });
                continue;
            }
            Ok(response) => {
                let _ = req.rx.send(ApiResponse {
                    code: StatusCode::OK,
                    response,
                });
            }
        };

        // Special case the functions which require exiting
        // the loop.
        if req.cmd == "continue" {
            return;
        }
    }
}

pub trait ContextWebApiExt {
    /// Set up the Web API and start the Web server.
    ///
    /// # Errors
    /// `IxaError` on failure to bind to `port`
    fn setup_web_api(&mut self, port: u16) -> Result<String, IxaError>;

    /// Schedule the simulation to pause at time t and listen for
    /// requests from the Web API.
    fn schedule_web_api(&mut self, t: f64);

    /// Add an API point.
    /// # Errors
    /// `IxaError` when the Web API has not been set up yet.
    fn add_web_api_handler(
        &mut self,
        name: &str,
        handler: impl Fn(&mut Context, serde_json::Value) -> Result<serde_json::Value, IxaError>
            + 'static,
    ) -> Result<(), IxaError>;
}

impl ContextWebApiExt for Context {
    fn setup_web_api(&mut self, port: u16) -> Result<String, IxaError> {
        // TODO(cym4@cdc.gov): Check on the limits here.
        let (api_to_ctx_send, api_to_ctx_recv) = mpsc::unbounded_channel::<ApiRequest>();

        let data_container = self.get_data_container_mut(ApiPlugin);
        if data_container.is_some() {
            return Err(IxaError::IxaError(String::from(
                "HTTP API already initialized",
            )));
        }

        // Start the API server
        let mut random: [u8; 16] = [0; 16];
        rand::rngs::OsRng.fill_bytes(&mut random);
        let secret = uuid::Builder::from_random_bytes(random)
            .into_uuid()
            .to_string();

        let (ready_tx, ready_rx) = oneshot::channel::<Result<String, IxaError>>();
        thread::spawn(move || serve(api_to_ctx_send, port, &secret, ready_tx));
        let url = ready_rx.blocking_recv().unwrap()?;

        let mut api_data = ApiData {
            receiver: api_to_ctx_recv,
            handlers: HashMap::new(),
        };

        register_api_handler::<breakpoint::Api, breakpoint::Args>(&mut api_data, "breakpoint");
        register_api_handler::<r#continue::Api, EmptyArgs>(&mut api_data, "continue");
        register_api_handler::<global_properties::Api, global_properties::Args>(
            &mut api_data,
            "global",
        );
        register_api_handler::<halt::Api, EmptyArgs>(&mut api_data, "halt");
        register_api_handler::<next::Api, EmptyArgs>(&mut api_data, "next");
        register_api_handler::<people::Api, people::Args>(&mut api_data, "people");
        register_api_handler::<population::Api, EmptyArgs>(&mut api_data, "population");
        register_api_handler::<time::Api, EmptyArgs>(&mut api_data, "time");
        // Record the data container.
        *data_container = Some(api_data);

        Ok(url)
    }

    fn schedule_web_api(&mut self, t: f64) {
        self.add_plan(t, |context| {
            run_with_plugin::<ApiPlugin>(context, |context, data_container| {
                handle_web_api(context, data_container.as_mut().unwrap());
            });
        });
    }

    /// Add an API point.
    fn add_web_api_handler(
        &mut self,
        name: &str,
        handler: impl Fn(&mut Context, serde_json::Value) -> Result<serde_json::Value, IxaError>
            + 'static,
    ) -> Result<(), IxaError> {
        let data_container = self.get_data_container_mut(ApiPlugin);

        match data_container {
            Some(dc) => {
                dc.handlers.insert(name.to_string(), Box::new(handler));
                Ok(())
            }
            None => Err(IxaError::IxaError(String::from("Web API not yet set up"))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::ContextWebApiExt;
    use crate::people::define_person_property;
    use crate::{define_global_property, ContextGlobalPropertiesExt};
    use crate::{Context, ContextPeopleExt};
    use reqwest::StatusCode;
    use serde::Serialize;
    use serde_json::json;
    use std::thread;

    define_global_property!(WebApiTestGlobal, String);
    define_person_property!(Age, u8);
    fn setup() -> (String, Context) {
        let mut context = Context::new();
        let url = context.setup_web_api(33339).unwrap();
        context.schedule_web_api(0.0);
        context
            .set_global_property_value(WebApiTestGlobal, "foobar".to_string())
            .unwrap();
        context.add_person((Age, 1)).unwrap();
        context.add_person((Age, 2)).unwrap();
        context
            .add_web_api_handler("external", |_context, args| Ok(args))
            .unwrap();
        (url, context)
    }

    // Continue the simulation. Note that we don't wait for a response
    // because there is a race condition between sending the final
    // response and program termination.
    fn send_continue(url: &str) {
        let client = reqwest::blocking::Client::new();
        client
            .post(format!("{url}cmd/continue"))
            .json(&{})
            .send()
            .unwrap();
    }

    // Send a request and check the response.
    fn send_request<T: Serialize + ?Sized>(url: &str, cmd: &str, req: &T) -> serde_json::Value {
        let client = reqwest::blocking::Client::new();
        let response = client
            .post(format!("{url}cmd/{cmd}"))
            .json(req)
            .send()
            .unwrap();
        let status = response.status();
        let response = response.json().unwrap();
        println!("{response:?}");
        assert_eq!(status, StatusCode::OK);
        response
    }

    // Send a request and check the response.
    fn send_request_text(url: &str, cmd: &str, req: String) -> reqwest::blocking::Response {
        let client = reqwest::blocking::Client::new();
        client
            .post(format!("{url}cmd/{cmd}"))
            .header("Content-Type", "application/json")
            .body(req)
            .send()
            .unwrap()
    }

    // We do all of the tests in one test block to avoid having to
    // start a lot of servers with different ports and having
    // to manage that. This may not be ideal, but we're doing it for now.
    // TODO(cym4@cdc.gov): Consider using some kind of static
    // object to isolate the test cases.
    #[allow(clippy::too_many_lines)]
    #[test]
    fn web_api_test() {
        #[derive(Serialize)]
        struct PopulationResponse {
            population: usize,
        }

        // TODO(cym4@cdc.gov): If this thread fails
        // then the test will stall instead of
        // erroring out, but there's nothing that
        // should fail here.
        let (tx, rx) = std::sync::mpsc::channel::<String>();
        let ctx_thread = thread::spawn(move || {
            let (url, mut context) = setup();
            let _ = tx.send(url);
            context.execute();
        });

        let url = rx.recv().unwrap();
        // Test the population API point.
        let res = send_request(&url, "population", &json!({}));
        assert_eq!(json!(&PopulationResponse { population: 2 }), res);

        // Test the time API point.
        let res = send_request(&url, "time", &json!({}));
        assert_eq!(
            json!(
                { "time": 0.0 }
            ),
            res
        );

        // Test the global property list point. We can't do
        // exact match because the return is every defined
        // global property anywhere in the code.
        let res = send_request(
            &url,
            "global",
            &json!({
                "Global": "List"
            }),
        );
        let list = res.get("List").unwrap().as_array().unwrap();
        let mut found = false;
        for prop in list {
            let prop_val = prop.as_str().unwrap();
            if prop_val == "ixa.WebApiTestGlobal" {
                found = true;
                break;
            }
        }
        assert!(found);

        // Test the global property get API point.
        let res = send_request(
            &url,
            "global",
            &json!({
                "Global": {
                    "Get" : {
                        "property" : "ixa.WebApiTestGlobal"
                    }
                }
            }),
        );
        // The extra quotes here are because we internally JSONify.
        // TODO(cym4@cdc.gov): Should we fix this internally?
        assert_eq!(
            res,
            json!({
                "Value": "\"foobar\""
            })
        );

        // Next
        let res = send_request(&url, "next", &json!({}));
        assert_eq!(res, json!("Ok"));

        // We test breakpoint commands as a group.
        // Breakpoint set
        let res = send_request(
            &url,
            "breakpoint",
            &json!({ "Breakpoint" : { "Set" : { "time": 1.0, "console": false} } }),
        );
        assert_eq!(res, json!("Ok"));

        let res = send_request(
            &url,
            "breakpoint",
            &json!({ "Breakpoint" : { "Set" : { "time": 2.0, "console": false} } }),
        );
        assert_eq!(res, json!("Ok"));

        let res = send_request(
            &url,
            "breakpoint",
            &json!({ "Breakpoint" : { "Delete" : { "id": 0, "all": false} } }),
        );
        assert_eq!(res, json!("Ok"));

        // Breakpoint list
        let res = send_request(&url, "breakpoint", &json!({"Breakpoint": "List"}));
        assert_eq!(
            res,
            json!({"List" : [
                "1: t=2 (First)"
            ]}
            )
        );

        let res = send_request(
            &url,
            "breakpoint",
            &json!({ "Breakpoint" : { "Delete" : { "all": true, } } }),
        );
        assert_eq!(res, json!("Ok"));

        // Check list again
        let res = send_request(&url, "breakpoint", &json!({"Breakpoint": "List"}));
        assert_eq!(
            res,
            json!({"List" : [/* empty list */ ]}
            )
        );

        let res = send_request(&url, "breakpoint", &json!({ "Breakpoint" : "Disable" }));
        assert_eq!(res, json!("Ok"));

        let res = send_request(&url, "breakpoint", &json!({ "Breakpoint" : "Enable" }));
        assert_eq!(res, json!("Ok"));

        // Person properties API.
        let res = send_request(
            &url,
            "people",
            &json!({
                "People" : {
                    "Get" : {
                        "person_id": 0,
                        "property" : "Age"
                    }
                }
            }),
        );
        assert_eq!(
            res,
            json!({"Properties" : [
                ( "Age",  "1" )
            ]}
            )
        );

        // List properties.
        let res = send_request(
            &url,
            "people",
            &json!({
                "People" : "Properties"
            }),
        );
        assert_eq!(
            res,
            json!({"PropertyNames" : [
                "Age"
            ]}
            )
        );

        // Tabulate API.
        let res = send_request(
            &url,
            "people",
            &json!({
                "People" : {
                    "Tabulate" : {
                        "properties": ["Age"]
                    }
                }
            }),
        );

        // This is a hack to deal with these arriving in
        // arbitrary order.
        assert!(
            (res == json!({"Tabulated" : [
                [{ "Age" :  "1" }, 1],
                [{ "Age" :  "2" }, 1]
            ]})) || (res
                == json!({"Tabulated" : [
                    [{ "Age" :  "2" }, 1],
                    [{ "Age" :  "1" }, 1]
                ]})),
        );

        // Valid JSON but wrong type.
        let res = send_request_text(
            &url,
            "breakpoint",
            String::from("{\"Set\": {\"time\" : \"invalid\"}}"),
        );
        assert_eq!(res.status(), StatusCode::BAD_REQUEST);

        // Invalid JSON.
        let res = send_request_text(&url, "next", String::from("{]"));
        assert_eq!(res.status(), StatusCode::BAD_REQUEST);

        // A generic externally added API handler
        let res = send_request(&url, "external", &json!({"External": [1]}));
        assert_eq!(res, json!({"External": [1]}));

        // Test continue and make sure that the context
        // exits.
        send_continue(&url);
        let _ = ctx_thread.join();
    }
}