api-server/src/main.rs

48 lines
998 B
Rust
Raw Normal View History

mod dem;
use std::env;
use axum_macros::debug_handler;
use axum::{
extract::{Path, State}, routing::{get}, Router
};
use dem::{DSC, new_cache};
use tracing::info;
#[tokio::main(flavor = "current_thread")]
async fn main() {
// initialize tracing
tracing_subscriber::fmt::init();
let cache = new_cache(1000);
// build our application with a route
let app = Router::new()
.route("/elevation/:lat/:lon", get(get_elevation))
.with_state(cache);
let port = env::var("HTTP_PORT").unwrap_or("3000".to_string());
let host = format!("[::]:{port}");
info!("Will start server on {host}");
let listener = tokio::net::TcpListener::bind(host).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
#[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!("{myele}")
}