2024-04-11 12:09:39 +00:00
|
|
|
|
2024-04-11 12:17:39 +00:00
|
|
|
mod dem;
|
2024-04-11 13:38:46 +00:00
|
|
|
|
2024-04-11 12:09:39 +00:00
|
|
|
|
2024-04-11 18:50:14 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-04-11 13:38:46 +00:00
|
|
|
use axum_macros::debug_handler;
|
|
|
|
use axum::{
|
2024-04-11 18:50:14 +00:00
|
|
|
extract::{Path, State}, http::StatusCode, routing::{get}, Router
|
2024-04-11 13:38:46 +00:00
|
|
|
};
|
2024-04-11 18:50:14 +00:00
|
|
|
use dem::{MyDataset};
|
2024-04-11 13:38:46 +00:00
|
|
|
|
2024-04-11 18:34:55 +00:00
|
|
|
use moka::future::Cache;
|
2024-04-11 13:38:46 +00:00
|
|
|
|
2024-04-11 18:10:53 +00:00
|
|
|
|
|
|
|
//#[derive(Default)]
|
|
|
|
//struct AppState {
|
|
|
|
// db: RwLock<HashMap<String, Dataset>>,
|
|
|
|
//}
|
|
|
|
|
2024-04-11 18:34:55 +00:00
|
|
|
type DSC = Cache<String, Arc<MyDataset>>;
|
|
|
|
|
2024-04-11 18:10:53 +00:00
|
|
|
#[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
|
|
|
|
2024-04-11 18:34:55 +00:00
|
|
|
// Evict based on the number of entries in the cache.
|
|
|
|
let cache = Cache::builder()
|
|
|
|
// Up to 10,000 entries.
|
|
|
|
.max_capacity(10_000)
|
|
|
|
// Create the cache.
|
|
|
|
.build();
|
|
|
|
//cache.insert("hello".to_string(), Arc::new(dem::MyDataset{ds: Dataset::open("oueou").unwrap()})).await;
|
2024-04-11 18:10:53 +00:00
|
|
|
|
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))
|
2024-04-11 18:34:55 +00:00
|
|
|
.with_state(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
|
|
|
|
|
|
|
|
|
|
|
#[debug_handler]
|
2024-04-11 18:34:55 +00:00
|
|
|
async fn get_elevation(State(shared): State<DSC>, Path((lat, lon)): Path<(f64, f64)>) -> String{
|
2024-04-11 18:10:53 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|