Add HttpFetcherConfig

This commit is contained in:
Maximilian Ammann 2022-01-14 20:19:57 +01:00
parent 956bca24b8
commit 1165869432
7 changed files with 23 additions and 14 deletions

View File

@ -32,7 +32,6 @@ console_error_panic_hook = "0.1"
winit = { version = "0.26", default-features = false } winit = { version = "0.26", default-features = false }
web-sys = { version = "0.3", features = [ web-sys = { version = "0.3", features = [
"Window", "Window",
"Worker", "WorkerOptions", "WorkerType", "MessageEvent",
"Headers", "Headers",
"WorkerGlobalScope", "Request", "RequestInit", "RequestMode", "Response", "WorkerGlobalScope", "Request", "RequestInit", "RequestMode", "Response",
] } ] }

View File

@ -8,10 +8,15 @@ pub mod static_tile_fetcher;
pub mod web_tile_fetcher; pub mod web_tile_fetcher;
pub mod worker_loop; pub mod worker_loop;
pub struct HttpFetcherConfig {
/// Under which path should we cache requests.
pub cache_path: String,
}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] #[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait HttpFetcher { pub trait HttpFetcher {
fn new() -> Self; fn new(config: HttpFetcherConfig) -> Self;
async fn fetch(&self, url: &str) -> Result<Vec<u8>, Error>; async fn fetch(&self, url: &str) -> Result<Vec<u8>, Error>;
} }
@ -19,7 +24,7 @@ pub trait HttpFetcher {
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] #[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait TileFetcher { pub trait TileFetcher {
fn new() -> Self; fn new(config: HttpFetcherConfig) -> Self;
async fn fetch_tile(&self, coords: &TileCoords) -> Result<Vec<u8>, Error>; async fn fetch_tile(&self, coords: &TileCoords) -> Result<Vec<u8>, Error>;
fn sync_fetch_tile(&self, coords: &TileCoords) -> Result<Vec<u8>, Error>; fn sync_fetch_tile(&self, coords: &TileCoords) -> Result<Vec<u8>, Error>;

View File

@ -7,6 +7,7 @@ use log::error;
use crate::coords::TileCoords; use crate::coords::TileCoords;
use crate::error::Error; use crate::error::Error;
use crate::io::HttpFetcherConfig;
use super::TileFetcher; use super::TileFetcher;
@ -23,7 +24,7 @@ impl StaticTileFetcher {
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] #[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl TileFetcher for StaticTileFetcher { impl TileFetcher for StaticTileFetcher {
fn new() -> Self { fn new(_config: HttpFetcherConfig) -> Self {
Self {} Self {}
} }

View File

@ -1,6 +1,6 @@
use crate::coords::TileCoords; use crate::coords::TileCoords;
use crate::error::Error; use crate::error::Error;
use crate::io::{HttpFetcher, TileFetcher}; use crate::io::{HttpFetcher, HttpFetcherConfig, TileFetcher};
use crate::platform::PlatformHttpFetcher; use crate::platform::PlatformHttpFetcher;
use async_trait::async_trait; use async_trait::async_trait;
@ -11,9 +11,9 @@ pub struct WebTileFetcher {
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] #[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl TileFetcher for WebTileFetcher { impl TileFetcher for WebTileFetcher {
fn new() -> Self { fn new(config: HttpFetcherConfig) -> Self {
Self { Self {
http_fetcher: PlatformHttpFetcher::new(), http_fetcher: PlatformHttpFetcher::new(config),
} }
} }

View File

@ -8,7 +8,7 @@ use crate::coords::TileCoords;
use vector_tile::parse_tile_bytes; use vector_tile::parse_tile_bytes;
use crate::io::web_tile_fetcher::WebTileFetcher; use crate::io::web_tile_fetcher::WebTileFetcher;
use crate::io::TileFetcher; use crate::io::{HttpFetcherConfig, TileFetcher};
use crate::render::ShaderVertex; use crate::render::ShaderVertex;
use crate::tesselation::{IndexDataType, OverAlignedVertexBuffer, Tesselated}; use crate::tesselation::{IndexDataType, OverAlignedVertexBuffer, Tesselated};
@ -49,7 +49,9 @@ impl WorkerLoop {
} }
pub async fn run_loop(&mut self) { pub async fn run_loop(&mut self) {
let fetcher = WebTileFetcher::new(); let fetcher = WebTileFetcher::new(HttpFetcherConfig {
cache_path: "/tmp/mapr-cache".to_string(),
});
// let fetcher = StaticTileFetcher::new(); // let fetcher = StaticTileFetcher::new();
let mut current_id = 0; let mut current_id = 0;

View File

@ -7,7 +7,7 @@ use reqwest_middleware_cache::managers::CACacheManager;
use reqwest_middleware_cache::{Cache, CacheMode}; use reqwest_middleware_cache::{Cache, CacheMode};
use crate::error::Error; use crate::error::Error;
use crate::io::HttpFetcher; use crate::io::{HttpFetcher, HttpFetcherConfig};
impl From<reqwest::Error> for Error { impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self { fn from(err: reqwest::Error) -> Self {
@ -28,14 +28,16 @@ pub struct PlatformHttpFetcher {
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] #[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl HttpFetcher for PlatformHttpFetcher { impl HttpFetcher for PlatformHttpFetcher {
fn new() -> Self { fn new(config: HttpFetcherConfig) -> Self {
let mut builder = ClientBuilder::new(Client::new()); let mut builder = ClientBuilder::new(Client::new());
// FIXME: Cache only works on desktop so far // FIXME: Cache only works on desktop so far
if cfg!(not(any(target_os = "android", target_arch = "aarch64"))) { if cfg!(not(any(target_os = "android", target_arch = "aarch64"))) {
builder = builder.with(Cache { builder = builder.with(Cache {
mode: CacheMode::Default, mode: CacheMode::Default,
cache_manager: CACacheManager::default(), cache_manager: CACacheManager {
path: config.cache_path,
},
}); });
} }

View File

@ -1,5 +1,5 @@
use crate::error::Error; use crate::error::Error;
use crate::io::HttpFetcher; use crate::io::{HttpFetcher, HttpFetcherConfig};
use async_trait::async_trait; use async_trait::async_trait;
use js_sys::ArrayBuffer; use js_sys::ArrayBuffer;
use js_sys::Uint8Array; use js_sys::Uint8Array;
@ -19,7 +19,7 @@ pub struct PlatformHttpFetcher;
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] #[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl HttpFetcher for PlatformHttpFetcher { impl HttpFetcher for PlatformHttpFetcher {
fn new() -> Self { fn new(_config: HttpFetcherConfig) -> Self {
Self {} Self {}
} }