Skip to main content

rumtk_web/api/
mod.rs

1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025  Ethan Dixon
6 * Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21mod email;
22
23use crate::utils::APIFunction;
24use crate::{APIPath, FormData, HTMLResult, RUMWebData, SharedAppState};
25use rumtk_core::cache::{new_cache, LazyRUMCache};
26use rumtk_core::strings::{rumtk_format, RUMString};
27use rumtk_core::{rumtk_cache_get, rumtk_cache_push};
28
29pub type APICache = LazyRUMCache<RUMString, APIFunction>;
30pub type APIItem<'a> = (&'a str, APIFunction);
31pub type UserAPIEndpoints<'a> = Vec<APIItem<'a>>;
32pub type APICacheItem = APIFunction;
33
34static mut API_CACHE: APICache = new_cache();
35const DEFAULT_API_HANDLER: APIFunction =
36    |path: APIPath, params: RUMWebData, form: FormData, state: SharedAppState| -> HTMLResult {
37        Err(rumtk_format!(
38            "No handler registered for API endpoint => {}",
39            path
40        ))
41    };
42
43pub fn register_api_endpoint(name: &str, api_handler: APIFunction) -> APICacheItem {
44    let key = RUMString::from(name);
45    rumtk_cache_push!(&raw mut API_CACHE, &key, api_handler);
46
47    println!(
48        "  ➡ Registered api endpoint {} => api function [{:?}]",
49        name, &api_handler
50    );
51    api_handler
52}
53
54pub fn get_endpoint(name: &str) -> Option<APICacheItem> {
55    rumtk_cache_get!(
56        &raw mut API_CACHE,
57        &RUMString::from(name)
58    )
59}
60
61pub fn get_default_api_handler() -> &'static APIFunction {
62    &DEFAULT_API_HANDLER
63}
64
65pub fn init_endpoints(user_components: Option<UserAPIEndpoints>) {
66    println!("🌩 Registering API Endpoints! 🌩");
67    /* Init any user prescribed components */
68    for (name, value) in user_components.unwrap_or_default() {
69        let _ = register_api_endpoint(name, value);
70    }
71    println!("🌩 ~~~~~~~~~~~~~~~~~~~~~~ 🌩");
72}
73
74#[macro_export]
75macro_rules! rumtk_web_register_api {
76    ( $key:expr, $fxn:expr ) => {{
77        use $crate::api::register_api_endpoint;
78        register_api_endpoint($key, $fxn)
79    }};
80}
81
82#[macro_export]
83macro_rules! rumtk_web_get_api_endpoint {
84    ( $key:expr ) => {{
85        use $crate::api::get_endpoint;
86        get_endpoint($key)
87    }};
88}
89
90#[macro_export]
91macro_rules! rumtk_web_init_api_endpoints {
92    ( $pages:expr ) => {{
93        use $crate::api::init_endpoints;
94        init_endpoints($pages)
95    }};
96}