martin 1.11.0

Blazing fast and lightweight tile server with PostGIS, MBTiles, and PMTiles support
Documentation
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#![cfg(feature = "mbtiles")]
#![expect(clippy::print_stdout, reason = "test diagnostics on failure")]

use actix_web::http::header::{ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_TYPE};
use actix_web::test::{TestRequest, call_service, read_body, read_body_json};
use indoc::formatdoc;
#[cfg(all(feature = "rendering", target_os = "linux"))]
use insta::assert_yaml_snapshot;
use martin::config::file::srv::SrvConfig;
use martin_tile_utils::{decode_brotli, decode_gzip};
use mbtiles::sqlx::SqliteConnection;
use mbtiles::{Mbtiles, temp_named_mbtiles};
use tilejson::TileJSON;

pub mod utils;
pub use utils::*;

macro_rules! create_app {
    ($sources:expr) => {{
        let state = mock_sources(mock_cfg($sources)).await.0;
        ::actix_web::test::init_service(
            ::actix_web::App::new()
                .app_data(actix_web::web::Data::new(
                    ::martin::srv::Catalog::new(
                        #[cfg(any(feature = "sprites", feature = "fonts", feature = "styles"))]
                        &state,
                    )
                    .unwrap(),
                ))
                .app_data(actix_web::web::Data::new(state.tile_manager))
                .app_data(actix_web::web::Data::new(SrvConfig::default()))
                .configure(|c| ::martin::srv::router(c, &SrvConfig::default())),
        )
        .await
    }};
}

fn test_get(path: &str) -> TestRequest {
    TestRequest::get().uri(path)
}

#[expect(clippy::similar_names)]
async fn config(
    test_name: &str,
) -> (
    String,
    (
        (Mbtiles, SqliteConnection),
        (Mbtiles, SqliteConnection),
        (Mbtiles, SqliteConnection),
        (Mbtiles, SqliteConnection),
        (Mbtiles, SqliteConnection),
    ),
) {
    let json_script = include_str!("../../tests/fixtures/mbtiles/json.sql");
    let (json_mbt, json_conn, json_file) =
        temp_named_mbtiles(&format!("{test_name}_json"), json_script).await;
    let mvt_script = include_str!("../../tests/fixtures/mbtiles/world_cities.sql");
    let (mvt_mbt, mvt_conn, mvt_file) =
        temp_named_mbtiles(&format!("{test_name}_mvt"), mvt_script).await;
    let raw_mvt_script = include_str!("../../tests/fixtures/mbtiles/uncompressed_mvt.sql");
    let (raw_mvt_mbt, raw_mvt_conn, raw_mvt_file) =
        temp_named_mbtiles(&format!("{test_name}_raw_mvt"), raw_mvt_script).await;
    let raw_mlt_script = include_str!("../../tests/fixtures/mbtiles/mlt.sql");
    let (raw_mlt_mbt, raw_mlt_conn, raw_mlt_file) =
        temp_named_mbtiles(&format!("{test_name}_raw_mlt"), raw_mlt_script).await;
    let webp_script = include_str!("../../tests/fixtures/mbtiles/webp-no-primary.sql");
    let (webp_mbt, webp_conn, webp_file) =
        temp_named_mbtiles(&format!("{test_name}_webp"), webp_script).await;

    (
        formatdoc! {"
    mbtiles:
        sources:
            m_json: {json}
            m_mvt: {mvt}
            m_raw_mvt: {raw_mvt}
            m_raw_mlt: {raw_mlt}
            m_webp: {webp}
    ",
        json = json_file.display(),
        mvt = mvt_file.display(),
        raw_mvt = raw_mvt_file.display(),
        raw_mlt = raw_mlt_file.display(),
        webp = webp_file.display()
        },
        (
            (json_mbt, json_conn),
            (mvt_mbt, mvt_conn),
            (raw_mvt_mbt, raw_mvt_conn),
            (raw_mlt_mbt, raw_mlt_conn),
            (webp_mbt, webp_conn),
        ),
    )
}

#[cfg(all(feature = "rendering", target_os = "linux"))]
#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_catalog_with_rendering_feature() {
    let (config, _conns) = config("mbt_get_catalog").await;
    let app = create_app!(&config);
    let req = test_get("/catalog").to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    let body: serde_json::Value = read_body_json(response).await;
    assert_yaml_snapshot!(body, @r#"
    fonts: {}
    settings:
      rendering: false
    sprites: {}
    styles: {}
    tiles:
      m_json:
        content_type: application/json
        name: Dummy json data
      m_mvt:
        content_encoding: gzip
        content_type: application/x-protobuf
        description: Major cities from Natural Earth data
        name: Major cities from Natural Earth data
      m_raw_mlt:
        attribution: "<a href=\"https://www.openmaptiles.org/\" target=\"_blank\">&copy; OpenMapTiles</a> <a href=\"https://www.openstreetmap.org/copyright\" target=\"_blank\">&copy; OpenStreetMap contributors</a>"
        content_type: application/vnd.maplibre-tile
        description: "A tileset showcasing all layers in OpenMapTiles. https://openmaptiles.org"
        name: OpenMapTiles
      m_raw_mvt:
        content_type: application/x-protobuf
        description: Major cities from Natural Earth data
        name: Major cities from Natural Earth data
      m_webp:
        content_type: image/webp
        name: ne2sr
    "#);
}

