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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use lazy_static::lazy_static;
use rust_embed::RustEmbed;
use std::sync::Arc;
use tera::Tera;
use warp::Filter;

mod filters;
mod handlers;

#[derive(RustEmbed)]
#[folder = "src/templates/"]
struct Templates;

/// Builds a catalog with root-url `url`. The handlers for this filter takes list of datasets.
pub fn catalog<T: Catalog + Clone>(
    root: String,
    catalog: T,
) -> Result<impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone, anyhow::Error>
{
    lazy_static! {
        static ref TERA: Arc<Tera> = {
            let mut tera = Tera::default();
            for t in &["base.html", "folder.html", "index.html"] {
                let template = Templates::get(&t).unwrap();
                let template = std::str::from_utf8(&template).unwrap();
                tera.add_raw_template(&t, &template).unwrap();
            }
            Arc::new(tera)
        };
    }

    Ok(filters::catalog(root, Arc::clone(&TERA), catalog))
}

pub trait Catalog: Send + Sync {
    /// List of all paths to data sources.
    fn paths<'a>(&'a self) -> Box<dyn Iterator<Item = &str> + 'a>;
}

impl<T: Catalog> Catalog for Arc<T> {
    fn paths<'a>(&'a self) -> Box<dyn Iterator<Item = &str> + 'a> {
        T::paths(self)
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use futures::executor::block_on;

    #[derive(Debug)]
    pub struct TestCatalog {
        paths: Vec<String>,
    }

    impl TestCatalog {
        pub fn test() -> Arc<TestCatalog> {
            Arc::new(TestCatalog {
                paths: [
                    "coads1.nc",
                    "coads2.nc",
                    "path1/hula.nc",
                    "path1/hula2.nc",
                    "path1/sub/hula3.nc",
                    "path2/bula.nc",
                ]
                .iter()
                .map(|s| s.to_string())
                .collect(),
            })
        }
    }

    impl Catalog for Arc<TestCatalog> {
        fn paths<'a>(&'a self) -> Box<dyn Iterator<Item = &str> + 'a> {
            Box::new(self.paths.iter().map(|s| s.as_str()))
        }
    }

    #[test]
    fn setup_catalog() {
        catalog("http://localhost:8001".into(), TestCatalog::test()).unwrap();
    }

    #[test]
    fn does_not_match_data_source() {
        let f = catalog("http://localhost:8001".into(), TestCatalog::test()).unwrap();

        assert_eq!(
            block_on(
                warp::test::request()
                    .method("GET")
                    .path("/data/coads1.nc")
                    .reply(&f)
            )
            .status(),
            404
        );

        assert_eq!(
            block_on(
                warp::test::request()
                    .method("GET")
                    .path("/data/path1/hula.nc")
                    .reply(&f)
            )
            .status(),
            404
        );

        assert_eq!(
            block_on(
                warp::test::request()
                    .method("GET")
                    .path("/data/path1/non-exist.nc")
                    .reply(&f)
            )
            .status(),
            404
        );
    }

    #[test]
    fn matches_root() {
        let f = catalog("http://localhost:8001".into(), TestCatalog::test()).unwrap();

        assert_eq!(
            block_on(warp::test::request().method("GET").path("/data/").reply(&f)).status(),
            200
        );

        assert_eq!(
            block_on(warp::test::request().method("GET").path("/data").reply(&f)).status(),
            200
        );
    }

    #[test]
    fn matches_subpath() {
        let f = catalog("http://localhost:8001".into(), TestCatalog::test()).unwrap();

        assert_eq!(
            block_on(
                warp::test::request()
                    .method("GET")
                    .path("/data/path1/")
                    .reply(&f)
            )
            .status(),
            200
        );

        assert_eq!(
            block_on(
                warp::test::request()
                    .method("GET")
                    .path("/data/path1")
                    .reply(&f)
            )
            .status(),
            200
        );
    }

    #[test]
    fn does_not_match_missing_subpath() {
        let f = catalog("http://localhost:8001".into(), TestCatalog::test()).unwrap();

        assert_eq!(
            block_on(
                warp::test::request()
                    .method("GET")
                    .path("/data/missing_path1/")
                    .reply(&f)
            )
            .status(),
            404
        );
    }
}