From a46121adc7aa1fd8e9e954952ff2fdd10ccaf2b8 Mon Sep 17 00:00:00 2001 From: NexVeridian Date: Mon, 17 Jul 2023 21:19:29 +0000 Subject: [PATCH] 1.1.1 --- Cargo.toml | 7 ++++--- src/main.rs | 21 ++++++++++++++++++--- src/routes.rs | 4 ++-- src/routes/polars_utils.rs | 10 +++++----- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 46ee4f6..4cd37b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,9 +16,10 @@ axum = "0.6" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" tokio = { version = "1.26", features = ["full"] } -aide = { version = "0.10", features = ["redoc", "axum"] } +aide = { version = "0.11", features = ["redoc", "axum"] } schemars = { version = "0.8", features = ["chrono"] } chrono = { version = "0.4", features = ["serde"] } glob = { version = "0.3" } -strum_macros = "0.24" -tower = { version = "0.4.12", features = ["limit", "buffer"] } +strum_macros = "0.25" +tower = { version = "0.4", features = ["limit", "buffer"] } +tower-http = { version = "0.4", features = ["cors"] } diff --git a/src/main.rs b/src/main.rs index 9713356..8d6f471 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) -> 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 { diff --git a/src/routes.rs b/src/routes.rs index 5fc5c56..e5565ce 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -10,7 +10,7 @@ pub async fn arkvc_holdings(date_range: Query) -> 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()) } diff --git a/src/routes/polars_utils.rs b/src/routes/polars_utils.rs index aca0d91..e629e7a 100644 --- a/src/routes/polars_utils.rs +++ b/src/routes/polars_utils.rs @@ -33,7 +33,7 @@ pub async fn filter_date_range( df: DataFrame, date_range: Query, ) -> Result> { - 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();