#[cfg(all(feature = "rendering", target_os = "linux"))]
#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_catalog_gzip_with_rendering_feature() {
    let (config, _conns) = config("mbt_get_catalog_gzip").await;
    let app = create_app!(&config);
    let accept = (ACCEPT_ENCODING, "gzip");
    let req = test_get("/catalog").insert_header(accept).to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    let body = decode_gzip(&read_body(response).await).unwrap();
    let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
    assert_yaml_snapshot!(body, @r#"
    fonts: {}
    settings:
      rendering: false
    sprites: {}
    styles: {}
    tiles:
      m_json:
        content_type: application/json
        name: Dummy json data
      m_mvt:
        content_encoding: gzip
        content_type: application/x-protobuf
        description: Major cities from Natural Earth data
        name: Major cities from Natural Earth data
      m_raw_mlt:
        attribution: "<a href=\"https://www.openmaptiles.org/\" target=\"_blank\">&copy; OpenMapTiles</a> <a href=\"https://www.openstreetmap.org/copyright\" target=\"_blank\">&copy; OpenStreetMap contributors</a>"
        content_type: application/vnd.maplibre-tile
        description: "A tileset showcasing all layers in OpenMapTiles. https://openmaptiles.org"
        name: OpenMapTiles
      m_raw_mvt:
        content_type: application/x-protobuf
        description: Major cities from Natural Earth data
        name: Major cities from Natural Earth data
      m_webp:
        content_type: image/webp
        name: ne2sr
    "#);
}

#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_tilejson() {
    let (config, _conns) = config("mbt_get_tilejson").await;
    let app = create_app!(&config);
    let req = test_get("/m_mvt").to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    let headers = response.headers();
    assert_eq!(headers.get(CONTENT_TYPE).unwrap(), "application/json");
    assert!(headers.get(CONTENT_ENCODING).is_none());
    let body: TileJSON = read_body_json(response).await;
    assert_eq!(body.maxzoom, Some(6));
}

#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_tilejson_gzip() {
    let (config, _conns) = config("mbt_get_tilejson_gzip").await;
    let app = create_app!(&config);
    let accept = (ACCEPT_ENCODING, "gzip");
    let req = test_get("/m_webp").insert_header(accept).to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    let headers = response.headers();
    assert_eq!(headers.get(CONTENT_TYPE).unwrap(), "application/json");
    assert_eq!(headers.get(CONTENT_ENCODING).unwrap(), "gzip");
    let body = decode_gzip(&read_body(response).await).unwrap();
    let body: TileJSON = serde_json::from_slice(body.as_slice()).unwrap();
    assert_eq!(body.maxzoom, Some(0));
}

#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_raster() {
    let (config, _conns) = config("mbt_get_raster").await;
    let app = create_app!(&config);
    let req = test_get("/m_webp/0/0/0").to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    assert_eq!(response.headers().get(CONTENT_TYPE).unwrap(), "image/webp");
    assert!(response.headers().get(CONTENT_ENCODING).is_none());
    let body = read_body(response).await;
    assert_eq!(body.len(), 23);
}

/// get a raster tile with accepted gzip enc, but should still be non-gzipped
#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_raster_gzip() {
    let (config, _conns) = config("mbt_get_raster_gzip").await;
    let app = create_app!(&config);
    let accept = (ACCEPT_ENCODING, "gzip");
    let req = test_get("/m_webp/0/0/0").insert_header(accept).to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    assert_eq!(response.headers().get(CONTENT_TYPE).unwrap(), "image/webp");
    assert!(response.headers().get(CONTENT_ENCODING).is_none());
    let body = read_body(response).await;
    assert_eq!(body.len(), 23);
}

#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_mvt() {
    let (config, _conns) = config("mbt_get_mvt").await;
    let app = create_app!(&config);
    let req = test_get("/m_mvt/0/0/0").to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    println!("Status = {:?}", response.status());
    println!("Headers = {:?}", response.headers());

    assert_eq!(
        response.headers().get(CONTENT_TYPE).unwrap(),
        "application/x-protobuf"
    );
    assert!(response.headers().get(CONTENT_ENCODING).is_none());
    let body = read_body(response).await;
    assert_eq!(body.len(), 1828);
}

/// get an MVT tile with accepted gzip enc
#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_mvt_gzip() {
    let (config, _conns) = config("mbt_get_mvt_gzip").await;
    let app = create_app!(&config);
    let accept = (ACCEPT_ENCODING, "gzip");
    let req = test_get("/m_mvt/0/0/0").insert_header(accept).to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    assert_eq!(
        response.headers().get(CONTENT_TYPE).unwrap(),
        "application/x-protobuf"
    );
    assert_eq!(response.headers().get(CONTENT_ENCODING).unwrap(), "gzip");
    let body = read_body(response).await;
    assert_eq!(body.len(), 1107); // this number could change if compression gets more optimized
    let body = decode_gzip(&body).unwrap();
    assert_eq!(body.len(), 1828);
}

