2024-04-11 12:17:39 +00:00
|
|
|
mod dem;
|
2024-04-11 13:38:46 +00:00
|
|
|
|
|
|
|
use axum::{
|
2024-04-21 12:05:46 +00:00
|
|
|
extract::{Extension, Path, Query, State},
|
2024-04-21 13:18:24 +00:00
|
|
|
http::StatusCode,
|
2024-04-21 12:05:46 +00:00
|
|
|
response::{IntoResponse, Response},
|
|
|
|
routing::get,
|
|
|
|
Json, Router,
|
2024-04-11 13:38:46 +00:00
|
|
|
};
|
2024-04-15 21:17:14 +00:00
|
|
|
use axum_macros::debug_handler;
|
2024-04-21 13:18:24 +00:00
|
|
|
use std::env;
|
2024-04-15 21:17:14 +00:00
|
|
|
|
2024-04-30 19:11:41 +00:00
|
|
|
use serde::ser::SerializeSeq;
|
2024-04-19 11:28:40 +00:00
|
|
|
use serde::{Deserialize, Deserializer, Serialize};
|
2024-04-21 13:18:24 +00:00
|
|
|
use serde_qs::axum::{QsQueryConfig, QsQueryRejection};
|
2024-04-19 11:28:40 +00:00
|
|
|
use tower_http::trace::TraceLayer;
|
2024-04-15 22:22:06 +00:00
|
|
|
use tower_http::{
|
|
|
|
services::ServeDir,
|
2024-04-21 13:18:24 +00:00
|
|
|
trace::{self},
|
2024-04-15 22:22:06 +00:00
|
|
|
};
|
2024-04-21 13:18:24 +00:00
|
|
|
use tracing::{error, info, Level};
|
2024-04-11 13:38:46 +00:00
|
|
|
|
2024-04-19 11:28:40 +00:00
|
|
|
use dem::DatasetRepository;
|
2024-04-11 18:10:53 +00:00
|
|
|
|
2024-04-15 21:17:14 +00:00
|
|
|
const DEFAULT_DATA_DIR: &str = "/data";
|
|
|
|
const DEFAULT_PORT: &str = "3000";
|
2024-04-11 18:34:55 +00:00
|
|
|
|
2024-04-19 11:28:40 +00:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct Opts {
|
|
|
|
#[serde(default, deserialize_with = "empty_string_as_none")]
|
|
|
|
json: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
2024-04-21 12:05:46 +00:00
|
|
|
struct JsParams {
|
2024-04-19 11:28:40 +00:00
|
|
|
#[serde(default, deserialize_with = "deserialize_array")]
|
|
|
|
pts: Vec<(f64, f64)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Debug)]
|
|
|
|
struct JsResult {
|
2024-04-21 12:05:46 +00:00
|
|
|
#[serde(serialize_with = "serialize_vec_round")]
|
2024-04-19 11:28:40 +00:00
|
|
|
elevations: Vec<Option<f64>>,
|
|
|
|
}
|
|
|
|
|
2024-04-21 12:05:46 +00:00
|
|
|
fn serialize_vec_round<S>(v: &Vec<Option<f64>>, s: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
|
|
|
let mut sv = s.serialize_seq(Some(v.len()))?;
|
|
|
|
for e in v {
|
|
|
|
match e {
|
|
|
|
None => sv.serialize_element(&e)?,
|
|
|
|
Some(x) => {
|
|
|
|
// Round the f64 to 1 decimal place. This is ugly as shit.
|
|
|
|
let fmt = format!("{:.1}", x);
|
|
|
|
let xx = fmt.parse::<f64>().unwrap();
|
|
|
|
sv.serialize_element(&xx)?;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
sv.end()
|
|
|
|
}
|
|
|
|
|
2024-04-19 11:28:40 +00:00
|
|
|
fn deserialize_array<'de, D>(deserializer: D) -> Result<Vec<(f64, f64)>, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let s = String::deserialize(deserializer)?;
|
|
|
|
let result: Result<Vec<(f64, f64)>, serde_json::Error> = serde_json::from_str(&s);
|
|
|
|
match result {
|
|
|
|
Ok(x) => Ok(x),
|
2024-04-21 12:05:46 +00:00
|
|
|
Err(e) => Err(serde::de::Error::custom(
|
|
|
|
"Invalid array: ".to_string() + &e.to_string(),
|
|
|
|
)),
|
2024-04-19 11:28:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn empty_string_as_none<'de, D>(de: D) -> Result<bool, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let opt = Option::<String>::deserialize(de)?;
|
|
|
|
match opt.as_deref() {
|
|
|
|
None => Ok(false),
|
|
|
|
Some("") => Ok(true),
|
|
|
|
Some(x) => Ok(x != "false"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 21:17:14 +00:00
|
|
|
#[tokio::main]
|
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-15 21:17:14 +00:00
|
|
|
let config = load_config().unwrap();
|
|
|
|
let cache = DatasetRepository::new(config.basedir);
|
|
|
|
|
|
|
|
let serve_dir = ServeDir::new("assets");
|
2024-04-11 18:10:53 +00:00
|
|
|
|
2024-04-19 11:28:40 +00:00
|
|
|
let app = Router::<DatasetRepository>::new()
|
2024-04-21 12:11:37 +00:00
|
|
|
.route("/v1/elevation/:lat/:lon", get(get_elevation))
|
|
|
|
.route("/v1/elevation", get(get_elevation_js))
|
2024-04-15 21:17:14 +00:00
|
|
|
.nest_service("/", serve_dir)
|
2024-04-15 21:44:57 +00:00
|
|
|
.with_state(cache)
|
2024-04-15 22:22:06 +00:00
|
|
|
.layer(
|
2024-04-15 21:44:57 +00:00
|
|
|
TraceLayer::new_for_http()
|
2024-04-21 13:17:51 +00:00
|
|
|
.make_span_with(trace::DefaultMakeSpan::new().level(Level::DEBUG))
|
|
|
|
.on_response(trace::DefaultOnResponse::new().level(Level::DEBUG)),
|
2024-04-19 11:28:40 +00:00
|
|
|
)
|
2024-04-21 12:05:46 +00:00
|
|
|
.layer(Extension(QsQueryConfig::new(5, false).error_handler(
|
|
|
|
|err| {
|
|
|
|
QsQueryRejection::new(
|
|
|
|
format!("Get fucked: {}", err),
|
|
|
|
StatusCode::UNPROCESSABLE_ENTITY,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)));
|
2024-04-11 13:38:46 +00:00
|
|
|
|
2024-04-15 21:17:14 +00:00
|
|
|
let host = format!("[::]:{}", config.port);
|
2024-04-11 22:33:10 +00:00
|
|
|
info!("Will start server on {host}");
|
|
|
|
|
|
|
|
let listener = tokio::net::TcpListener::bind(host).await.unwrap();
|
|
|
|
axum::serve(listener, app).await.unwrap();
|
2024-04-11 12:09:39 +00:00
|
|
|
}
|
2024-04-11 13:38:46 +00:00
|
|
|
|
|
|
|
#[debug_handler]
|
2024-04-15 22:22:06 +00:00
|
|
|
async fn get_elevation(
|
|
|
|
State(dsr): State<DatasetRepository>,
|
2024-04-19 11:28:40 +00:00
|
|
|
query_opts: Query<Opts>,
|
2024-04-15 22:22:06 +00:00
|
|
|
Path((lat, lon)): Path<(f64, f64)>,
|
2024-04-19 11:28:40 +00:00
|
|
|
) -> Response {
|
|
|
|
println!("lat: {}, lon: {}", lat, lon);
|
|
|
|
println!("query_opts: {:?}", query_opts);
|
|
|
|
|
|
|
|
let ele;
|
|
|
|
match dem::elevation_from_coordinates(&dsr, lat, lon).await {
|
2024-04-21 12:05:46 +00:00
|
|
|
Ok(x) => match x {
|
|
|
|
Some(el) => ele = el,
|
2024-04-21 13:17:51 +00:00
|
|
|
None => ele = 0.,
|
2024-04-21 12:05:46 +00:00
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
return e.to_string().into_response();
|
2024-04-19 11:28:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct Ele {
|
|
|
|
elevation: f64,
|
2024-04-30 19:11:41 +00:00
|
|
|
}
|
2024-04-19 11:28:40 +00:00
|
|
|
|
|
|
|
if query_opts.json {
|
|
|
|
let r = Ele { elevation: ele };
|
2024-04-21 13:18:24 +00:00
|
|
|
Json(r).into_response()
|
2024-04-19 11:28:40 +00:00
|
|
|
} else {
|
2024-04-21 13:18:24 +00:00
|
|
|
format!("{}", ele).into_response()
|
2024-04-15 22:22:06 +00:00
|
|
|
}
|
2024-04-11 13:38:46 +00:00
|
|
|
}
|
|
|
|
|
2024-04-19 11:28:40 +00:00
|
|
|
#[debug_handler]
|
|
|
|
async fn get_elevation_js(
|
|
|
|
State(dsr): State<DatasetRepository>,
|
|
|
|
Query(params): Query<JsParams>,
|
|
|
|
) -> Response {
|
2024-04-21 12:05:46 +00:00
|
|
|
let mut response = JsResult { elevations: vec![] };
|
2024-04-19 11:28:40 +00:00
|
|
|
for pt in params.pts {
|
|
|
|
let ele = dem::elevation_from_coordinates(&dsr, pt.0, pt.1).await;
|
2024-04-21 12:05:46 +00:00
|
|
|
match ele {
|
|
|
|
Ok(x) => response.elevations.push(x),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error: {e}");
|
|
|
|
response.elevations.push(None);
|
|
|
|
}
|
|
|
|
}
|
2024-04-19 11:28:40 +00:00
|
|
|
}
|
|
|
|
Json(response).into_response()
|
|
|
|
}
|
|
|
|
|
2024-04-15 21:17:14 +00:00
|
|
|
fn load_config() -> Result<Config, env::VarError> {
|
|
|
|
Ok(Config {
|
|
|
|
basedir: env::var("DEM_LOCATION").unwrap_or_else(|_| DEFAULT_DATA_DIR.to_string()),
|
|
|
|
port: env::var("HTTP_PORT").unwrap_or_else(|_| DEFAULT_PORT.to_string()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
struct Config {
|
|
|
|
basedir: String,
|
|
|
|
port: String,
|
2024-04-15 21:44:57 +00:00
|
|
|
}
|