2024-04-11 12:09:39 +00:00
|
|
|
|
2024-04-11 12:17:39 +00:00
|
|
|
mod dem;
|
2024-04-11 18:10:53 +00:00
|
|
|
use std::{borrow::Borrow, collections::HashMap, io::{self, BufRead}, sync::{Arc, RwLock}};
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
use tracing_subscriber::registry::Data;
|
2024-04-11 13:38:46 +00:00
|
|
|
|
2024-04-11 12:09:39 +00:00
|
|
|
|
2024-04-11 13:38:46 +00:00
|
|
|
use axum_macros::debug_handler;
|
|
|
|
use axum::{
|
2024-04-11 18:10:53 +00:00
|
|
|
body::Bytes, extract::{Path, State}, http::StatusCode, routing::{get, post}, Json, Router
|
2024-04-11 13:38:46 +00:00
|
|
|
};
|
2024-04-11 18:10:53 +00:00
|
|
|
use dem::DatasetCache;
|
2024-04-11 13:38:46 +00:00
|
|
|
|
2024-04-11 18:10:53 +00:00
|
|
|
use gdal::Dataset;
|
2024-04-11 13:38:46 +00:00
|
|
|
|
2024-04-11 18:10:53 +00:00
|
|
|
pub type SharedState = Arc<DatasetCache>;
|
|
|
|
|
|
|
|
//#[derive(Default)]
|
|
|
|
//struct AppState {
|
|
|
|
// db: RwLock<HashMap<String, Dataset>>,
|
|
|
|
//}
|
|
|
|
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
2024-04-11 13:38:46 +00:00
|
|
|
async fn main() {
|
|
|
|
// initialize tracing
|
|
|
|
tracing_subscriber::fmt::init();
|
2024-04-11 12:09:39 +00:00
|
|
|
|
2024-04-11 18:10:53 +00:00
|
|
|
|
|
|
|
let data_set_cache = SharedState::default(); //Arc::new(RwLock::new(dem::DatasetCache::new()));
|
|
|
|
|
2024-04-11 13:38:46 +00:00
|
|
|
// build our application with a route
|
|
|
|
let app = Router::new()
|
|
|
|
// `GET /` goes to `root`
|
|
|
|
.route("/", get(root))
|
2024-04-11 18:10:53 +00:00
|
|
|
.route("/elevation/:lat/:lon", get(get_elevation))
|
|
|
|
.with_state(Arc::clone(&data_set_cache));
|
2024-04-11 13:38:46 +00:00
|
|
|
|
|
|
|
// run our app with hyper, listening globally on port 3000
|
|
|
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
|
|
|
axum::serve(listener, app).await.unwrap();
|
|
|
|
}
|
2024-04-11 12:09:39 +00:00
|
|
|
|
2024-04-11 13:38:46 +00:00
|
|
|
// basic handler that responds with a static string
|
|
|
|
async fn root() -> &'static str {
|
|
|
|
"Hello, World!"
|
2024-04-11 12:09:39 +00:00
|
|
|
}
|
2024-04-11 13:38:46 +00:00
|
|
|
|
|
|
|
|
2024-04-11 18:10:53 +00:00
|
|
|
//async fn get_elevation(State(data_set_cache): State<Arc<RwLock<DatasetCache>>>, Path((lat, lon)): Path<(f64, f64)>) -> String{
|
2024-04-11 13:38:46 +00:00
|
|
|
#[debug_handler]
|
2024-04-11 18:10:53 +00:00
|
|
|
async fn get_elevation(State(shared): State<SharedState>, Path((lat, lon)): Path<(f64, f64)>) -> String{
|
|
|
|
|
|
|
|
// let x = AppState::default();
|
|
|
|
// x.db.get("hello");
|
|
|
|
|
|
|
|
|
|
|
|
let ele = dem::elevation_from_coordinates(shared, lat, lon);
|
|
|
|
let myele = ele.await;
|
|
|
|
format!("{lat} {lon} {myele}")
|
2024-04-11 13:38:46 +00:00
|
|
|
}
|
|
|
|
|