Skip to content

Commit

Permalink
improvement: storage_create handler works and has error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sentinel1909 committed Feb 19, 2025
1 parent da03a11 commit ef99da8
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 80 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::borrow::Cow;
#[folder = "../static"]
struct Asset;

// struct type to represent incoming path parameters
#[PathParams]
pub struct GetFilenameParams<'a> {
filename: Cow<'a, str>,
Expand Down
3 changes: 2 additions & 1 deletion app/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ pub fn register(bp: &mut Blueprint) {
.error_handler(f!(crate::routes::index::tera_error2response));
bp.route(GET, "/static/{filename}", f!(self::static_assets::get));
bp.route(GET, "/api/ping", f!(self::ping::get));
bp.route(GET, "/api/storage/create/{dir}", f!(self::storage_create::get));
bp.route(GET, "/api/storage/create", f!(self::storage_create::get))
.error_handler(f!(crate::routes::storage_create::storage_create2response));
}
27 changes: 18 additions & 9 deletions app/src/routes/storage_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

// dependencies

Check warning on line 3 in app/src/routes/storage_create.rs

View workflow job for this annotation

GitHub Actions / Format

Diff in /home/runner/work/pavex-html/pavex-html/app/src/routes/storage_create.rs
use crate::configuration::AppConfig;
use pavex::{request::path::PathParams, response::{body::Json, Response}};
use opendal::Result;
use pavex::{request::query::QueryParams, response::{body::Json, Response}};
use std::borrow::Cow;

