From e79a1325a16a2c439d93b0cba291f3ebc56d8cc9 Mon Sep 17 00:00:00 2001 From: zerj9 <96551236+zerj9@users.noreply.github.com> Date: Sun, 15 Dec 2024 22:00:21 +0000 Subject: [PATCH] fix: update tile endpoint --- gridwalk-backend/src/core/connector.rs | 29 ++++++++++++--- gridwalk-backend/src/routes/mod.rs | 4 +- gridwalk-backend/src/routes/tiles.rs | 51 ++++++++++++++++++++------ gridwalk-backend/src/server.rs | 1 + 4 files changed, 66 insertions(+), 19 deletions(-) diff --git a/gridwalk-backend/src/core/connector.rs b/gridwalk-backend/src/core/connector.rs index 2d20d98..21afdc2 100644 --- a/gridwalk-backend/src/core/connector.rs +++ b/gridwalk-backend/src/core/connector.rs @@ -103,7 +103,14 @@ pub trait GeoConnector: Send + Sync { async fn disconnect(&mut self) -> Result<()>; async fn create_namespace(&self, name: &str) -> Result<()>; async fn list_sources(&self, namespace: &str) -> Result>; - async fn get_tile(&self, z: u32, x: u32, y: u32) -> Result>; + async fn get_tile( + &self, + namespace: &str, + source_name: &str, + z: u32, + x: u32, + y: u32, + ) -> Result>; } #[derive(Clone, Debug, Serialize, Deserialize)] @@ -195,14 +202,24 @@ impl GeoConnector for PostgisConnector { .await .map_err(|e| anyhow!("Failed to execute query to list sources: {}", e))?; let sources: Vec = rows.iter().map(|row| row.get(0)).collect(); + let test_tile = self + .get_tile("f57e7ba0-a30f-47bd-b641-11d5e25b9978", "test", 0, 0, 0) + .await; + println!("{:?}", test_tile); Ok(sources) } - async fn get_tile(&self, z: u32, x: u32, y: u32) -> Result> { + async fn get_tile( + &self, + namespace: &str, + source_name: &str, + z: u32, + x: u32, + y: u32, + ) -> Result> { println!("Fetching MVT for tile z:{} x:{} y:{}", z, x, y); let pool = self.pool.as_ref(); let client = pool.get().await?; - let _table_name = "test"; let _geom_column = "geom"; let query = format!( " @@ -223,7 +240,7 @@ mvt_data AS ( ) AS geom, t.name FROM - test t, + {table} t, bounds WHERE ST_Intersects(t.geom, bounds.geom) @@ -231,14 +248,14 @@ mvt_data AS ( SELECT ST_AsMVT(mvt_data.*, 'blah', 4096, 'geom') AS mvt FROM mvt_data; ", + table = format!("\"{}\".\"{}\"", namespace, source_name), z = z, x = x, y = y, ); let row = client.query_one(&query, &[]).await?; - println!("{row:?}"); + //println!("{row:?}"); // Debugging let mvt_data: Vec = row.get(0); - println!("{mvt_data:?}"); Ok(mvt_data) } } diff --git a/gridwalk-backend/src/routes/mod.rs b/gridwalk-backend/src/routes/mod.rs index b0f8843..44cd455 100644 --- a/gridwalk-backend/src/routes/mod.rs +++ b/gridwalk-backend/src/routes/mod.rs @@ -2,7 +2,7 @@ mod connector; mod layer; mod os_token; mod project; -//mod tiles; +mod tiles; mod user; mod workspace; @@ -10,6 +10,6 @@ pub use connector::*; pub use layer::*; pub use os_token::*; pub use project::*; -//pub use tiles::*; +pub use tiles::*; pub use user::*; pub use workspace::*; diff --git a/gridwalk-backend/src/routes/tiles.rs b/gridwalk-backend/src/routes/tiles.rs index 3aedb27..8053ca2 100644 --- a/gridwalk-backend/src/routes/tiles.rs +++ b/gridwalk-backend/src/routes/tiles.rs @@ -1,8 +1,8 @@ use crate::app_state::AppState; -use crate::core::Connection; -//use crate::data::Database; +use crate::auth::AuthUser; +use crate::core::{ConnectionAccess, Workspace}; use axum::{ - extract::{Path, State}, + extract::{Extension, Path, State}, http::{header, StatusCode}, response::{IntoResponse, Response}, }; @@ -11,22 +11,51 @@ use std::sync::Arc; pub async fn tiles( State(state): State>, - Path((workspace_id, connection_id, z, x, y)): Path<(String, String, u32, u32, u32)>, + Extension(auth_user): Extension, + Path((workspace_id, source_name, connection_id, z, x, y)): Path<( + String, + String, + String, + u32, + u32, + u32, + )>, ) -> impl IntoResponse { - let connection = Connection::from_id(&state.app_data, &workspace_id, &connection_id) + let _user = auth_user + .user + .as_ref() + .ok_or_else(|| (StatusCode::UNAUTHORIZED, "")); + + // Get the workspace + let workspace = match Workspace::from_id(&state.app_data, &workspace_id).await { + Ok(ws) => ws, + Err(_) => return "workspace not found".into_response(), + }; + + // Check if user is a member of the workspace + let _workspace_member = workspace + .get_member(&state.app_data, &auth_user.user.unwrap()) .await - .unwrap(); + .map_err(|_| (StatusCode::FORBIDDEN, "")); + + // Check if workspace has access to the connection namespace + let _connection_access = ConnectionAccess::get(&state.app_data, &workspace, &connection_id) + .await + .map_err(|_| (StatusCode::NOT_FOUND, "")); let geoconnector = state - .geospatial_config - .get_connection(&connection.id) + .geo_connections + .get_connection(&connection_id) .await .unwrap(); - let tile = geoconnector.get_tile(z, x, y).await.unwrap(); + let tile = geoconnector + .get_tile(&workspace_id, &source_name, z, x, y) + .await + .unwrap(); - println!("tile"); - println!("{tile:?}"); + //println!("tile"); + //println!("{tile:?}"); Response::builder() .status(StatusCode::OK) diff --git a/gridwalk-backend/src/server.rs b/gridwalk-backend/src/server.rs index 86eb7fb..1cd76af 100644 --- a/gridwalk-backend/src/server.rs +++ b/gridwalk-backend/src/server.rs @@ -51,6 +51,7 @@ pub fn create_app(app_state: AppState) -> Router { ) .route("/create_project", post(create_project)) .route("/upload_layer", post(upload_layer)) + .route("/workspaces/:workspace_id/connections/:connection_id/sources/:source_name/tiles/:z/:x/:y", get(tiles)) .layer(DefaultBodyLimit::disable()) .layer(RequestBodyLimitLayer::new(100 * 1024 * 1024)) .layer(middleware::from_fn_with_state(