diff --git a/Cargo.lock b/Cargo.lock index 984f1fb1..55605431 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -534,6 +534,39 @@ dependencies = [ "tokio 1.17.0", ] +[[package]] +name = "actix-web-lab" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "034cda5de08616dcc7d261977d337eb1f7dead34a788905b0b04af7fc0f4260f" +dependencies = [ + "actix-files", + "actix-http", + "actix-router", + "actix-service", + "actix-utils", + "actix-web", + "ahash", + "bytes 1.1.0", + "csv", + "derive_more", + "digest 0.10.3", + "futures-core", + "futures-util", + "hmac 0.12.1", + "local-channel", + "log", + "matchit", + "mime", + "once_cell", + "pin-project-lite 0.2.8", + "serde 1.0.136", + "serde_json", + "serde_urlencoded", + "subtle", + "tokio 1.17.0", +] + [[package]] name = "actix_derive" version = "0.6.0" @@ -814,7 +847,7 @@ dependencies = [ "actix 0.12.0", "actix-cors", "actix-web", - "actix-web-lab", + "actix-web-lab 0.10.0", "async-graphql", "async-graphql-actix-web", "env_logger", @@ -1940,6 +1973,7 @@ dependencies = [ "actix-web", "derive_more", "env_logger", + "log", "rand 0.8.5", ] @@ -2795,6 +2829,7 @@ dependencies = [ "env_logger", "futures-util", "json", + "log", "serde 1.0.136", "serde_json", ] @@ -2819,6 +2854,8 @@ name = "json_decode_error" version = "1.0.0" dependencies = [ "actix-web", + "env_logger", + "log", "serde 1.0.136", ] @@ -2827,6 +2864,8 @@ name = "json_error" version = "1.0.0" dependencies = [ "actix-web", + "env_logger", + "log", "serde 1.0.136", "serde_json", ] @@ -2872,7 +2911,7 @@ version = "1.0.0" dependencies = [ "actix-cors", "actix-web", - "actix-web-lab", + "actix-web-lab 0.10.0", "dotenv", "env_logger", "juniper", @@ -2891,7 +2930,7 @@ version = "1.0.0" dependencies = [ "actix-cors", "actix-web", - "actix-web-lab", + "actix-web-lab 0.10.0", "env_logger", "juniper", "log", @@ -3101,6 +3140,12 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" +[[package]] +name = "matchit" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9376a4f0340565ad675d11fc1419227faf5f60cd7ac9cb2e7185a471f30af833" + [[package]] name = "md-5" version = "0.9.1" @@ -3152,6 +3197,7 @@ name = "middleware-http-to-https" version = "1.0.0" dependencies = [ "actix-web", + "actix-web-lab 0.13.0", "env_logger", "futures-util", "log", @@ -4260,6 +4306,7 @@ dependencies = [ "actix-test", "actix-web", "env_logger", + "log", "serde 1.0.136", "serde_json", "time 0.3.7", @@ -5039,6 +5086,7 @@ version = "1.0.0" dependencies = [ "actix-web", "env_logger", + "log", "tokio 1.17.0", ] @@ -5483,7 +5531,7 @@ name = "template_sailfish" version = "0.1.0" dependencies = [ "actix-web", - "actix-web-lab", + "actix-web-lab 0.10.0", "env_logger", "log", "sailfish", diff --git a/auth/redis-session/Cargo.toml b/auth/redis-session/Cargo.toml index 0d9dd486..9e5db697 100644 --- a/auth/redis-session/Cargo.toml +++ b/auth/redis-session/Cargo.toml @@ -7,7 +7,9 @@ edition = "2021" actix-web = "4" actix-session = "0.5" actix-redis = "0.10" + env_logger = "0.9" +log = "0.4" serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/auth/redis-session/src/main.rs b/auth/redis-session/src/main.rs index 421751f8..1e0fd4c4 100644 --- a/auth/redis-session/src/main.rs +++ b/auth/redis-session/src/main.rs @@ -73,8 +73,10 @@ async fn logout(session: Session) -> Result { #[actix_web::main] async fn main() -> std::io::Result<()> { - std::env::set_var("RUST_LOG", "actix_web=info,actix_redis=info"); - env_logger::init(); + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + + log::info!("starting HTTP server at http://localhost:8080"); + // Generate a random 32 byte key. Note that it is important to use a unique // private key for every project. Anyone with access to the key can generate diff --git a/auth/simple-auth-server/src/main.rs b/auth/simple-auth-server/src/main.rs index f137facd..df835c5e 100644 --- a/auth/simple-auth-server/src/main.rs +++ b/auth/simple-auth-server/src/main.rs @@ -67,7 +67,7 @@ async fn main() -> std::io::Result<()> { ), ) }) - .bind("127.0.0.1:3000")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/basics/error-handling/Cargo.toml b/basics/error-handling/Cargo.toml index 5c50c9f7..038473e3 100644 --- a/basics/error-handling/Cargo.toml +++ b/basics/error-handling/Cargo.toml @@ -5,6 +5,8 @@ edition = "2021" [dependencies] actix-web = "4" -derive_more = "0.99.2" -rand = "0.8" + +derive_more = "0.99.5" env_logger = "0.9" +log = "0.4" +rand = "0.8" diff --git a/basics/error-handling/src/main.rs b/basics/error-handling/src/main.rs index 68ddcd08..dfe56918 100644 --- a/basics/error-handling/src/main.rs +++ b/basics/error-handling/src/main.rs @@ -89,13 +89,15 @@ async fn do_something() -> Result { #[actix_web::main] async fn main() -> std::io::Result<()> { - std::env::set_var("RUST_LOG", "actix_web=info"); - env_logger::init(); + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + + log::info!("starting HTTP server at http://localhost:8080"); + HttpServer::new(move || { App::new().service(web::resource("/something").route(web::get().to(do_something))) }) - .bind("127.0.0.1:8088")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/forms/multipart-s3/README.md b/forms/multipart-s3/README.md index cf488d85..65dc81b7 100644 --- a/forms/multipart-s3/README.md +++ b/forms/multipart-s3/README.md @@ -1,25 +1,30 @@ -# multipart+s3 -Upload a file in multipart form to aws s3(https://github.com/rusoto/rusoto) -Receive multiple data in multipart form in JSON format and receive it as a struct. +# Multipart + AWS S3 + +Upload a file in multipart form to aws s3 (https://github.com/rusoto/rusoto). +Receive multiple data in multipart form in JSON format and receive it as a struct. + +# Usage -# usage ``` cd forms/multipart-s3 ``` -1. copy .env.example .env -2. edit .env AWS_ACCESS_KEY_ID=you_key -3. edit .env AWS_SECRET_ACCESS_KEY=you_key -4. edit .env AWS_S3_BUCKET_NAME=you_key +1. copy .env.example .env +1. edit .env AWS_ACCESS_KEY_ID=you_key +1. edit .env AWS_SECRET_ACCESS_KEY=you_key +1. edit .env AWS_S3_BUCKET_NAME=you_key # Running Server + ``` cd forms/multipart-s3 cargo run (or ``cargo watch -x run``) ``` -http://localhost:3000 +http://localhost:8080 # Using Other Regions + + - https://docs.rs/rusoto_core/0.42.0/rusoto_core/enum.Region.html diff --git a/https-tls/rustls-client-cert/src/main.rs b/https-tls/rustls-client-cert/src/main.rs index 42aab36f..fed7c46f 100644 --- a/https-tls/rustls-client-cert/src/main.rs +++ b/https-tls/rustls-client-cert/src/main.rs @@ -1,7 +1,7 @@ //! This example shows how to use `actix_web::HttpServer::on_connect` to access client certificates //! pass them to a handler through connection-local data. -use std::{any::Any, env, fs::File, io::BufReader, net::SocketAddr}; +use std::{any::Any, fs::File, io::BufReader, net::SocketAddr}; use actix_tls::accept::rustls::{reexports::ServerConfig, TlsStream}; use actix_web::{ @@ -69,11 +69,7 @@ fn get_client_cert(connection: &dyn Any, data: &mut Extensions) { #[actix_web::main] async fn main() -> std::io::Result<()> { - if env::var("RUST_LOG").is_err() { - env::set_var("RUST_LOG", "info"); - } - - env_logger::init(); + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); let mut cert_store = RootCertStore::empty(); @@ -107,7 +103,8 @@ async fn main() -> std::io::Result<()> { .collect(); let config = config.with_single_cert(cert_chain, keys.remove(0)).unwrap(); - // start server + log::info!("starting HTTP server at http://localhost:8080 and http://localhost:8443"); + HttpServer::new(|| App::new().default_service(web::to(route_whoami))) .on_connect(get_client_cert) .bind(("localhost", 8080))? diff --git a/json/json-decode-error/Cargo.toml b/json/json-decode-error/Cargo.toml index e1524dd3..a232270d 100644 --- a/json/json-decode-error/Cargo.toml +++ b/json/json-decode-error/Cargo.toml @@ -5,4 +5,7 @@ edition = "2021" [dependencies] actix-web = "4" + +env_logger = "0.9" +log = "0.4" serde = "1" diff --git a/json/json-decode-error/README.md b/json/json-decode-error/README.md index 8abd605a..1d89c87e 100644 --- a/json/json-decode-error/README.md +++ b/json/json-decode-error/README.md @@ -11,7 +11,7 @@ representation of the error. ```shell cd json/json_decode_error cargo run -# Started HTTP server: 127.0.0.1:8088 +# Started HTTP server: 127.0.0.1:8080 ``` ## Examples @@ -23,7 +23,7 @@ ellipsis `...`. - A well-formed request ```shell - $ curl -i 127.0.0.1:8088 -H 'Content-Type: application/json' -d '{"name": "Alice"}' + $ curl -i 127.0.0.1:8080 -H 'Content-Type: application/json' -d '{"name": "Alice"}' HTTP/1.1 200 OK ... @@ -33,7 +33,7 @@ ellipsis `...`. - Missing `Content-Type` header ```shell - $ curl -i 127.0.0.1:8088 -d '{"name": "Bob"}' + $ curl -i 127.0.0.1:8080 -d '{"name": "Bob"}' HTTP/1.1 415 Unsupported Media Type ... @@ -43,7 +43,7 @@ ellipsis `...`. - Malformed JSON ```shell - $ curl -i 127.0.0.1:8088 -H 'Content-Type: application/json' -d '{"name": "Eve}' + $ curl -i 127.0.0.1:8080 -H 'Content-Type: application/json' -d '{"name": "Eve}' HTTP/1.1 400 Bad Request ... @@ -53,7 +53,7 @@ ellipsis `...`. - JSON value of wrong type ```shell - $ curl -i 127.0.0.1:8088 -H 'Content-Type: application/json' -d '{"name": 350}' + $ curl -i 127.0.0.1:8080 -H 'Content-Type: application/json' -d '{"name": 350}' HTTP/1.1 422 Unprocessable Entity ... @@ -63,7 +63,7 @@ ellipsis `...`. - Wrong JSON key ```shell - $ curl -i 127.0.0.1:8088 -H 'Content-Type: application/json' -d '{"namn": "John"}' + $ curl -i 127.0.0.1:8080 -H 'Content-Type: application/json' -d '{"namn": "John"}' HTTP/1.1 422 Unprocessable Entity ... diff --git a/json/json-decode-error/src/main.rs b/json/json-decode-error/src/main.rs index 80948541..eed68191 100644 --- a/json/json-decode-error/src/main.rs +++ b/json/json-decode-error/src/main.rs @@ -27,6 +27,10 @@ fn json_error_handler(err: error::JsonPayloadError, _req: &HttpRequest) -> error #[actix_web::main] async fn main() -> std::io::Result<()> { + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + + log::info!("starting HTTP server at http://localhost:8080"); + HttpServer::new(|| { App::new().service(greet).app_data( web::JsonConfig::default() @@ -34,7 +38,7 @@ async fn main() -> std::io::Result<()> { .error_handler(json_error_handler), ) }) - .bind("127.0.0.1:8088")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/json/json-error/Cargo.toml b/json/json-error/Cargo.toml index dbd0a71a..8a9bb02c 100644 --- a/json/json-error/Cargo.toml +++ b/json/json-error/Cargo.toml @@ -5,5 +5,8 @@ edition = "2021" [dependencies] actix-web = "4" + +env_logger = "0.9" +log = "0.4" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/json/json-error/src/main.rs b/json/json-error/src/main.rs index af82eddb..3260d274 100644 --- a/json/json-error/src/main.rs +++ b/json/json-error/src/main.rs @@ -38,12 +38,12 @@ async fn index() -> Result { #[actix_web::main] async fn main() -> io::Result<()> { - let ip_address = "127.0.0.1:8000"; - println!("Running server on {}", ip_address); + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + + log::info!("starting HTTP server at http://localhost:8080"); HttpServer::new(|| App::new().service(web::resource("/").route(web::get().to(index)))) - .bind(ip_address) - .expect("Can not bind to port 8000") + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/json/json/Cargo.toml b/json/json/Cargo.toml index c4db84aa..0cdac9f1 100644 --- a/json/json/Cargo.toml +++ b/json/json/Cargo.toml @@ -6,8 +6,9 @@ edition = "2021" [dependencies] actix-web = "4" -env_logger = "0.9.0" +env_logger = "0.9" futures-util = { version = "0.3.7", default-features = false, features = ["std"] } json = "0.12" +log = "0.4" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/json/json/src/main.rs b/json/json/src/main.rs index 71bc8c46..11de7feb 100644 --- a/json/json/src/main.rs +++ b/json/json/src/main.rs @@ -58,8 +58,9 @@ async fn index_mjsonrust(body: web::Bytes) -> Result { #[actix_web::main] async fn main() -> std::io::Result<()> { - std::env::set_var("RUST_LOG", "actix_web=info"); - env_logger::init(); + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + + log::info!("starting HTTP server at http://localhost:8080"); HttpServer::new(|| { App::new() diff --git a/middleware/middleware-http-to-https/Cargo.toml b/middleware/middleware-http-to-https/Cargo.toml index b68e1517..ccf66d40 100644 --- a/middleware/middleware-http-to-https/Cargo.toml +++ b/middleware/middleware-http-to-https/Cargo.toml @@ -5,6 +5,8 @@ edition = "2021" [dependencies] actix-web = { version = "4", features = ["rustls"] } +actix-web-lab = "0.13" + env_logger = "0.9" futures-util = { version = "0.3.7", default-features = false, features = ["std"] } log = "0.4" diff --git a/middleware/middleware-http-to-https/src/main.rs b/middleware/middleware-http-to-https/src/main.rs index 3f543741..d41745e7 100644 --- a/middleware/middleware-http-to-https/src/main.rs +++ b/middleware/middleware-http-to-https/src/main.rs @@ -61,8 +61,8 @@ async fn main() -> std::io::Result<()> { }) .service(index) }) - .bind("127.0.0.1:80")? // Port 80 to listen for HTTP request - .bind_rustls("127.0.0.1:443", config)? // Port 443 to listen for HTTPS request + .bind(("127.0.0.1", 80))? // HTTP port + .bind_rustls(("127.0.0.1", 443), config)? // HTTPS port .run() .await } diff --git a/shutdown-server/Cargo.toml b/shutdown-server/Cargo.toml index 69b42a69..a9681116 100644 --- a/shutdown-server/Cargo.toml +++ b/shutdown-server/Cargo.toml @@ -8,4 +8,5 @@ description = "Send a request to the server to shut it down" actix-web = "4" env_logger = "0.9" +log = "0.4" tokio = { version = "1.16", features = ["signal"] } diff --git a/shutdown-server/src/main.rs b/shutdown-server/src/main.rs index 5667c198..648f19a8 100644 --- a/shutdown-server/src/main.rs +++ b/shutdown-server/src/main.rs @@ -16,13 +16,12 @@ async fn stop(stopper: web::Data>) -> HttpResponse { #[actix_web::main] async fn main() -> std::io::Result<()> { - std::env::set_var("RUST_LOG", "actix_server=debug,actix_web=debug"); - env_logger::init(); + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); // create a channel let (tx, rx) = mpsc::channel::<()>(); - let bind = ("127.0.0.1", 8080); + log::info!("starting HTTP server at http://localhost:8080"); // start server as normal but don't .await after .run() yet let server = HttpServer::new(move || { @@ -33,7 +32,7 @@ async fn main() -> std::io::Result<()> { .service(hello) .service(stop) }) - .bind(&bind)? + .bind(("127.0.0.1", 8080))? .run(); // clone the server handle diff --git a/websockets/chat-tcp/src/main.rs b/websockets/chat-tcp/src/main.rs index 6d3f80e9..125eb183 100644 --- a/websockets/chat-tcp/src/main.rs +++ b/websockets/chat-tcp/src/main.rs @@ -224,10 +224,10 @@ impl WsChatSession { async fn main() -> std::io::Result<()> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); - // Start chat server actor + // start chat server actor let server = server::ChatServer::default().start(); - // Start tcp server in separate thread + // start TCP server in separate thread let srv = server.clone(); session::tcp_server("127.0.0.1:12345", srv);