#[PathParams]
pub struct DirectoryPathParam {
#[derive(serde::Deserialize)]
pub struct DirParams {

Check warning on line 10 in app/src/routes/storage_create.rs

View workflow job for this annotation

GitHub Actions / Format

Diff in /home/runner/work/pavex-html/pavex-html/app/src/routes/storage_create.rs
pub dir: Cow<'static, str>,
}

Expand All @@ -15,19 +16,27 @@ struct StorageCreateResponse {
message: Cow<'static, str>,
}

// error handler
pub async fn storage_create2response(e: &pavex::Error) -> Response {

Check warning on line 20 in app/src/routes/storage_create.rs

View workflow job for this annotation

GitHub Actions / Format

Diff in /home/runner/work/pavex-html/pavex-html/app/src/routes/storage_create.rs
Response::internal_server_error().set_typed_body(e.to_string())
}

// storage_create get handler function
pub async fn get(config: &AppConfig, params: PathParams<DirectoryPathParam>) -> Response {
pub async fn get(config: &AppConfig, params: &QueryParams<DirParams>) -> Result<Response> {

Check warning on line 25 in app/src/routes/storage_create.rs

View workflow job for this annotation

GitHub Actions / Format

Diff in /home/runner/work/pavex-html/pavex-html/app/src/routes/storage_create.rs

let directory = params.0.dir;
let storage = config.local_storage_config().build().unwrap();
let mut directory = params.0.dir.to_string();
directory.push('/');
let storage = config.local_storage_config().build()?;

storage.create_dir(directory.as_ref()).await.unwrap();
storage.create_dir(&directory).await?;

let response = StorageCreateResponse {
message: Cow::Borrowed("Created"),
message: Cow::Owned(format!("Created new directory named: {}", directory)),
};

let json_response = Json::new(response).expect("Failed to serialize the response body.");

Response::ok().set_typed_body(json_response)
let response = Response::ok().set_typed_body(json_response);

Ok(response)
}
1 change: 1 addition & 0 deletions server_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ app = { version = "0.1.0", path = "../app", default-features = false }
http = { version = "1.2.0", default-features = false }
hyper = { version = "1.6.0", default-features = false }
matchit = { version = "0.8.6", default-features = false }
opendal = { version = "0.51.2", default-features = false }
pavex = { version = "0.1.74", default-features = false }
pavex_tracing = { version = "0.1.74", default-features = false }
tera = { version = "1.20.0", default-features = false }
Expand Down
146 changes: 76 additions & 70 deletions server_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Router {
let mut router = matchit::Router::new();
router.insert("/", 0u32).unwrap();
router.insert("/api/ping", 1u32).unwrap();
router.insert("/api/storage/create/{dir}", 2u32).unwrap();
router.insert("/api/storage/create", 2u32).unwrap();
router.insert("/static/{filename}", 3u32).unwrap();
router
}
Expand Down Expand Up @@ -158,13 +158,12 @@ impl Router {
match &request_head.method {
&pavex::http::Method::GET => {
let matched_route_template = pavex::request::path::MatchedPathPattern::new(
"/api/storage/create/{dir}",
"/api/storage/create",
);
route_3::entrypoint(
url_params,
&request_head,
matched_route_template,
&state.app_config,
matched_route_template,
&request_head,
)
.await
}
Expand All @@ -174,7 +173,7 @@ impl Router {
])
.into();
let matched_route_template = pavex::request::path::MatchedPathPattern::new(
"/api/storage/create/{dir}",
"/api/storage/create",
);
route_4::entrypoint(
&request_head,
Expand Down Expand Up @@ -577,92 +576,101 @@ pub mod route_2 {
}
}
pub mod route_3 {
pub async fn entrypoint<'a, 'b, 'c, 'd>(
s_0: pavex::request::path::RawPathParams<'a, 'b>,
s_1: &'c pavex::request::RequestHead,
s_2: pavex::request::path::MatchedPathPattern,
s_3: &'d app::configuration::AppConfig,
pub async fn entrypoint<'a, 'b>(
s_0: &'a app::configuration::AppConfig,
s_1: pavex::request::path::MatchedPathPattern,
s_2: &'b pavex::request::RequestHead,
) -> pavex::response::Response {
let response = wrapping_0(s_0, s_1, s_2, s_3).await;
let response = wrapping_0(s_0, s_1, s_2).await;
response
}
async fn stage_1<'a, 'b, 'c, 'd>(
s_0: pavex::request::path::RawPathParams<'a, 'b>,
s_1: &'c app::configuration::AppConfig,
s_2: &'d pavex::request::RequestHead,
s_3: pavex::request::path::MatchedPathPattern,
async fn stage_1<'a, 'b>(
s_0: &'a pavex::request::RequestHead,
s_1: &'b app::configuration::AppConfig,
s_2: pavex::request::path::MatchedPathPattern,
) -> pavex::response::Response {
let response = wrapping_1(s_0, s_2, s_3, s_1).await;
let response = wrapping_1(s_1, s_2, s_0).await;
response
}
async fn stage_2<'a, 'b, 'c, 'd>(
async fn stage_2<'a, 'b, 'c>(
s_0: &'a pavex_tracing::RootSpan,
s_1: pavex::request::path::RawPathParams<'b, 'c>,
s_2: &'d app::configuration::AppConfig,
s_1: &'b pavex::request::RequestHead,
s_2: &'c app::configuration::AppConfig,
) -> pavex::response::Response {
let response = handler(s_0, s_1, s_2).await;
let response = handler(s_1, s_0, s_2).await;
let response = post_processing_0(response, s_0).await;
response
}
async fn wrapping_0(
v0: pavex::request::path::RawPathParams<'_, '_>,
v1: &pavex::request::RequestHead,
v2: pavex::request::path::MatchedPathPattern,
v3: &app::configuration::AppConfig,
v0: &app::configuration::AppConfig,
v1: pavex::request::path::MatchedPathPattern,
v2: &pavex::request::RequestHead,
) -> pavex::response::Response {
let v4 = crate::route_3::Next0 {
s_0: v0,
s_1: v3,
let v3 = crate::route_3::Next0 {
s_0: v2,
s_1: v0,
s_2: v1,
s_3: v2,
next: stage_1,
};
let v5 = pavex::middleware::Next::new(v4);
let v6 = pavex::middleware::wrap_noop(v5).await;
<pavex::response::Response as pavex::response::IntoResponse>::into_response(v6)
let v4 = pavex::middleware::Next::new(v3);
let v5 = pavex::middleware::wrap_noop(v4).await;
<pavex::response::Response as pavex::response::IntoResponse>::into_response(v5)
}
async fn wrapping_1(
v0: pavex::request::path::RawPathParams<'_, '_>,
v1: &pavex::request::RequestHead,
v2: pavex::request::path::MatchedPathPattern,
v3: &app::configuration::AppConfig,
v0: &app::configuration::AppConfig,
v1: pavex::request::path::MatchedPathPattern,
v2: &pavex::request::RequestHead,
) -> pavex::response::Response {
let v4 = pavex::telemetry::ServerRequestId::generate();
let v5 = app::telemetry::root_span(v1, v2, v4);
let v6 = crate::route_3::Next1 {
s_0: &v5,
s_1: v0,
s_2: v3,
let v3 = pavex::telemetry::ServerRequestId::generate();
let v4 = app::telemetry::root_span(v2, v1, v3);
let v5 = crate::route_3::Next1 {
s_0: &v4,
s_1: v2,
s_2: v0,
next: stage_2,
};
let v7 = pavex::middleware::Next::new(v6);
let v8 = <pavex_tracing::RootSpan as core::clone::Clone>::clone(&v5);
let v9 = pavex_tracing::logger(v8, v7).await;
<pavex::response::Response as pavex::response::IntoResponse>::into_response(v9)
let v6 = pavex::middleware::Next::new(v5);
let v7 = <pavex_tracing::RootSpan as core::clone::Clone>::clone(&v4);
let v8 = pavex_tracing::logger(v7, v6).await;
<pavex::response::Response as pavex::response::IntoResponse>::into_response(v8)
}
async fn handler(
v0: &pavex_tracing::RootSpan,
v1: pavex::request::path::RawPathParams<'_, '_>,
v0: &pavex::request::RequestHead,
v1: &pavex_tracing::RootSpan,
v2: &app::configuration::AppConfig,
) -> pavex::response::Response {
let v3 = pavex::request::path::PathParams::extract(v1);
let v3 = pavex::request::query::QueryParams::extract(v0);
let v4 = match v3 {
Ok(ok) => ok,
Err(v4) => {
return {
let v5 = pavex::request::path::errors::ExtractPathParamsError::into_response(
let v5 = pavex::request::query::errors::ExtractQueryParamsError::into_response(
&v4,
);
let v6 = pavex::Error::new(v4);
app::telemetry::error_logger(&v6, v0).await;
app::telemetry::error_logger(&v6, v1).await;
<pavex::response::Response as pavex::response::IntoResponse>::into_response(
v5,
)
};
}
};
let v5 = app::routes::storage_create::get(v2, v4).await;
<pavex::response::Response as pavex::response::IntoResponse>::into_response(v5)
let v5 = app::routes::storage_create::get(v2, &v4).await;
let v6 = match v5 {
Ok(ok) => ok,
Err(v6) => {
return {
let v7 = pavex::Error::new(v6);
let v8 = app::routes::storage_create::storage_create2response(&v7)
.await;
app::telemetry::error_logger(&v7, v1).await;
<pavex::response::Response as pavex::response::IntoResponse>::into_response(
v8,
)
};
}
};
<pavex::response::Response as pavex::response::IntoResponse>::into_response(v6)
}
async fn post_processing_0(
v0: pavex::response::Response,
Expand All @@ -671,45 +679,43 @@ pub mod route_3 {
let v2 = app::telemetry::response_logger(v0, v1).await;
<pavex::response::Response as pavex::response::IntoResponse>::into_response(v2)
}
struct Next0<'a, 'b, 'c, 'd, T>
struct Next0<'a, 'b, T>
where
T: std::future::Future<Output = pavex::response::Response>,
{
s_0: pavex::request::path::RawPathParams<'a, 'b>,
s_1: &'c app::configuration::AppConfig,
s_2: &'d pavex::request::RequestHead,
s_3: pavex::request::path::MatchedPathPattern,
s_0: &'a pavex::request::RequestHead,
s_1: &'b app::configuration::AppConfig,
s_2: pavex::request::path::MatchedPathPattern,
next: fn(
pavex::request::path::RawPathParams<'a, 'b>,
&'c app::configuration::AppConfig,
&'d pavex::request::RequestHead,
&'a pavex::request::RequestHead,
&'b app::configuration::AppConfig,
pavex::request::path::MatchedPathPattern,
) -> T,
}
impl<'a, 'b, 'c, 'd, T> std::future::IntoFuture for Next0<'a, 'b, 'c, 'd, T>
impl<'a, 'b, T> std::future::IntoFuture for Next0<'a, 'b, T>
where
T: std::future::Future<Output = pavex::response::Response>,
{
type Output = pavex::response::Response;
type IntoFuture = T;
fn into_future(self) -> Self::IntoFuture {
(self.next)(self.s_0, self.s_1, self.s_2, self.s_3)
(self.next)(self.s_0, self.s_1, self.s_2)
}
}
struct Next1<'a, 'b, 'c, 'd, T>
struct Next1<'a, 'b, 'c, T>
where
T: std::future::Future<Output = pavex::response::Response>,
{
s_0: &'a pavex_tracing::RootSpan,
s_1: pavex::request::path::RawPathParams<'b, 'c>,
s_2: &'d app::configuration::AppConfig,
s_1: &'b pavex::request::RequestHead,
s_2: &'c app::configuration::AppConfig,
next: fn(
&'a pavex_tracing::RootSpan,
pavex::request::path::RawPathParams<'b, 'c>,
&'d app::configuration::AppConfig,
&'b pavex::request::RequestHead,
&'c app::configuration::AppConfig,
) -> T,
}
impl<'a, 'b, 'c, 'd, T> std::future::IntoFuture for Next1<'a, 'b, 'c, 'd, T>
impl<'a, 'b, 'c, T> std::future::IntoFuture for Next1<'a, 'b, 'c, T>
where
T: std::future::Future<Output = pavex::response::Response>,
{
Expand Down

0 comments on commit ef99da8

Please sign in to comment.