Skip to content

Commit

Permalink
Merge branch 'main' into perf/grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertshelley authored Jul 9, 2024
2 parents 9029147 + c8fec85 commit cb4e873
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ homepage = "https://github.com/hubertshelley/silent"
license = "Apache-2.0"
readme = "./readme.md"
repository = "https://github.com/hubertshelley/silent"
version = "1.3.2"
version = "1.3.4"
7 changes: 7 additions & 0 deletions examples/log_local_time/Cargo.lock

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

9 changes: 9 additions & 0 deletions examples/log_local_time/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "example-log-local-time"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
silent = { path = "../../silent" }
10 changes: 10 additions & 0 deletions examples/log_local_time/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use silent::prelude::*;

fn main() {
logger::fmt()
.with_timer(logger::fmt::time::ChronoLocal::rfc_3339())
.with_max_level(Level::INFO)
.init();
let route = Route::new("").get(|_req| async { Ok("hello world") });
Server::new().run(route);
}
2 changes: 1 addition & 1 deletion silent/src/core/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl FilePart {
}
/// Save the file to a new location.
#[inline]
pub fn save(&mut self, path: String) -> Result<u64, SilentError> {
pub fn save(&self, path: String) -> Result<u64, SilentError> {
std::fs::copy(self.path(), Path::new(&path)).map_err(|e| SilentError::BusinessError {
code: StatusCode::INTERNAL_SERVER_ERROR,
msg: format!("Failed to save file: {}", e),
Expand Down
27 changes: 27 additions & 0 deletions silent/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,33 @@ pub enum SilentError {

pub type SilentResult<T> = Result<T, SilentError>;

impl From<(StatusCode, String)> for SilentError {
fn from(value: (StatusCode, String)) -> Self {
Self::business_error(value.0, value.1)
}
}

impl From<(u16, String)> for SilentError {
fn from(value: (u16, String)) -> Self {
Self::business_error(
StatusCode::from_u16(value.0).expect("invalid status code"),
value.1,
)
}
}

impl From<String> for SilentError {
fn from(value: String) -> Self {
Self::business_error(StatusCode::INTERNAL_SERVER_ERROR, value)
}
}

impl From<BoxedError> for SilentError {
fn from(value: BoxedError) -> Self {
Self::business_error(StatusCode::INTERNAL_SERVER_ERROR, value.to_string())
}
}

impl SilentError {
pub fn business_error_obj<S>(code: StatusCode, msg: S) -> Self
where
Expand Down
2 changes: 1 addition & 1 deletion silent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub mod prelude {
#[cfg(feature = "upgrade")]
pub use crate::route::handler_append::WSHandlerAppend;
pub use crate::route::handler_append::{HandlerAppend, HandlerGetter};
pub use crate::route::{Route, RouteService, RouterAdapt};
pub use crate::route::{RootRoute, Route, RouteService, RouterAdapt};
#[cfg(feature = "scheduler")]
pub use crate::scheduler::Task;
#[cfg(feature = "security")]
Expand Down
12 changes: 12 additions & 0 deletions silent/src/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Route {
pub handler: HashMap<Method, Arc<dyn Handler>>,
pub children: Vec<Route>,
pub middlewares: Vec<Arc<dyn MiddleWareHandler>>,
pub root_middlewares: Vec<Arc<dyn MiddleWareHandler>>,
special_match: bool,
create_path: String,
}
Expand Down Expand Up @@ -80,6 +81,7 @@ impl Route {
handler: HashMap::new(),
children: Vec::new(),
middlewares: Vec::new(),
root_middlewares: Vec::new(),
special_match: first_path.starts_with('<') && first_path.ends_with('>'),
create_path: path.to_string(),
};
Expand All @@ -90,6 +92,7 @@ impl Route {
}
}
fn append_route(mut self, route: Route) -> Self {
self.root_middlewares.extend(route.root_middlewares.clone());
self.children.push(route);
self
}
Expand All @@ -110,6 +113,7 @@ impl Route {
}
pub fn append<R: RouterAdapt>(mut self, route: R) -> Self {
let mut route = route.into_router();
self.root_middlewares.extend(route.root_middlewares.clone());
self.middlewares
.iter()
.cloned()
Expand All @@ -118,6 +122,14 @@ impl Route {
real_route.children.push(route);
self
}
pub fn root_hook(mut self, handler: impl MiddleWareHandler + 'static) -> Self {
self.root_middlewares.push(Arc::new(handler));
self
}
pub fn root_hook_first(mut self, handler: impl MiddleWareHandler + 'static) -> Self {
self.root_middlewares.insert(0, Arc::new(handler));
self
}
pub fn hook(mut self, handler: impl MiddleWareHandler + 'static) -> Self {
self.middleware_hook(Arc::new(handler));
self
Expand Down
1 change: 1 addition & 0 deletions silent/src/route/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl RootRoute {

pub fn push(&mut self, route: Route) {
self.middlewares.clone_from(&route.middlewares);
self.middlewares.extend(route.root_middlewares.clone());
self.children.push(route);
}

Expand Down

0 comments on commit cb4e873

Please sign in to comment.