-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: create logger service * feat: opt out __tuono/data routes from logger * feat: update version to v0.12.1
- Loading branch information
1 parent
f22249f
commit c394898
Showing
13 changed files
with
133 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "tuono" | ||
version = "0.12.0" | ||
version = "0.12.1" | ||
edition = "2021" | ||
authors = ["V. Ageno <[email protected]>"] | ||
description = "The react/rust fullstack framework" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "tuono_lib" | ||
version = "0.12.0" | ||
version = "0.12.1" | ||
edition = "2021" | ||
authors = ["V. Ageno <[email protected]>"] | ||
description = "The react/rust fullstack framework" | ||
|
@@ -33,9 +33,12 @@ either = "1.13.0" | |
tower-http = {version = "0.6.0", features = ["fs"]} | ||
colored = "2.1.0" | ||
|
||
tuono_lib_macros = {path = "../tuono_lib_macros", version = "0.12.0"} | ||
tuono_lib_macros = {path = "../tuono_lib_macros", version = "0.12.1"} | ||
# Match the same version used by axum | ||
tokio-tungstenite = "0.24.0" | ||
futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] } | ||
tungstenite = "0.24.0" | ||
http = "1.1.0" | ||
pin-project = "1.1.7" | ||
tower = "0.5.1" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod logger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use colored::Colorize; | ||
use http::{method::Method, Request, Response}; | ||
use pin_project::pin_project; | ||
use std::fmt::Debug; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
use tokio::time::Instant; | ||
use tower::{Layer, Service}; | ||
|
||
#[derive(Clone)] | ||
pub struct LoggerLayer {} | ||
|
||
impl LoggerLayer { | ||
pub fn new() -> Self { | ||
LoggerLayer {} | ||
} | ||
} | ||
|
||
impl<S> Layer<S> for LoggerLayer { | ||
type Service = Logger<S>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
Logger::new(inner) | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct Logger<S> { | ||
inner: S, | ||
} | ||
|
||
impl<S> Logger<S> { | ||
pub fn new(inner: S) -> Self { | ||
Logger { inner } | ||
} | ||
} | ||
|
||
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for Logger<S> | ||
where | ||
S: Service<Request<ReqBody>, Response = Response<ResBody>>, | ||
ResBody: Default, | ||
<S as Service<Request<ReqBody>>>::Error: Debug, | ||
{ | ||
type Response = S::Response; | ||
type Error = S::Error; | ||
type Future = LoggerFuture<S::Future>; | ||
|
||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready(cx) | ||
} | ||
|
||
fn call(&mut self, req: Request<ReqBody>) -> Self::Future { | ||
let method = req.method().clone(); | ||
let path = req.uri().path().to_string(); | ||
|
||
LoggerFuture { | ||
future: self.inner.call(req), | ||
method, | ||
path, | ||
start: Instant::now(), | ||
} | ||
} | ||
} | ||
|
||
#[pin_project] | ||
pub struct LoggerFuture<F> { | ||
#[pin] | ||
future: F, | ||
method: Method, | ||
path: String, | ||
start: Instant, | ||
} | ||
|
||
impl<F, B, E> Future for LoggerFuture<F> | ||
where | ||
F: Future<Output = Result<Response<B>, E>>, | ||
B: Default, | ||
E: Debug, | ||
{ | ||
type Output = Result<Response<B>, E>; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let this = self.project(); | ||
let res: F::Output = match this.future.poll(cx) { | ||
Poll::Ready(res) => res, | ||
Poll::Pending => return Poll::Pending, | ||
}; | ||
|
||
if this.path.starts_with("/__tuono/data") { | ||
return Poll::Ready(res); | ||
} | ||
|
||
let status_code = res.as_ref().unwrap().status(); | ||
|
||
println!( | ||
" {} {} {} in {}ms", | ||
this.method, | ||
this.path, | ||
status_code.as_str().green(), | ||
this.start.elapsed().as_millis() | ||
); | ||
|
||
Poll::Ready(res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters