Skip to content

feat: 添加log-local-time示例 #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tokio = { version = "1.38.0", optional = true }
bytes = "1.6.0"
http-body-util = "0.1.2"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["local-time"] }
tracing-subscriber = { version = "0.3.18", features = ["chrono"] }
async-trait = "0.1.80"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
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 @@ -80,6 +80,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
Loading