This commit is contained in:
Elijah McMorris 2023-07-17 21:19:29 +00:00
parent 4306b99634
commit a46121adc7
Signed by: NexVeridian
SSH key fingerprint: SHA256:bsA1SKZxuEcEVHAy3gY1HUeM5ykRJl0U0kQHQn0hMg8
4 changed files with 29 additions and 13 deletions

View file

@ -7,17 +7,22 @@ use aide::{
redoc::Redoc,
transform::TransformOperation,
};
use axum::{error_handling::HandleErrorLayer, http::StatusCode, BoxError, Extension, Json};
use axum::{
error_handling::HandleErrorLayer,
http::{Method, StatusCode},
BoxError, Extension, Json,
};
use std::{net::SocketAddr, time::Duration};
use tower::{buffer::BufferLayer, limit::RateLimitLayer, ServiceBuilder};
use tower_http::cors::{Any, CorsLayer};
mod routes;
async fn serve_api(Extension(api): Extension<OpenApi>) -> impl IntoApiResponse {
return Json(api);
Json(api)
}
fn description_date<'t>(op: TransformOperation<'t>) -> TransformOperation<'t> {
fn description_date(op: TransformOperation) -> TransformOperation {
op.parameter_untyped("start", |p| {
p.description("Start date range - Inclusive >= - ISO 8601")
})
@ -40,6 +45,14 @@ async fn main() {
.layer(RateLimitLayer::new(req_per_sec, Duration::from_secs(1)))
};
let cors = || {
ServiceBuilder::new().layer(
CorsLayer::new()
.allow_methods(Method::GET)
.allow_origin(Any),
)
};
let app = ApiRouter::new()
.route("/", Redoc::new("/api.json").axum_route())
.api_route(
@ -50,6 +63,7 @@ async fn main() {
}),
)
.layer(rate_limit(5))
.layer(cors())
.api_route(
"/ark_holdings",
get_with(routes::ark_holdings, |mut o| {
@ -58,6 +72,7 @@ async fn main() {
}),
)
.layer(rate_limit(20))
.layer(cors())
.route("/api.json", get(serve_api));
let mut api = OpenApi {

View file

@ -10,7 +10,7 @@ pub async fn arkvc_holdings(date_range: Query<polars_utils::DateRange>) -> impl
.await
.unwrap();
return axum::Json(polars_utils::to_json(filter_df).await.unwrap());
axum::Json(polars_utils::to_json(filter_df).await.unwrap())
}
pub async fn ark_holdings(
@ -25,5 +25,5 @@ pub async fn ark_holdings(
.await
.unwrap();
return axum::Json(polars_utils::to_json(filter_df).await.unwrap());
axum::Json(polars_utils::to_json(filter_df).await.unwrap())
}

View file

@ -33,7 +33,7 @@ pub async fn filter_date_range(
df: DataFrame,
date_range: Query<DateRange>,
) -> Result<DataFrame, Box<dyn Error>> {
if date_range.start.or(date_range.end) == None {
if date_range.start.or(date_range.end).is_none() {
return Ok(df);
}
@ -43,10 +43,10 @@ pub async fn filter_date_range(
.map(|x| {
let date = x.unwrap();
match (date_range.start, date_range.end) {
(Some(start), Some(end)) => return date >= start && date <= end,
(Some(start), _) => return date >= start,
(_, Some(end)) => return date <= end,
_ => return false,
(Some(start), Some(end)) => date >= start && date <= end,
(Some(start), _) => date >= start,
(_, Some(end)) => date <= end,
_ => false,
}
})
.collect();