macro_rules! rumtk_web_run_app {
( ) => { ... };
( $pages:expr ) => { ... };
( $pages:expr, $components:expr ) => { ... };
( $pages:expr, $components:expr, $forms:expr ) => { ... };
( $pages:expr, $components:expr, $forms:expr, $apis:expr ) => { ... };
( $pages:expr, $components:expr, $forms:expr, $apis:expr, $skip_serve:expr ) => { ... };
}Expand description
This is the main macro for defining your applet and launching it. Usage is very simple and the only decision from a user is whether to pass a list of UserPages or a list of UserPages and a list of UserComponents.
These lists are used to automatically register your pages
(e.g. /index => ('index', my_index_function)) and your custom components
(e.g. button => ('button', my_button_function)
This macro will load CSS from predefined sources, concatenate their contents with predefined CSS,
minified the concatenated results, and generate a bundle css file containing the minified results.
The CSS bundle is written to file ./static/css/bundle.min.css.
Note: anything in ./static will be considered static assets that need to be served.
This macro will also parse the command line automatically with a few predefined options and use that information to override the config defaults.
By default, the app is launched to 127.0.0.1:3000 which is the loopback address.
App is served with the best compression algorithm allowed by the client browser.
For testing purposes, the function
§Example Usage
§With Page and Component definition
use rumtk_core::strings::{rumtk_format};
use rumtk_web::{
rumtk_web_run_app,
rumtk_web_render_component,
rumtk_web_render_html,
rumtk_web_get_text_item
};
use rumtk_web::components::form::{FormElementBuilder, props::InputProps, FormElements};
use rumtk_web::{SharedAppState, RenderedPageComponents};
use rumtk_web::{APIPath, URLPath, URLParams, HTMLResult, RUMString, RouterForm};
use rumtk_web::defaults::{DEFAULT_TEXT_ITEM, PARAMS_CONTENTS, PARAMS_CSS_CLASS};
use askama::Template;
// About page
pub fn about(app_state: SharedAppState) -> RenderedPageComponents {
let title_coop = rumtk_web_render_component!("title", [("type", "coop_values")], app_state.clone());
let title_team = rumtk_web_render_component!("title", [("type", "meet_the_team")], app_state.clone());
let text_card_story = rumtk_web_render_component!("text_card", [("type", "story")], app_state.clone());
let text_card_coop = rumtk_web_render_component!("text_card", [("type", "coop_values")], app_state.clone());
let portrait_card = rumtk_web_render_component!("portrait_card", [("section", "company"), ("type", "personnel")], app_state.clone());
let spacer_5 = rumtk_web_render_component!("spacer", [("size", "5")], app_state.clone());
vec![
text_card_story,
spacer_5.clone(),
title_coop,
text_card_coop,
spacer_5,
title_team,
portrait_card
]
}
//Custom component
#[derive(Template, Debug)]
#[template(
source = "
<style>
</style>
{% if custom_css_enabled %}
<link href='/static/components/div.css' rel='stylesheet'>
{% endif %}
<div class='div-{{css_class}}'>{{contents|safe}}</div>
",
ext = "html"
)]
struct MyDiv {
contents: RUMString,
css_class: RUMString,
custom_css_enabled: bool,
}
fn my_div(path_components: URLPath, params: URLParams, state: SharedAppState) -> HTMLResult {
let contents = rumtk_web_get_text_item!(params, PARAMS_CONTENTS, DEFAULT_TEXT_ITEM);
let css_class = rumtk_web_get_text_item!(params, PARAMS_CSS_CLASS, DEFAULT_TEXT_ITEM);
let custom_css_enabled = state.read().expect("Lock failure").config.custom_css;
rumtk_web_render_html!(MyDiv {
contents: RUMString::from(contents),
css_class: RUMString::from(css_class),
custom_css_enabled
})
}
fn my_form (builder: FormElementBuilder) -> FormElements {
vec![
builder("input", "", InputProps::default(), "default")
]
}
fn my_api_handler(path: APIPath, params: URLParams, form: RouterForm, state: SharedAppState) -> HTMLResult {
Err(rumtk_format!(
"No handler registered for API endpoint => {}",
path
))
}
//Requesting to immediately exit instead of indefinitely serving pages so this example can be used as a unit test.
let skip_serve = true;
let result = rumtk_web_run_app!(
vec![("about", about)],
vec![("my_div", my_div)], //Optional, can be omitted alongside the skip_serve flag
vec![("my_form", my_form)], //Optional, can be omitted alongside the skip_serve flag
vec![("v2/add", my_api_handler)], //Optional, can be omitted alongside the skip_serve flag
skip_serve //Omit in production code. This is used so that this example can work as a unit test.
);