ic_pluto/static_files.rs
1/// The main way to load the static files as ready-to-use routes in the application.
2///
3/// This ensures every file is accessible through HTTP GET requests by adding all of them to the router automatically.
4///
5/// # Example
6///
7/// The best way to use this macro is to include it in the bootstraping step for the router:
8///
9///
10/// ```rust
11/// #[post_upgrade]
12/// fn post_upgrade() {
13/// ROUTER.with(|r| {
14/// let mut instance = controller::setup();
15/// pluto::use_static_files!(instance);
16/// *r.borrow_mut() = instance;
17/// })
18/// }
19/// ````
20#[macro_export]
21macro_rules! use_static_files {
22 (
23 $router:path
24 ) => {
25 for file in crate::compiled::templates::statics::STATICS.iter() {
26 $router.get(&format!("/{}", file.name), false, |_req| async {
27 Ok(HttpResponse {
28 status_code: 200,
29 headers: HashMap::from([("Content-Type".to_string(), file.mime.to_string())]),
30 body: if file.mime.type_() == "text" || file.mime.subtype() == "json" {
31 pluto::http::HttpBody::String(
32 String::from_utf8(file.content.to_vec()).unwrap(),
33 )
34 } else {
35 file.content.to_owned().into()
36 },
37 })
38 });
39 }
40 };
41}