api-server/src/main.rs

62 lines
1.7 KiB
Rust
Raw Normal View History

mod dem;
use std::{borrow::Borrow, collections::HashMap, io::{self, BufRead}, sync::{Arc, RwLock}};
use tokio::sync::Mutex;
use tracing_subscriber::registry::Data;
use axum_macros::debug_handler;
use axum::{
body::Bytes, extract::{Path, State}, http::StatusCode, routing::{get, post}, Json, Router
};
use dem::DatasetCache;
use gdal::Dataset;
pub type SharedState = Arc<DatasetCache>;
//#[derive(Default)]
//struct AppState {
// db: RwLock<HashMap<String, Dataset>>,
//}
#[tokio::main(flavor = "current_thread")]
async fn main() {
// initialize tracing
tracing_subscriber::fmt::init();
let data_set_cache = SharedState::default(); //Arc::new(RwLock::new(dem::DatasetCache::new()));
// 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(Arc::clone(&data_set_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!"
}
//async fn get_elevation(State(data_set_cache): State<Arc<RwLock<DatasetCache>>>, Path((lat, lon)): Path<(f64, f64)>) -> String{
#[debug_handler]
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}")
}