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

@ -16,9 +16,10 @@ axum = "0.6"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
tokio = { version = "1.26", features = ["full"] } 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"] } schemars = { version = "0.8", features = ["chrono"] }
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
glob = { version = "0.3" } glob = { version = "0.3" }
strum_macros = "0.24" strum_macros = "0.25"
tower = { version = "0.4.12", features = ["limit", "buffer"] } tower = { version = "0.4", features = ["limit", "buffer"] }
tower-http = { version = "0.4", features = ["cors"] }

View file

@ -7,17 +7,22 @@ use aide::{
redoc::Redoc, redoc::Redoc,
transform::TransformOperation, 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 std::{net::SocketAddr, time::Duration};
use tower::{buffer::BufferLayer, limit::RateLimitLayer, ServiceBuilder}; use tower::{buffer::BufferLayer, limit::RateLimitLayer, ServiceBuilder};
use tower_http::cors::{Any, CorsLayer};
mod routes; mod routes;
async fn serve_api(Extension(api): Extension<OpenApi>) -> impl IntoApiResponse { 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| { op.parameter_untyped("start", |p| {
p.description("Start date range - Inclusive >= - ISO 8601") 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))) .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() let app = ApiRouter::new()
.route("/", Redoc::new("/api.json").axum_route()) .route("/", Redoc::new("/api.json").axum_route())
.api_route( .api_route(
@ -50,6 +63,7 @@ async fn main() {
}), }),
) )
.layer(rate_limit(5)) .layer(rate_limit(5))
.layer(cors())
.api_route( .api_route(
"/ark_holdings", "/ark_holdings",
get_with(routes::ark_holdings, |mut o| { get_with(routes::ark_holdings, |mut o| {
@ -58,6 +72,7 @@ async fn main() {
}), }),
) )
.layer(rate_limit(20)) .layer(rate_limit(20))
.layer(cors())
.route("/api.json", get(serve_api)); .route("/api.json", get(serve_api));
let mut api = OpenApi { let mut api = OpenApi {

View file

@ -10,7 +10,7 @@ pub async fn arkvc_holdings(date_range: Query<polars_utils::DateRange>) -> impl
.await .await
.unwrap(); .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( pub async fn ark_holdings(
@ -25,5 +25,5 @@ pub async fn ark_holdings(
.await .await
.unwrap(); .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, df: DataFrame,
date_range: Query<DateRange>, date_range: Query<DateRange>,
) -> Result<DataFrame, Box<dyn Error>> { ) -> 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); return Ok(df);
} }
@ -43,10 +43,10 @@ pub async fn filter_date_range(
.map(|x| { .map(|x| {
let date = x.unwrap(); let date = x.unwrap();
match (date_range.start, date_range.end) { match (date_range.start, date_range.end) {
(Some(start), Some(end)) => return date >= start && date <= end, (Some(start), Some(end)) => date >= start && date <= end,
(Some(start), _) => return date >= start, (Some(start), _) => date >= start,
(_, Some(end)) => return date <= end, (_, Some(end)) => date <= end,
_ => return false, _ => false,
} }
}) })
.collect(); .collect();