Skip to content

Commit

Permalink
initial backend code
Browse files Browse the repository at this point in the history
  • Loading branch information
zerj9 committed Oct 1, 2024
1 parent 087a18d commit b82ba5c
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
15 changes: 15 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

17 changes: 17 additions & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "backend"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1"
axum = { version = "0.7.5", features = ["macros"] }
martin = { git = "https://github.com/enmeshed-analytics/martin.git", features = ["postgres"] }
martin-tile-utils = { git = "https://github.com/enmeshed-analytics/martin.git" }
rustls = { version = "0.23.13", features = ["std"] }
serde = "1.0"
serde_json = "1.0"
tokio = { version = "1.40.0", features = ["full"] }
tower-http = { version = "0.5", features = ["trace"] }
tracing = "0.1"
tracing-subscriber = "0.3"
101 changes: 101 additions & 0 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use anyhow::Result;
use axum::{
extract::{Path, State},
http::{header, StatusCode},
response::{IntoResponse, Response},
routing::get,
Json, Router,
};
use martin::pg::PgConfig;
use martin::{IdResolver, OptBoolObj, Source};
use martin_tile_utils::TileCoord;
use rustls;
use std::collections::HashMap;
use tower_http::trace::{self, TraceLayer};
use tracing::{info, Level};

#[derive(Clone)]
struct AppState {
sources: HashMap<String, Box<dyn Source>>,
}

#[tokio::main]
async fn main() -> Result<()> {
// Initialize tracing
tracing_subscriber::fmt::init();
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.unwrap();

// MARTIN CODE
let mut pg_config = PgConfig {
connection_string: Some("postgresql://admin:password@localhost:5432/gridwalk".to_string()),
auto_publish: OptBoolObj::Bool(true),
..Default::default()
};
pg_config.finalize().unwrap();
let tile_info_sources = pg_config.resolve(IdResolver::default()).await.unwrap();
let mut sources: HashMap<String, Box<dyn Source>> = HashMap::new();

for source in tile_info_sources {
let id = source.get_id().to_string();

// Insert into the HashMap
sources.insert(id, source);
}
let app_state = AppState { sources };

// Build our application with a route
let app = Router::new()
.route("/health", get(health_check))
.route("/tiles/:z/:x/:y", get(tiles)) // Add your handler route
.with_state(app_state)
.layer(
TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
);

// Run our app with hyper
let listener = tokio::net::TcpListener::bind("127.0.0.1:3001").await?;
info!("Server listening on {}", listener.local_addr()?);
axum::serve(listener, app).await?;

Ok(())
}

async fn health_check() -> Json<serde_json::Value> {
Json(serde_json::json!({ "status": "healthy" }))
}

async fn tiles(Path((z, y, x)): Path<(u32, u32, u32)>, State(state): State<AppState>) -> Response {
if let Some(tile_info_source) = state.sources.get("pois") {
let xyz = TileCoord {
x,
y,
z: z.try_into().unwrap(),
};

match tile_info_source.get_tile(xyz, None).await {
Ok(tile_data) => {
// Create a response with the tile data and appropriate headers
(
StatusCode::OK,
[
(header::CONTENT_TYPE, "application/vnd.mapbox-vector-tile"),
(header::ACCESS_CONTROL_ALLOW_ORIGIN, "*"),
],
tile_data,
)
.into_response()
}
Err(_) => (StatusCode::NOT_FOUND, "Tile not found".to_string()).into_response(),
}
} else {
(
StatusCode::NOT_FOUND,
"Tile info source not found".to_string(),
)
.into_response()
}
}

0 comments on commit b82ba5c

Please sign in to comment.