From 89b20de3b63723de501075b4d97266c232f350c3 Mon Sep 17 00:00:00 2001 From: Sean Murphy Date: Wed, 10 Mar 2021 23:06:30 -0800 Subject: [PATCH 1/6] Session cookie secure attribute --- src/sessions/middleware.rs | 18 +++++++++++++++++- tests/sessions.rs | 6 ++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/sessions/middleware.rs b/src/sessions/middleware.rs index ce564f72..d4e14e1f 100644 --- a/src/sessions/middleware.rs +++ b/src/sessions/middleware.rs @@ -54,6 +54,7 @@ pub struct SessionMiddleware { cookie_domain: Option, session_ttl: Option, save_unchanged: bool, + secure: Option, same_site_policy: SameSite, key: Key, } @@ -66,6 +67,7 @@ impl std::fmt::Debug for SessionMiddleware { .field("cookie_name", &self.cookie_name) .field("cookie_domain", &self.cookie_domain) .field("session_ttl", &self.session_ttl) + .field("secure", &self.secure) .field("same_site_policy", &self.same_site_policy) .field("key", &"..") .field("save_unchanged", &self.save_unchanged) @@ -91,7 +93,10 @@ where session.expire_in(ttl); } - let secure_cookie = request.url().scheme() == "https"; + let mut secure_cookie = request.url().scheme() == "https"; + if let Some(secure) = self.secure { + secure_cookie = secure; + } request.set_ext(session.clone()); let mut response = next.run(request).await; @@ -140,6 +145,7 @@ impl SessionMiddleware { /// * cookie path: "/" /// * cookie name: "tide.sid" /// * session ttl: one day + /// * secure: request.scheme == 'https' /// * same site: strict /// * save unchanged: enabled /// @@ -160,6 +166,7 @@ impl SessionMiddleware { /// .with_cookie_name("custom.cookie.name") /// .with_cookie_path("/some/path") /// .with_cookie_domain("www.rust-lang.org") + /// .with_secure(true) /// .with_same_site_policy(SameSite::Lax) /// .with_session_ttl(Some(Duration::from_secs(1))) /// .without_save_unchanged(), @@ -172,6 +179,7 @@ impl SessionMiddleware { cookie_path: "/".into(), cookie_name: "tide.sid".into(), cookie_domain: None, + secure: None, same_site_policy: SameSite::Lax, session_ttl: Some(Duration::from_secs(24 * 60 * 60)), key: Key::derive_from(secret), @@ -217,6 +225,14 @@ impl SessionMiddleware { self } + /// Sets the secure attribute of the cookie. + /// Defaults to true if the incoming request scheme is 'https' + /// Can optionally be set to true or false to override + pub fn with_secure(mut self, secure: bool) -> Self { + self.secure = Some(secure); + self + } + /// Sets the same site policy for the session cookie. Defaults to /// SameSite::Lax. See [incrementally better /// cookies](https://tools.ietf.org/html/draft-west-cookie-incrementalism-01) diff --git a/tests/sessions.rs b/tests/sessions.rs index 924907a2..6fe54439 100644 --- a/tests/sessions.rs +++ b/tests/sessions.rs @@ -64,7 +64,8 @@ async fn test_customized_sessions() -> tide::Result<()> { .with_cookie_name("custom.cookie.name") .with_cookie_path("/nested") .with_cookie_domain("www.rust-lang.org") - .with_same_site_policy(SameSite::Strict) + .with_secure(true) + .with_same_site_policy(SameSite::Lax) .with_session_ttl(Some(Duration::from_secs(1))) .without_save_unchanged(), ); @@ -99,7 +100,8 @@ async fn test_customized_sessions() -> tide::Result<()> { assert!(cookies.get("tide.sid").is_none()); let cookie = &cookies["custom.cookie.name"]; assert_eq!(cookie.http_only(), Some(true)); - assert_eq!(cookie.same_site(), Some(SameSite::Strict)); + assert_eq!(cookie.secure(), Some(true)); + assert_eq!(cookie.same_site(), Some(SameSite::Lax)); assert_eq!(cookie.path(), Some("/nested")); assert_eq!(cookie.domain(), Some("www.rust-lang.org")); let cookie_value = cookie.value().to_string(); From 849df8f6dcccef5c039144b6b346d57c45381992 Mon Sep 17 00:00:00 2001 From: Sean Murphy Date: Mon, 14 Mar 2022 21:14:51 -0700 Subject: [PATCH 2/6] Fixing clippy lint --- src/listener/concurrent_listener.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/listener/concurrent_listener.rs b/src/listener/concurrent_listener.rs index 486b21e6..68ef8e67 100644 --- a/src/listener/concurrent_listener.rs +++ b/src/listener/concurrent_listener.rs @@ -113,8 +113,7 @@ where fn info(&self) -> Vec { self.listeners .iter() - .map(|listener| listener.info().into_iter()) - .flatten() + .flat_map(|listener| listener.info().into_iter()) .collect() } } From 0960344825cb51e60495288b7760fe75ef620887 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Fri, 29 Apr 2022 17:18:40 -0700 Subject: [PATCH 3/6] Stop re-exporting logging details from tide::log Continue using logging internally, but don't re-export log macros or mechanisms to start logging. --- Cargo.toml | 5 ++-- examples/catflap.rs | 2 +- examples/chunked.rs | 2 +- examples/concurrent_listeners.rs | 2 +- examples/cookies.rs | 2 +- examples/error_handling.rs | 2 +- examples/hello.rs | 2 +- examples/json.rs | 2 +- examples/middleware.rs | 7 +++--- examples/nested.rs | 2 +- examples/redirect.rs | 2 +- examples/sessions.rs | 2 +- examples/state.rs | 2 +- examples/static_file.rs | 2 +- examples/upload.rs | 5 ++-- src/fs/serve_dir.rs | 8 +++--- src/fs/serve_file.rs | 4 +-- src/lib.rs | 2 -- src/listener/concurrent_listener.rs | 1 - src/listener/failover_listener.rs | 4 +-- src/listener/tcp_listener.rs | 7 +++--- src/listener/unix_listener.rs | 7 +++--- src/log/middleware.rs | 15 ++++++------ src/log/mod.rs | 38 +---------------------------- src/route.rs | 5 ++-- src/server.rs | 6 ++--- src/sessions/middleware.rs | 3 ++- 27 files changed, 55 insertions(+), 86 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 227d37ef..3352565e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ rustdoc-args = ["--cfg", "feature=\"docs\""] default = ["h1-server", "cookies", "logger", "sessions"] cookies = ["http-types/cookies"] h1-server = ["async-h1"] -logger = ["femme"] +logger = [] docs = ["unstable"] sessions = ["async-session", "cookies"] sse = ["async-sse"] @@ -39,7 +39,6 @@ async-session = { version = "3.0", optional = true } async-sse = { version = "5.1.0", optional = true } async-std = { version = "1.6.5", features = ["unstable"] } async-trait = "0.1.41" -femme = { version = "2.1.1", optional = true } futures-util = "0.3.6" http-client = { version = "6.1.0", default-features = false } http-types = { version = "2.11.0", default-features = false, features = ["fs"] } @@ -53,7 +52,9 @@ routefinder = "0.5.0" [dev-dependencies] async-std = { version = "1.6.5", features = ["unstable", "attributes"] } criterion = "0.3.3" +femme = "2.1.1" juniper = "0.14.2" +kv-log-macro = "1.0.7" lazy_static = "1.4.0" logtest = "2.0.0" portpicker = "0.1.0" diff --git a/examples/catflap.rs b/examples/catflap.rs index c3be1e67..91c141c1 100644 --- a/examples/catflap.rs +++ b/examples/catflap.rs @@ -2,7 +2,7 @@ #[async_std::main] async fn main() -> Result<(), std::io::Error> { use std::{env, net::TcpListener, os::unix::io::FromRawFd}; - tide::log::start(); + femme::start(); let mut app = tide::new(); app.with(tide::log::LogMiddleware::new()); app.at("/").get(|_| async { Ok(CHANGE_THIS_TEXT) }); diff --git a/examples/chunked.rs b/examples/chunked.rs index 6ea80539..aa1f2193 100644 --- a/examples/chunked.rs +++ b/examples/chunked.rs @@ -2,7 +2,7 @@ use tide::Body; #[async_std::main] async fn main() -> Result<(), std::io::Error> { - tide::log::start(); + femme::start(); let mut app = tide::new(); app.with(tide::log::LogMiddleware::new()); app.at("/").get(|_| async { diff --git a/examples/concurrent_listeners.rs b/examples/concurrent_listeners.rs index 10d2ec64..b79e6fb5 100644 --- a/examples/concurrent_listeners.rs +++ b/examples/concurrent_listeners.rs @@ -2,7 +2,7 @@ use tide::Request; #[async_std::main] async fn main() -> Result<(), std::io::Error> { - tide::log::start(); + femme::start(); let mut app = tide::new(); app.with(tide::log::LogMiddleware::new()); diff --git a/examples/cookies.rs b/examples/cookies.rs index 68f3a611..966bd447 100644 --- a/examples/cookies.rs +++ b/examples/cookies.rs @@ -21,7 +21,7 @@ async fn remove_cookie(_req: Request<()>) -> tide::Result { #[async_std::main] async fn main() -> Result<(), std::io::Error> { - tide::log::start(); + femme::start(); let mut app = tide::new(); app.with(tide::log::LogMiddleware::new()); diff --git a/examples/error_handling.rs b/examples/error_handling.rs index 96aa79eb..ca35277d 100644 --- a/examples/error_handling.rs +++ b/examples/error_handling.rs @@ -5,7 +5,7 @@ use tide::{Body, Request, Response, Result, StatusCode}; #[async_std::main] async fn main() -> Result<()> { - tide::log::start(); + femme::start(); let mut app = tide::new(); app.with(tide::log::LogMiddleware::new()); diff --git a/examples/hello.rs b/examples/hello.rs index d6647531..39e2089b 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -1,6 +1,6 @@ #[async_std::main] async fn main() -> Result<(), std::io::Error> { - tide::log::start(); + femme::start(); let mut app = tide::new(); app.with(tide::log::LogMiddleware::new()); diff --git a/examples/json.rs b/examples/json.rs index 8bfb123e..085a359d 100644 --- a/examples/json.rs +++ b/examples/json.rs @@ -9,7 +9,7 @@ struct Cat { #[async_std::main] async fn main() -> tide::Result<()> { - tide::log::start(); + femme::start(); let mut app = tide::new(); app.with(tide::log::LogMiddleware::new()); diff --git a/examples/middleware.rs b/examples/middleware.rs index a7488a59..39709149 100644 --- a/examples/middleware.rs +++ b/examples/middleware.rs @@ -3,6 +3,7 @@ use std::pin::Pin; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; +use kv_log_macro::trace; use tide::http::mime; use tide::utils::{After, Before}; use tide::{Middleware, Next, Request, Response, Result, StatusCode}; @@ -31,7 +32,7 @@ fn user_loader<'a>( ) -> Pin + Send + 'a>> { Box::pin(async { if let Some(user) = request.state().find_user().await { - tide::log::trace!("user loaded", {user: user.name}); + trace!("user loaded", {user: user.name}); request.set_ext(user); Ok(next.run(request).await) // this middleware only needs to run before the endpoint, so @@ -64,7 +65,7 @@ struct RequestCount(usize); impl Middleware for RequestCounterMiddleware { async fn handle(&self, mut req: Request, next: Next<'_, State>) -> Result { let count = self.requests_counted.fetch_add(1, Ordering::Relaxed); - tide::log::trace!("request counter", { count: count }); + trace!("request counter", { count: count }); req.set_ext(RequestCount(count)); let mut res = next.run(req).await; @@ -91,7 +92,7 @@ const INTERNAL_SERVER_ERROR_HTML_PAGE: &str = " #[async_std::main] async fn main() -> Result<()> { - tide::log::start(); + femme::start(); let mut app = tide::with_state(UserDatabase::default()); app.with(After(|response: Response| async move { diff --git a/examples/nested.rs b/examples/nested.rs index b9eece73..c87acde5 100644 --- a/examples/nested.rs +++ b/examples/nested.rs @@ -1,6 +1,6 @@ #[async_std::main] async fn main() -> Result<(), std::io::Error> { - tide::log::start(); + femme::start(); let mut app = tide::new(); app.with(tide::log::LogMiddleware::new()); app.at("/").get(|_| async { Ok("Root") }); diff --git a/examples/redirect.rs b/examples/redirect.rs index c0ab4c64..e9ded4b8 100644 --- a/examples/redirect.rs +++ b/examples/redirect.rs @@ -2,7 +2,7 @@ use tide::{Redirect, Response, StatusCode}; #[async_std::main] async fn main() -> Result<(), std::io::Error> { - tide::log::start(); + femme::start(); let mut app = tide::new(); app.with(tide::log::LogMiddleware::new()); app.at("/").get(|_| async { Ok("Root") }); diff --git a/examples/sessions.rs b/examples/sessions.rs index 5a2cf899..f6e9e966 100644 --- a/examples/sessions.rs +++ b/examples/sessions.rs @@ -1,6 +1,6 @@ #[async_std::main] async fn main() -> Result<(), std::io::Error> { - tide::log::start(); + femme::start(); let mut app = tide::new(); app.with(tide::log::LogMiddleware::new()); diff --git a/examples/state.rs b/examples/state.rs index a687ca2f..5679c8a6 100644 --- a/examples/state.rs +++ b/examples/state.rs @@ -16,7 +16,7 @@ impl State { #[async_std::main] async fn main() -> tide::Result<()> { - tide::log::start(); + femme::start(); let mut app = tide::with_state(State::new()); app.with(tide::log::LogMiddleware::new()); app.at("/").get(|req: tide::Request| async move { diff --git a/examples/static_file.rs b/examples/static_file.rs index 6cd80a6a..8a961eef 100644 --- a/examples/static_file.rs +++ b/examples/static_file.rs @@ -1,6 +1,6 @@ #[async_std::main] async fn main() -> Result<(), std::io::Error> { - tide::log::start(); + femme::start(); let mut app = tide::new(); app.with(tide::log::LogMiddleware::new()); app.at("/").get(|_| async { Ok("visit /src/*") }); diff --git a/examples/upload.rs b/examples/upload.rs index 0297a368..4bf2da51 100644 --- a/examples/upload.rs +++ b/examples/upload.rs @@ -3,6 +3,7 @@ use std::path::Path; use std::sync::Arc; use async_std::{fs::OpenOptions, io}; +use kv_log_macro::info; use tempfile::TempDir; use tide::prelude::*; use tide::{Body, Request, Response, StatusCode}; @@ -26,7 +27,7 @@ impl TempDirState { #[async_std::main] async fn main() -> Result<(), IoError> { - tide::log::start(); + femme::start(); let mut app = tide::with_state(TempDirState::try_new()?); app.with(tide::log::LogMiddleware::new()); @@ -48,7 +49,7 @@ async fn main() -> Result<(), IoError> { let bytes_written = io::copy(req, file).await?; - tide::log::info!("file written", { + info!("file written", { bytes: bytes_written, path: fs_path.canonicalize()?.to_str() }); diff --git a/src/fs/serve_dir.rs b/src/fs/serve_dir.rs index 9b464153..431dfcd9 100644 --- a/src/fs/serve_dir.rs +++ b/src/fs/serve_dir.rs @@ -1,7 +1,7 @@ -use crate::log; use crate::{Body, Endpoint, Request, Response, Result, StatusCode}; use async_std::path::PathBuf as AsyncPathBuf; +use kv_log_macro::{info, warn}; use std::path::{Path, PathBuf}; use std::{ffi::OsStr, io}; @@ -40,17 +40,17 @@ where } } - log::info!("Requested file: {:?}", file_path); + info!("Requested file: {:?}", file_path); let file_path = AsyncPathBuf::from(file_path); if !file_path.starts_with(&self.dir) { - log::warn!("Unauthorized attempt to read: {:?}", file_path); + warn!("Unauthorized attempt to read: {:?}", file_path); Ok(Response::new(StatusCode::Forbidden)) } else { match Body::from_file(&file_path).await { Ok(body) => Ok(Response::builder(StatusCode::Ok).body(body).build()), Err(e) if e.kind() == io::ErrorKind::NotFound => { - log::warn!("File not found: {:?}", &file_path); + warn!("File not found: {:?}", &file_path); Ok(Response::new(StatusCode::NotFound)) } Err(e) => Err(e.into()), diff --git a/src/fs/serve_file.rs b/src/fs/serve_file.rs index 0c3d72c4..2ed80e41 100644 --- a/src/fs/serve_file.rs +++ b/src/fs/serve_file.rs @@ -1,10 +1,10 @@ -use crate::log; use crate::{Body, Endpoint, Request, Response, Result, StatusCode}; use std::io; use std::path::Path; use async_std::path::PathBuf as AsyncPathBuf; use async_trait::async_trait; +use kv_log_macro::warn; pub(crate) struct ServeFile { path: AsyncPathBuf, @@ -26,7 +26,7 @@ impl Endpoint for ServeFile { match Body::from_file(&self.path).await { Ok(body) => Ok(Response::builder(StatusCode::Ok).body(body).build()), Err(e) if e.kind() == io::ErrorKind::NotFound => { - log::warn!("File not found: {:?}", &self.path); + warn!("File not found: {:?}", &self.path); Ok(Response::new(StatusCode::NotFound)) } Err(e) => Err(e.into()), diff --git a/src/lib.rs b/src/lib.rs index 90f43d6b..b9e739d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,9 +32,7 @@ //! //! #[async_std::main] //! async fn main() -> tide::Result<()> { -//! tide::log::start(); //! let mut app = tide::new(); -//! app.with(tide::log::LogMiddleware::new()); //! app.at("/orders/shoes").post(order_shoes); //! app.listen("127.0.0.1:8080").await?; //! Ok(()) diff --git a/src/listener/concurrent_listener.rs b/src/listener/concurrent_listener.rs index 68ef8e67..906f1f4c 100644 --- a/src/listener/concurrent_listener.rs +++ b/src/listener/concurrent_listener.rs @@ -13,7 +13,6 @@ use futures_util::stream::{futures_unordered::FuturesUnordered, StreamExt}; /// ```rust /// fn main() -> Result<(), std::io::Error> { /// async_std::task::block_on(async { -/// tide::log::start(); /// let mut app = tide::new(); /// app.at("/").get(|_| async { Ok("Hello, world!") }); /// diff --git a/src/listener/failover_listener.rs b/src/listener/failover_listener.rs index d81658c7..26f0ee19 100644 --- a/src/listener/failover_listener.rs +++ b/src/listener/failover_listener.rs @@ -4,6 +4,7 @@ use crate::Server; use std::fmt::{self, Debug, Display, Formatter}; use async_std::io; +use kv_log_macro::info; use crate::listener::ListenInfo; @@ -15,7 +16,6 @@ use crate::listener::ListenInfo; /// ```rust /// fn main() -> Result<(), std::io::Error> { /// async_std::task::block_on(async { -/// tide::log::start(); /// let mut app = tide::new(); /// app.at("/").get(|_| async { Ok("Hello, world!") }); /// @@ -109,7 +109,7 @@ where return Ok(()); } Err(e) => { - crate::log::info!("unable to bind", { + info!("unable to bind", { listener: listener.to_string(), error: e.to_string() }); diff --git a/src/listener/tcp_listener.rs b/src/listener/tcp_listener.rs index 7b86a013..03be652e 100644 --- a/src/listener/tcp_listener.rs +++ b/src/listener/tcp_listener.rs @@ -1,13 +1,14 @@ use super::{is_transient_error, ListenInfo}; use crate::listener::Listener; -use crate::{log, Server}; +use crate::Server; use std::fmt::{self, Display, Formatter}; use async_std::net::{self, SocketAddr, TcpStream}; use async_std::prelude::*; use async_std::{io, task}; +use kv_log_macro::error; /// This represents a tide [Listener](crate::listener::Listener) that /// wraps an [async_std::net::TcpListener]. It is implemented as an @@ -56,7 +57,7 @@ fn handle_tcp(app: Server, stream: }); if let Err(error) = fut.await { - log::error!("async-h1 error", { error: error.to_string() }); + error!("async-h1 error", { error: error.to_string() }); } }); } @@ -105,7 +106,7 @@ where Err(ref e) if is_transient_error(e) => continue, Err(error) => { let delay = std::time::Duration::from_millis(500); - crate::log::error!("Error: {}. Pausing for {:?}.", error, delay); + error!("Error: {}. Pausing for {:?}.", error, delay); task::sleep(delay).await; continue; } diff --git a/src/listener/unix_listener.rs b/src/listener/unix_listener.rs index d99a21d3..85c717f7 100644 --- a/src/listener/unix_listener.rs +++ b/src/listener/unix_listener.rs @@ -1,7 +1,7 @@ use super::{is_transient_error, ListenInfo}; use crate::listener::Listener; -use crate::{log, Server}; +use crate::Server; use std::fmt::{self, Display, Formatter}; @@ -9,6 +9,7 @@ use async_std::os::unix::net::{self, SocketAddr, UnixStream}; use async_std::path::PathBuf; use async_std::prelude::*; use async_std::{io, task}; +use kv_log_macro::error; /// This represents a tide [Listener](crate::listener::Listener) that /// wraps an [async_std::os::unix::net::UnixListener]. It is implemented as an @@ -57,7 +58,7 @@ fn handle_unix(app: Server, stream: }); if let Err(error) = fut.await { - log::error!("async-h1 error", { error: error.to_string() }); + error!("async-h1 error", { error: error.to_string() }); } }); } @@ -103,7 +104,7 @@ where Err(ref e) if is_transient_error(e) => continue, Err(error) => { let delay = std::time::Duration::from_millis(500); - crate::log::error!("Error: {}. Pausing for {:?}.", error, delay); + error!("Error: {}. Pausing for {:?}.", error, delay); task::sleep(delay).await; continue; } diff --git a/src/log/middleware.rs b/src/log/middleware.rs index 144db885..bfd5c3e2 100644 --- a/src/log/middleware.rs +++ b/src/log/middleware.rs @@ -1,4 +1,5 @@ -use crate::log; +use kv_log_macro::{error, info, warn}; + use crate::{Middleware, Next, Request}; /// Log all incoming requests and responses. @@ -40,7 +41,7 @@ impl LogMiddleware { let path = req.url().path().to_owned(); let method = req.method().to_string(); - log::info!("<-- Request received", { + info!("<-- Request received", { method: method, path: path, }); @@ -49,7 +50,7 @@ impl LogMiddleware { let status = response.status(); if status.is_server_error() { if let Some(error) = response.error() { - log::error!("Internal error --> Response sent", { + error!("Internal error --> Response sent", { message: format!("{:?}", error), error_type: error.type_name(), method: method, @@ -58,7 +59,7 @@ impl LogMiddleware { duration: format!("{:?}", start.elapsed()), }); } else { - log::error!("Internal error --> Response sent", { + error!("Internal error --> Response sent", { method: method, path: path, status: format!("{} - {}", status as u16, status.canonical_reason()), @@ -67,7 +68,7 @@ impl LogMiddleware { } } else if status.is_client_error() { if let Some(error) = response.error() { - log::warn!("Client error --> Response sent", { + warn!("Client error --> Response sent", { message: format!("{:?}", error), error_type: error.type_name(), method: method, @@ -76,7 +77,7 @@ impl LogMiddleware { duration: format!("{:?}", start.elapsed()), }); } else { - log::warn!("Client error --> Response sent", { + warn!("Client error --> Response sent", { method: method, path: path, status: format!("{} - {}", status as u16, status.canonical_reason()), @@ -84,7 +85,7 @@ impl LogMiddleware { }); } } else { - log::info!("--> Response sent", { + info!("--> Response sent", { method: method, path: path, status: format!("{} - {}", status as u16, status.canonical_reason()), diff --git a/src/log/mod.rs b/src/log/mod.rs index 8f74ac2d..f4143023 100644 --- a/src/log/mod.rs +++ b/src/log/mod.rs @@ -1,41 +1,5 @@ -//! Event logging types. -//! -//! # Examples -//! -//! ```no_run -//! use tide::log; -//! -//! log::start(); -//! -//! log::info!("Hello cats"); -//! log::debug!("{} wants tuna", "Nori"); -//! log::error!("We're out of tuna!"); -//! log::info!("{} are hungry", "cats", { -//! cat_1: "Chashu", -//! cat_2: "Nori", -//! }); -//! ``` - -pub use kv_log_macro::{debug, error, info, log, trace, warn}; -pub use kv_log_macro::{max_level, Level}; +//! Support for logging in tide; see [`LogMiddleware`]. mod middleware; -#[cfg(feature = "logger")] -pub use femme::LevelFilter; - pub use middleware::LogMiddleware; - -/// Start logging. -#[cfg(feature = "logger")] -pub fn start() { - femme::start(); - crate::log::info!("Logger started", { level: "Info" }); -} - -/// Start logging with a log level. -#[cfg(feature = "logger")] -pub fn with_level(level: LevelFilter) { - femme::with_level(level); - crate::log::info!("Logger started", { level: format!("{}", level) }); -} diff --git a/src/route.rs b/src/route.rs index 8b827c1d..e52889fd 100644 --- a/src/route.rs +++ b/src/route.rs @@ -5,9 +5,10 @@ use std::sync::Arc; use crate::endpoint::MiddlewareEndpoint; use crate::fs::{ServeDir, ServeFile}; -use crate::log; use crate::{router::Router, Endpoint, Middleware}; +use kv_log_macro::trace; + /// A handle to a route. /// /// All HTTP requests are made against resources. After using [`Server::at`] (or @@ -81,7 +82,7 @@ impl<'a, State: Clone + Send + Sync + 'static> Route<'a, State> { where M: Middleware, { - log::trace!( + trace!( "Adding middleware {} to route {:?}", middleware.name(), self.path diff --git a/src/server.rs b/src/server.rs index 1849e9ca..3b09fc40 100644 --- a/src/server.rs +++ b/src/server.rs @@ -2,11 +2,11 @@ use async_std::io; use async_std::sync::Arc; +use kv_log_macro::{info, trace}; #[cfg(feature = "cookies")] use crate::cookies; use crate::listener::{Listener, ToListener}; -use crate::log; use crate::middleware::{Middleware, Next}; use crate::router::{Router, Selection}; use crate::{Endpoint, Request, Route}; @@ -179,7 +179,7 @@ where where M: Middleware, { - log::trace!("Adding middleware {}", middleware.name()); + trace!("Adding middleware {}", middleware.name()); let m = Arc::get_mut(&mut self.middleware) .expect("Registering middleware is not possible after the Server has started"); m.push(Arc::new(middleware)); @@ -207,7 +207,7 @@ where let mut listener = listener.to_listener()?; listener.bind(self).await?; for info in listener.info().iter() { - log::info!("Server listening on {}", info); + info!("Server listening on {}", info); } listener.accept().await?; Ok(()) diff --git a/src/sessions/middleware.rs b/src/sessions/middleware.rs index ce564f72..db612a53 100644 --- a/src/sessions/middleware.rs +++ b/src/sessions/middleware.rs @@ -11,6 +11,7 @@ use async_session::{ hmac::{Hmac, Mac, NewMac}, sha2::Sha256, }; +use kv_log_macro::error; const BASE64_DIGEST_LEN: usize = 44; @@ -98,7 +99,7 @@ where if session.is_destroyed() { if let Err(e) = self.store.destroy_session(session).await { - crate::log::error!("unable to destroy session", { error: e.to_string() }); + error!("unable to destroy session", { error: e.to_string() }); } if let Some(mut cookie) = cookie { From 1b0caefc74645dd6bc20919b113db6485564aa6c Mon Sep 17 00:00:00 2001 From: Thomas Holloway Date: Mon, 11 Jul 2022 11:36:09 -0500 Subject: [PATCH 4/6] Add tide-flash message middleware --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c65eb89f..fb13c4b5 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ team. Use at your own risk. ### Auth * [tide-http-auth](https://github.com/chrisdickinson/tide-http-auth) * [tide-openidconnect](https://github.com/malyn/tide-openidconnect) -* [tide-jwt](htps://github.com/nyxtom/tide-jwt) +* [tide-jwt](https://github.com/nyxtom/tide-jwt) ### Testing * [tide-testing](https://github.com/jbr/tide-testing) @@ -148,6 +148,7 @@ team. Use at your own risk. * [tide-compressed-sse](https://github.com/Yarn/tide_compressed_sse) * [tide-websockets](https://github.com/http-rs/tide-websockets) * [tide-csrf](https://github.com/malyn/tide-csrf) +* [tide-flash](https://github.com/nyxtom/tide-flash) ### Session Stores * [async-redis-session](https://github.com/jbr/async-redis-session) From 9ea54445a6ad1573595966dc6e3a56954d4a7ab8 Mon Sep 17 00:00:00 2001 From: Thomas Holloway Date: Mon, 18 Jul 2022 23:16:01 -0500 Subject: [PATCH 5/6] Update github ci to include logger feature for examples and some tests --- .github/workflows/ci.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b2ae1bac..f58372af 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -31,7 +31,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: check - args: --all --bins --examples + args: --all --bins --examples --features logger - name: check avoid-dev-deps uses: actions-rs/cargo@v1 @@ -44,7 +44,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: check - args: --all --bins --examples --tests --features unstable + args: --all --bins --examples --tests --features unstable,logger - name: check no-default-features uses: actions-rs/cargo@v1 @@ -62,13 +62,13 @@ jobs: uses: actions-rs/cargo@v1 with: command: test - args: --all + args: --all --features logger - name: tests unstable uses: actions-rs/cargo@v1 with: command: test - args: --all --features unstable + args: --all --features unstable,logger check_fmt_and_docs: name: Checking fmt, clippy, and docs @@ -82,7 +82,7 @@ jobs: rustc --version - name: clippy - run: cargo clippy --tests --examples -- -D warnings + run: cargo clippy --tests --examples --features logger -- -D warnings - name: fmt run: cargo fmt --all -- --check From 8a9282776e0f6af142d97b71bc5e858e032be862 Mon Sep 17 00:00:00 2001 From: Thomas Holloway Date: Thu, 14 Jul 2022 23:39:14 -0500 Subject: [PATCH 6/6] Fix warning on symetrical boolean nonminimal bool --- src/security/cors.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/security/cors.rs b/src/security/cors.rs index cfb2f04d..f85d6652 100644 --- a/src/security/cors.rs +++ b/src/security/cors.rs @@ -462,6 +462,7 @@ mod test { } #[test] + #[allow(clippy::nonminimal_bool)] fn symetrical() { let regex = Regex::new(r"e[xzs]a.*le.com*").unwrap(); let x = Origin::from(regex.clone());