Skip to content

Commit

Permalink
Replace RouterPath with NestedPath
Browse files Browse the repository at this point in the history
  • Loading branch information
ackwell committed Aug 30, 2024
1 parent f267ba6 commit 43c5f36
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/http/api1/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use aide::{axum::ApiRouter, openapi, transform::TransformOpenApi};
use axum::{
debug_handler,
extract::{FromRef, State},
extract::{FromRef, NestedPath, State},
response::IntoResponse,
routing::get,
Json, Router,
Expand All @@ -16,7 +16,7 @@ use tower_http::cors::CorsLayer;

use crate::http::{http::HttpState, service::Service};

use super::{asset, extract::RouterPath, read::RowReaderState, search, sheet, version};
use super::{asset, read::RowReaderState, search, sheet, version};

const OPENAPI_JSON_ROUTE: &str = "/openapi.json";

Expand Down Expand Up @@ -136,21 +136,21 @@ struct OpenApiOverrides<'a> {

#[debug_handler]
async fn openapi_json(
RouterPath(router_path): RouterPath,
nested_path: NestedPath,
State(openapi): State<Arc<openapi::OpenApi>>,
) -> impl IntoResponse {
Json(OpenApiOverrides {
base: &*openapi,
servers: &[openapi::Server {
url: router_path,
url: nested_path.as_str().to_string(),
..Default::default()
}],
})
.into_response()
}

#[debug_handler]
async fn scalar(RouterPath(router_path): RouterPath) -> impl IntoResponse {
async fn scalar(nested_path: NestedPath) -> impl IntoResponse {
html! {
(DOCTYPE)
html {
Expand All @@ -160,7 +160,7 @@ async fn scalar(RouterPath(router_path): RouterPath) -> impl IntoResponse {
meta name="viewport" content="width=device-width, initial-scale=1";
}
body {
script id="api-reference" data-url={ (router_path) (OPENAPI_JSON_ROUTE) } {}
script id="api-reference" data-url={ (nested_path.as_str()) (OPENAPI_JSON_ROUTE) } {}
script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference" {}
}
}
Expand Down
23 changes: 2 additions & 21 deletions src/http/api1/extract.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::convert::Infallible;

use aide::OperationIo;
use axum::{
async_trait,
extract::{FromRef, FromRequestParts, OriginalUri},
http::{request::Parts, Uri},
extract::{FromRef, FromRequestParts},
http::request::Parts,
RequestPartsExt,
};
use schemars::JsonSchema;
Expand Down Expand Up @@ -54,23 +52,6 @@ where
}
}

// This cursed garbage courtesy of trying to get the path of the parent router. Fun.
pub struct RouterPath(pub String);

#[async_trait]
impl<S> FromRequestParts<S> for RouterPath {
type Rejection = Infallible;

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
let uri = parts.extract::<Uri>().await?;
let OriginalUri(original_uri) = parts.extract::<OriginalUri>().await?;

let router_path = original_uri.path().strip_suffix(uri.path()).unwrap_or("");

Ok(Self(router_path.into()))
}
}

#[derive(FromRequestParts, OperationIo)]
#[from_request(via(axum::extract::Path), rejection(Error))]
#[aide(input_with = "axum::extract::Path<T>", json_schema)]
Expand Down

0 comments on commit 43c5f36

Please sign in to comment.