aboutsummaryrefslogtreecommitdiff
path: root/server/src/assets.rs
blob: 7195fbfd7e5a72e43adad9405327c5d5db3a1c06 (plain)
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
/*
    This file is part of keks-meet (https://codeberg.org/metamuffin/keks-meet)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2023 metamuffin <metamuffin.org>
*/

#[cfg(debug_assertions)]
#[macro_export]
macro_rules! s_file {
    ($path: literal, $content_type: literal) => {
        warp::fs::file(concat!("../", $path))
    };
}

#[cfg(debug_assertions)]
#[macro_export]
macro_rules! s_asset_dir {
    ($path: literal) => {
        warp::fs::dir(concat!("../", $path))
    };
}

#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! s_file {
    ($path: literal, $content_type: literal) => {
        warp::any().map(|| {
            warp::reply::with_header(
                include_bytes!(concat!("../../", $path)).to_vec(),
                "content-type",
                $content_type,
            )
        })
    };
}

#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! s_asset_dir {
    ($path:literal) => {{
        use include_dir::{include_dir, Dir};
        const DIR: Dir = include_dir!(concat!("$CARGO_MANIFEST_DIR/../", $path));
        warp::path::tail().and_then(|t: warp::path::Tail| async move {
            let path = t.as_str();
            let content_type = match &path {
                _ if path.ends_with(".wasm") => "application/wasm",
                _ if path.ends_with(".js") => "application/javascript",
                _ if path.ends_with(".css") => "text/css",
                _ if path.ends_with(".svg") => "image/svg+xml",
                _ if path.ends_with(".ini") => "text/plain",
                _ => "application/octet-stream",
            };
            DIR.get_file(path)
                .map(|f| warp::reply::with_header(f.contents(), "content-type", content_type))
                .ok_or(warp::reject::not_found())
        })
    }};
}