Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --preferred-encoding (gzip|brotli) to use when tile is not pre-encoded by source #1189

Merged
merged 22 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions martin/src/args/srv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::srv::{SrvConfig, KEEP_ALIVE_DEFAULT, LISTEN_ADDRESSES_DEFAULT};
use martin_tile_utils::Encoding;
use TileEncoding::{Brotli, Gzip};

#[derive(clap::Args, Debug, PartialEq, Default)]
#[command(about, version)]
Expand All @@ -10,6 +12,8 @@ pub struct SrvArgs {
/// Number of web server workers
#[arg(short = 'W', long)]
pub workers: Option<usize>,
#[arg(help = "to do", short, long)]
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved
pub preferred_encoding: Option<String>,
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved
}

impl SrvArgs {
Expand All @@ -24,5 +28,15 @@ impl SrvArgs {
if self.workers.is_some() {
srv_config.worker_processes = self.workers;
}
if let Some(encoding_str) = self.preferred_encoding {
match encoding_str.as_str() {
"gzip" => srv_config.preferred_encoding = Option::from(Encoding::Gzip),
"brotli" => srv_config.preferred_encoding = Option::from(Encoding::Brotli),
"br" => srv_config.preferred_encoding = Option::from(Encoding::Brotli),
_ => panic!("Invalid encoding: {}", encoding_str),
}
} else {
srv_config.preferred_encoding = Option::from(Encoding::Brotli);
}
}
}
3 changes: 3 additions & 0 deletions martin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use futures::future::try_join_all;
use log::info;
use serde::{Deserialize, Serialize};
use subst::VariableMap;
use martin_tile_utils::Encoding;

#[cfg(any(feature = "mbtiles", feature = "pmtiles", feature = "sprites"))]
use crate::file_config::FileConfigEnum;
Expand All @@ -32,6 +33,7 @@ pub struct ServerState {
pub sprites: SpriteSources,
#[cfg(feature = "fonts")]
pub fonts: FontSources,
pub preferred_encoding: Encoding,
}

#[serde_with::skip_serializing_none]
Expand Down Expand Up @@ -143,6 +145,7 @@ impl Config {
#[cfg(feature = "fonts")]
fonts: FontSources::resolve(&mut self.fonts)?,
cache,
preferred_encoding: self.srv.preferred_encoding.unwrap_or(Encoding::Brotli),
})
}

Expand Down
3 changes: 3 additions & 0 deletions martin/src/srv/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use martin_tile_utils::Encoding;
use serde::{Deserialize, Serialize};

pub const KEEP_ALIVE_DEFAULT: u64 = 75;
Expand All @@ -9,6 +10,7 @@ pub struct SrvConfig {
pub keep_alive: Option<u64>,
pub listen_addresses: Option<String>,
pub worker_processes: Option<usize>,
pub preferred_encoding: Option<Encoding>,
}

#[cfg(test)]
Expand All @@ -31,6 +33,7 @@ mod tests {
keep_alive: Some(75),
listen_addresses: some("0.0.0.0:3000"),
worker_processes: Some(8),
preferred_encoding: None,
}
);
}
Expand Down
3 changes: 2 additions & 1 deletion martin/src/srv/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ pub fn new_server(config: SrvConfig, state: ServerState) -> MartinResult<(Server

let app = App::new()
.app_data(Data::new(state.tiles.clone()))
.app_data(Data::new(state.cache.clone()));
.app_data(Data::new(state.cache.clone()))
.app_data(Data::new(state.preferred_encoding));
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(feature = "sprites")]
let app = app.app_data(Data::new(state.sprites.clone()));
Expand Down
Loading