diff --git a/mapserver/src/config.rs b/mapserver/src/config.rs index 23a2221..245a7b2 100644 --- a/mapserver/src/config.rs +++ b/mapserver/src/config.rs @@ -1,3 +1,4 @@ +// TODO x,y bounds pub struct Config { /// Must start and end with `/` pub base_url: String, @@ -6,6 +7,7 @@ pub struct Config { pub mapper_threads: usize, pub minetestmapper_args: Vec, pub minetestmapper_path: String, + /// Should end with `/` pub output_path: String, pub tile_size: usize, pub world_path: String, diff --git a/mapserver/src/main.rs b/mapserver/src/main.rs index 84db4f3..0b7f8c7 100644 --- a/mapserver/src/main.rs +++ b/mapserver/src/main.rs @@ -2,8 +2,8 @@ mod config; -use crossbeam_channel::{Receiver, Sender}; -use log::{debug, error, info, warn}; +use crossbeam_channel::Sender; +use log::{debug, error, warn}; use parking_lot::RwLock; use std::{collections::HashMap, io::Read, sync::Arc, time::SystemTime}; use tide::Request; @@ -48,10 +48,15 @@ fn run_minetestmapper( z: i32, x: i32, y: i32, + tile_dir: &str, tile_path: &str, config: Arc, ) -> Result<(), Error> { debug!("Generating tile ({},{},{})", z, x, y); + std::fs::create_dir_all(tile_dir).map_err(|e| { + error!("Generating tile ({},{},{}): {}", z, x, y, e); + Error::Io(e) + })?; std::process::Command::new(&config.minetestmapper_path) .args([ "-i", @@ -89,6 +94,7 @@ fn generate_tile( z: i32, x: i32, y: i32, + tile_dir: &str, tile_path: &str, tasks: Arc>>>, config: Arc, @@ -109,7 +115,7 @@ fn generate_tile( let res: Result<(), Error> = try { if z == config.zoom_default { - run_minetestmapper(z, x, y, tile_path, config)?; + run_minetestmapper(z, x, y, tile_dir, tile_path, config)?; } else { Err(Error::Todo)?; } @@ -130,11 +136,12 @@ fn resp_tile( z: i32, x: i32, y: i32, + tile_dir: &str, tile_path: &str, tasks: Arc>>>, config: Arc, ) -> Result, Error> { - generate_tile(z, x, y, tile_path, tasks, config)?; + generate_tile(z, x, y, tile_dir, tile_path, tasks, config)?; read_tile_file(tile_path) } @@ -147,7 +154,8 @@ async fn req_tile( let x: i32 = req.param("x")?.parse()?; let y: i32 = req.param("y")?.parse()?; - let tile_path = format!("{}/{}/{}/{}.png", config.output_path, z, x, y); + let tile_dir = format!("{}{}/{}", config.output_path, z, x); + let tile_path = format!("{}/{}.png", tile_dir, y); match std::fs::metadata(&tile_path) { Ok(metadata) => match metadata.modified() { @@ -157,7 +165,7 @@ async fn req_tile( .unwrap() .as_secs() > config.cache_age { - match resp_tile(z, x, y, &tile_path, tasks, config) { + match resp_tile(z, x, y, &tile_dir, &tile_path, tasks, config) { Ok(tile_content) => Ok(tide::Response::builder(200) .content_type(http_types::mime::PNG) .body(tile_content) @@ -176,7 +184,7 @@ async fn req_tile( } Err(e) => { warn!("Cannot get modified time of `{}`: {}", tile_path, e); - match resp_tile(z, x, y, &tile_path, tasks, config) { + match resp_tile(z, x, y, &tile_dir, &tile_path, tasks, config) { Ok(tile_content) => Ok(tide::Response::builder(200) .content_type(http_types::mime::PNG) .body(tile_content) @@ -191,7 +199,7 @@ async fn req_tile( } else { warn!("Cannot get metadata of `{}`: {}", tile_path, e); } - match resp_tile(z, x, y, &tile_path, tasks, config) { + match resp_tile(z, x, y, &tile_dir, &tile_path, tasks, config) { Ok(tile_content) => Ok(tide::Response::builder(200) .content_type(http_types::mime::PNG) .body(tile_content)