apimock/
lib.rs

1//! API mock Server generating HTTP/JSON responses
2//!
3//! Mocking helper to develop microservices and APIs.
4//! [hyper](https://hyper.rs/)-based HTTP server generating REST responses containing JSON ones.
5
6pub mod core;
7use core::app::App;
8
9const CONFIG_FILEPATH_OPTION_NAMES: [&str; 2] = ["-c", "--config"];
10const CONFIG_LISTENER_PORT_OPTION_NAMES: [&str; 2] = ["-p", "--port"];
11const MIDDLEWARE_FILEPATH_OPTION_NAMES: [&str; 1] = ["--middleware"];
12const DEFAULT_CONFIG_FILENAME: &str = "apimock.toml";
13const DEFAULT_MIDDLEWARE_FILEPATH: &str = "./middleware.rhai";
14
15/// return hyper http server
16#[cfg(not(feature = "spawn"))]
17pub async fn server(
18    config_filepath: &str,
19    listener_port: Option<u16>,
20    middleware_filepath: Option<String>,
21) -> App {
22    App::new(
23        config_filepath,
24        listener_port,
25        middleware_filepath,
26        None,
27        true,
28    )
29    .await
30}
31
32#[cfg(feature = "spawn")]
33use tokio::sync::mpsc::Sender;
34
35/// accept sender to main proc set to logger and
36/// return hyper http server
37/// `includes_ansi_codes`: if true, log includes ansi escape codes for console text color
38#[cfg(feature = "spawn")]
39pub async fn server(
40    config_filepath: &str,
41    middleware_filepath: Option<String>,
42    spawn_tx: Sender<String>,
43    includes_ansi_codes: bool,
44) -> App {
45    App::new(
46        config_filepath,
47        None,
48        middleware_filepath,
49        Some(spawn_tx),
50        includes_ansi_codes,
51    )
52    .await
53}