/// get an MVT tile with accepted brotli enc
#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_mvt_brotli() {
    let (config, _conns) = config("mbt_get_mvt_brotli").await;
    let app = create_app!(&config);
    let accept = (ACCEPT_ENCODING, "br");
    let req = test_get("/m_mvt/0/0/0").insert_header(accept).to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    assert_eq!(
        response.headers().get(CONTENT_TYPE).unwrap(),
        "application/x-protobuf"
    );
    assert_eq!(response.headers().get(CONTENT_ENCODING).unwrap(), "br");
    let body = read_body(response).await;
    assert_eq!(body.len(), 871); // this number could change if compression gets more optimized
    let body = decode_brotli(&body).unwrap();
    assert_eq!(body.len(), 1828);
}

/// get an uncompressed MVT tile
#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_raw_mvt() {
    let (config, _conns) = config("mbt_get_raw_mvt").await;
    let app = create_app!(&config);
    let req = test_get("/m_raw_mvt/0/0/0").to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    assert_eq!(
        response.headers().get(CONTENT_TYPE).unwrap(),
        "application/x-protobuf"
    );
    assert!(response.headers().get(CONTENT_ENCODING).is_none());
    let body = read_body(response).await;
    assert_eq!(body.len(), 2);
}

/// get an uncompressed MLT tile
#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_raw_mlt() {
    let (config, _conns) = config("mbt_get_raw_mlt").await;
    let app = create_app!(&config);
    let req = test_get("/m_raw_mlt/0/0/0").to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    assert_eq!(
        response.headers().get(CONTENT_TYPE).unwrap(),
        "application/vnd.maplibre-tile"
    );
    assert_eq!(response.headers().get(CONTENT_ENCODING), None);
    let body = read_body(response).await;
    assert_eq!(body.iter().as_slice(), &[0x02, 0x01]);
}

/// get an uncompressed MVT tile with accepted gzip
#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_raw_mvt_gzip() {
    let (config, _conns) = config("mbt_get_raw_mvt_gzip").await;
    let app = create_app!(&config);
    let accept = (ACCEPT_ENCODING, "gzip");
    let req = test_get("/m_raw_mvt/0/0/0")
        .insert_header(accept)
        .to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    assert_eq!(
        response.headers().get(CONTENT_TYPE).unwrap(),
        "application/x-protobuf"
    );
    assert_eq!(response.headers().get(CONTENT_ENCODING).unwrap(), "gzip");
    let body = read_body(response).await;
    assert_eq!(body.len(), 22); // this number could change if compression gets more optimized
    let body = decode_gzip(&body).unwrap();
    assert_eq!(body.len(), 2);
}

/// get an uncompressed MVT tile with accepted both gzip and brotli enc
#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_raw_mvt_gzip_br() {
    let (config, _conns) = config("mbt_get_raw_mvt_gzip_br").await;
    let app = create_app!(&config);
    // Sadly, most browsers prefer to ask for gzip - maybe we should force brotli if supported.
    let accept = (ACCEPT_ENCODING, "br, gzip, deflate");
    let req = test_get("/m_raw_mvt/0/0/0")
        .insert_header(accept)
        .to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    assert_eq!(
        response.headers().get(CONTENT_TYPE).unwrap(),
        "application/x-protobuf"
    );
    assert_eq!(response.headers().get(CONTENT_ENCODING).unwrap(), "gzip");
    let body = read_body(response).await;
    assert_eq!(body.len(), 22); // this number could change if compression gets more optimized
    let body = decode_gzip(&body).unwrap();
    assert_eq!(body.len(), 2);
}

/// get a JSON tile
#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_json() {
    let (config, _conns) = config("mbt_get_json").await;
    let app = create_app!(&config);
    let req = test_get("/m_json/0/0/0").to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    assert_eq!(
        response.headers().get(CONTENT_TYPE).unwrap(),
        "application/json"
    );
    assert!(response.headers().get(CONTENT_ENCODING).is_none());
    let body = read_body(response).await;
    assert_eq!(body.len(), 13);
}

/// get a JSON tile with accepted gzip
#[actix_rt::test]
#[tracing_test::traced_test]
async fn mbt_get_json_gzip() {
    let (config, _conns) = config("mbt_get_json_gzip").await;
    let app = create_app!(&config);
    let accept = (ACCEPT_ENCODING, "gzip");
    let req = test_get("/m_json/0/0/0").insert_header(accept).to_request();
    let response = call_service(&app, req).await;
    let response = assert_response(response).await;
    assert_eq!(
        response.headers().get(CONTENT_TYPE).unwrap(),
        "application/json"
    );
    assert_eq!(response.headers().get(CONTENT_ENCODING).unwrap(), "gzip");
    let body = read_body(response).await;
    assert_eq!(body.len(), 33); // this number could change if compression gets more optimized
    let body = decode_gzip(&body).unwrap();
    assert_eq!(body.len(), 13);
}