Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
nyxtom committed Jul 21, 2022
2 parents 0341865 + 9f3cde6 commit 5dd9dc2
Show file tree
Hide file tree
Showing 28 changed files with 77 additions and 89 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rustdoc-args = ["--cfg", "feature=\"docs\""]
default = ["h1-server"]
cookies = ["http-types/cookies"]
h1-server = ["async-h1"]
logger = ["femme"]
logger = []
docs = ["unstable"]
sessions = ["async-session", "cookies"]
sse = ["async-sse"]
Expand All @@ -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"] }
Expand All @@ -54,7 +53,9 @@ regex = "1.5.5"
[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"
Expand Down
2 changes: 1 addition & 1 deletion examples/catflap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) });
Expand Down
2 changes: 1 addition & 1 deletion examples/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion examples/concurrent_listeners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion examples/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion examples/error_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion examples/hello.rs
Original file line number Diff line number Diff line change
@@ -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());
Expand Down
2 changes: 1 addition & 1 deletion examples/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
7 changes: 4 additions & 3 deletions examples/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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};
Expand All @@ -25,7 +26,7 @@ impl UserDatabase {
// it would likely be closely tied to a specific application
async fn user_loader(mut request: Request, next: Next) -> Result {
if let Some(user) = request.state::<UserDatabase>().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
Expand Down Expand Up @@ -57,7 +58,7 @@ struct RequestCount(usize);
impl Middleware for RequestCounterMiddleware {
async fn handle(&self, mut req: Request, next: Next) -> 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;
Expand All @@ -84,7 +85,7 @@ const INTERNAL_SERVER_ERROR_HTML_PAGE: &str = "<html><body>

#[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 {
Expand Down
2 changes: 1 addition & 1 deletion examples/nested.rs
Original file line number Diff line number Diff line change
@@ -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") });
Expand Down
2 changes: 1 addition & 1 deletion examples/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") });
Expand Down
2 changes: 1 addition & 1 deletion examples/sessions.rs
Original file line number Diff line number Diff line change
@@ -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());

Expand Down
2 changes: 1 addition & 1 deletion examples/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion examples/static_file.rs
Original file line number Diff line number Diff line change
@@ -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/*") });
Expand Down
5 changes: 3 additions & 2 deletions examples/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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());

Expand All @@ -49,7 +50,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()
});
Expand Down
8 changes: 4 additions & 4 deletions src/fs/serve_dir.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -37,17 +37,17 @@ impl Endpoint for ServeDir {
}
}

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()),
Expand Down
4 changes: 2 additions & 2 deletions src/fs/serve_file.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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()),
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
1 change: 0 additions & 1 deletion src/listener/concurrent_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!") });
///
Expand Down
4 changes: 2 additions & 2 deletions src/listener/failover_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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!") });
///
Expand Down Expand Up @@ -103,7 +103,7 @@ impl Listener for FailoverListener {
return Ok(());
}
Err(e) => {
crate::log::info!("unable to bind", {
info!("unable to bind", {
listener: listener.to_string(),
error: e.to_string()
});
Expand Down
7 changes: 4 additions & 3 deletions src/listener/tcp_listener.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -56,7 +57,7 @@ fn handle_tcp(app: Server, stream: TcpStream) {
});

if let Err(error) = fut.await {
log::error!("async-h1 error", { error: error.to_string() });
error!("async-h1 error", { error: error.to_string() });
}
});
}
Expand Down Expand Up @@ -102,7 +103,7 @@ impl Listener for TcpListener {
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;
}
Expand Down
7 changes: 4 additions & 3 deletions src/listener/unix_listener.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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::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
Expand Down Expand Up @@ -57,7 +58,7 @@ fn handle_unix(app: Server, stream: UnixStream) {
});

if let Err(error) = fut.await {
log::error!("async-h1 error", { error: error.to_string() });
error!("async-h1 error", { error: error.to_string() });
}
});
}
Expand Down Expand Up @@ -100,7 +101,7 @@ impl Listener for UnixListener {
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;
}
Expand Down
Loading

0 comments on commit 5dd9dc2

Please sign in to comment.