api-server/src/main.rs

63 lines
1.5 KiB
Rust
Raw Normal View History

mod dem;
use std::sync::Arc;
use axum_macros::debug_handler;
use axum::{
extract::{Path, State}, http::StatusCode, routing::{get}, Router
};
use dem::{MyDataset};
use moka::future::Cache;
//#[derive(Default)]
//struct AppState {
// db: RwLock<HashMap<String, Dataset>>,
//}
type DSC = Cache<String, Arc<MyDataset>>;
#[tokio::main(flavor = "current_thread")]
async fn main() {
// initialize tracing
tracing_subscriber::fmt::init();
// 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;
// build our application with a route
let app = Router::new()
// `GET /` goes to `root`
.route("/", get(root))
.route("/elevation/:lat/:lon", get(get_elevation))
.with_state(cache);
// 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();
}
// basic handler that responds with a static string
async fn root() -> &'static str {
"Hello, World!"
}
#[debug_handler]
async fn get_elevation(State(shared): State<DSC>, Path((lat, lon)): Path<(f64, f64)>) -> String{
let ele = dem::elevation_from_coordinates(shared, lat, lon);
let myele = ele.await;
format!("{lat} {lon} {myele}")
}