From bbeb262a5caa72780e16283a90cf27514c6475e2 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 8 May 2018 11:08:43 -0700 Subject: [PATCH] upgrade actix-web to 0.6 --- Cargo.lock | 154 ++++++++++++++++++++++++++++------ Cargo.toml | 1 + async_ex1/Cargo.toml | 2 +- async_ex1/src/main.rs | 29 ++++--- basics/Cargo.toml | 2 +- basics/src/main.rs | 43 +++++----- cookie-auth/Cargo.toml | 2 +- cookie-auth/src/auth.rs | 33 ++++---- cookie-auth/src/main.rs | 31 ++++--- cookie-session/Cargo.toml | 2 +- cookie-session/src/main.rs | 10 +-- diesel/Cargo.toml | 2 +- diesel/src/db.rs | 12 +-- diesel/src/main.rs | 46 +++++----- hello-world/Cargo.toml | 2 +- hello-world/src/main.rs | 12 +-- http-proxy/Cargo.toml | 2 +- http-proxy/src/main.rs | 6 +- http-proxy/src/server.rs | 2 +- json/Cargo.toml | 2 +- json/src/main.rs | 33 ++++---- juniper/Cargo.toml | 2 +- juniper/src/main.rs | 51 ++++++----- multipart/Cargo.toml | 2 +- multipart/src/main.rs | 20 ++--- protobuf/Cargo.toml | 2 +- protobuf/src/main.rs | 19 ++--- protobuf/src/protobuf.rs | 53 ++++++------ r2d2/src/db.rs | 25 +++--- r2d2/src/main.rs | 34 ++++---- redis-session/Cargo.toml | 4 +- redis-session/src/main.rs | 8 +- rustfmt.toml | 5 ++ state/Cargo.toml | 2 +- state/src/main.rs | 18 ++-- template_askama/Cargo.toml | 4 +- template_askama/src/main.rs | 16 ++-- template_tera/Cargo.toml | 2 +- template_tera/src/main.rs | 37 ++++---- tls/Cargo.toml | 2 +- tls/src/main.rs | 29 ++++--- unix-socket/Cargo.toml | 2 +- unix-socket/src/main.rs | 13 ++- web-cors/backend/Cargo.toml | 2 +- web-cors/backend/src/main.rs | 43 ++++++---- web-cors/backend/src/user.rs | 17 ++-- websocket-chat/Cargo.toml | 2 +- websocket-chat/src/client.rs | 71 +++++++++------- websocket-chat/src/codec.rs | 39 +++++---- websocket-chat/src/main.rs | 107 +++++++++++++---------- websocket-chat/src/server.rs | 15 ++-- websocket-chat/src/session.rs | 100 +++++++++++++--------- websocket/Cargo.toml | 2 +- websocket/src/client.rs | 26 +++--- websocket/src/main.rs | 5 +- 55 files changed, 689 insertions(+), 518 deletions(-) create mode 100644 rustfmt.toml diff --git a/Cargo.lock b/Cargo.lock index c5d5e1c..f076317 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,11 +22,11 @@ dependencies = [ [[package]] name = "actix-redis" -version = "0.3.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "backoff 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "cookie 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -71,7 +71,6 @@ dependencies = [ "mio 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)", "net2 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl 0.10.7 (registry+https://github.com/rust-lang/crates.io-index)", "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -83,6 +82,53 @@ dependencies = [ "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "actix-web" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "base64 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "brotli2 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", + "cookie 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "flate2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "h2 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "http 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "http-range 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "httparse 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "mime 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "mime_guess 2.0.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)", + "net2 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl 0.10.7 (registry+https://github.com/rust-lang/crates.io-index)", + "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_urlencoded 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-io 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-openssl 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "version_check 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -93,7 +139,7 @@ name = "actix-web-cors" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", @@ -173,6 +219,21 @@ dependencies = [ "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "awc_examples" +version = "0.1.0" +dependencies = [ + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", + "validator 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "validator_derive 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "backoff" version = "0.1.2" @@ -239,7 +300,7 @@ name = "basics" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -291,6 +352,15 @@ dependencies = [ "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "card-validate" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cargo_metadata" version = "0.3.3" @@ -339,7 +409,7 @@ name = "cookie-auth" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "cookie 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", @@ -351,7 +421,7 @@ name = "cookie-session" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", @@ -475,7 +545,7 @@ name = "diesel-example" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -724,7 +794,7 @@ name = "hello-world" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -751,7 +821,7 @@ name = "http-proxy" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -789,6 +859,11 @@ dependencies = [ "unicode-normalization 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "if_chain" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "indexmap" version = "1.0.1" @@ -843,7 +918,7 @@ name = "json-example" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", @@ -873,7 +948,7 @@ name = "juniper-example" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "juniper 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1066,7 +1141,7 @@ name = "multipart-example" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1275,7 +1350,7 @@ name = "protobuf-example" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1410,8 +1485,8 @@ name = "redis-session" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-redis 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-redis 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1658,7 +1733,7 @@ name = "state" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1729,7 +1804,7 @@ name = "template-askama" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "askama 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1739,7 +1814,7 @@ name = "template-tera" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "tera 0.11.7 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1805,7 +1880,7 @@ name = "tls-example" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.7 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2062,7 +2137,7 @@ name = "unix-socket" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-uds 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2114,6 +2189,32 @@ dependencies = [ "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "validator" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "card-validate 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "validator_derive" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "if_chain 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)", + "validator 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "vcpkg" version = "0.2.3" @@ -2144,7 +2245,7 @@ name = "websocket" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2155,7 +2256,7 @@ name = "websocket-example" version = "0.1.0" dependencies = [ "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2237,8 +2338,9 @@ dependencies = [ [metadata] "checksum actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e767e3170dc7cdec50fe1b74d22fd9d2f4b78b97b2052a254b5acb07dae68634" -"checksum actix-redis 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d2c35d0dae8a853584c66b2db88e61203cd0b690666aae3cc3451cfc345791c8" +"checksum actix-redis 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f6ed04247bb9ee9a2a9a0ad808049de379d8197719bbb22ea0a56e9a10f55a50" "checksum actix-web 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ebda1eae1638d03af2fc27a7d57c5fd035901a949f548d7c72ff5e07f5052d9f" +"checksum actix-web 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a32c694413af2fd0f3f3bb9fcc820ca5eb081c810dd77f023d07957fac4296d3" "checksum actix_derive 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c4b1dc922654b9aca7a8a31eab875fde804fa9fbd67f220f2e457787b23590f2" "checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4" "checksum antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "34fde25430d87a9388dadbe6e34d7f72a462c8b43ac8d309b42b0a8505d7e2a5" @@ -2260,6 +2362,7 @@ dependencies = [ "checksum bytecount 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "af27422163679dea46a1a7239dffff64d3dcdc3ba5fe9c49c789fbfe0eb949de" "checksum byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "73b5bdfe7ee3ad0b99c9801d58807a9dbc9e09196365b0203853b99889ab3c87" "checksum bytes 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "2f1d50c876fb7545f5f289cd8b2aee3f359d073ae819eed5d6373638e2c61e59" +"checksum card-validate 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "377e2cbab679f09143661a24262602f6f5e0fb20d164b464c0fc25c4268937c9" "checksum cargo_metadata 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1f56ec3e469bca7c276f2eea015aa05c5e381356febdbb0683c2580189604537" "checksum cc 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "0ebb87d1116151416c0cf66a0e3fb6430cccd120fd6300794b4dfaa050ac40ba" "checksum cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "405216fd8fe65f718daa7102ea808a946b6ce40c742998fbfd3463645552de18" @@ -2312,6 +2415,7 @@ dependencies = [ "checksum humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" "checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e" "checksum idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "014b298351066f1512874135335d62a789ffe78a9974f94b43ed5621951eaf7d" +"checksum if_chain 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "61bb90bdd39e3af69b0172dfc6130f6cd6332bf040fbb9bdd4401d37adbd48b8" "checksum indexmap 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08173ba1e906efb6538785a8844dd496f5d34f0a2d88038e95195172fc667220" "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" "checksum ipconfig 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9ec4e18c0a0d4340870c14284293632d8421f419008371422dd327892b88877c" @@ -2450,6 +2554,8 @@ dependencies = [ "checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" "checksum uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcc7e3b898aa6f6c08e5295b6c89258d1331e9ac578cc992fb818759951bdc22" "checksum uuid 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8630752f979f1b6b87c49830a5e3784082545de63920d59fbaac252474319447" +"checksum validator 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0ee73a134910391b2a3ffd99f5ba0a67ebc0731e7cf92d56b6dccd1542823163" +"checksum validator_derive 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ac6c1c4023f5feb24a1b54a02e8828ae231659cf72b20f6191a0656163a4ca12" "checksum vcpkg 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7ed0f6789c8a85ca41bbc1c9d175422116a9869bd1cf31bb08e1493ecce60380" "checksum version_check 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6b772017e347561807c1aa192438c5fd74242a670a6cffacc40f2defd1dc069d" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" diff --git a/Cargo.toml b/Cargo.toml index 59f7ed6..ce6d56a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "./", + "async_ex1", "basics", "cookie-auth", "cookie-session", diff --git a/async_ex1/Cargo.toml b/async_ex1/Cargo.toml index cdfcde1..9b66bbd 100644 --- a/async_ex1/Cargo.toml +++ b/async_ex1/Cargo.toml @@ -5,7 +5,7 @@ authors = ["dowwie "] [dependencies] actix = "0.5.6" -actix-web = { version = "^0.5", features=["alpn"] } +actix-web = { version = "^0.6", features=["alpn"] } futures = "0.1" serde = "1.0.43" serde_derive = "1.0.43" diff --git a/async_ex1/src/main.rs b/async_ex1/src/main.rs index 7b76dd9..b2b68c8 100644 --- a/async_ex1/src/main.rs +++ b/async_ex1/src/main.rs @@ -1,8 +1,8 @@ // This is a contrived example intended to illustrate actix-web features. // *Imagine* that you have a process that involves 3 steps. The steps here // are dumb in that they do nothing other than call an -// httpbin endpoint that returns the json that was posted to it. The intent here -// is to illustrate how to chain these steps together as futures and return +// httpbin endpoint that returns the json that was posted to it. The intent +// here is to illustrate how to chain these steps together as futures and return // a final result in a response. // // Actix-web features illustrated here include: @@ -20,15 +20,15 @@ extern crate serde_derive; extern crate serde_json; #[macro_use] extern crate validator_derive; +extern crate env_logger; extern crate futures; extern crate validator; -extern crate env_logger; -use std::time::Duration; +use actix_web::{client, dev::Handler, http::Method, server, App, AsyncResponder, Body, + Error, HttpMessage, HttpRequest, HttpResponse, Json, Result}; use futures::{future::ok as fut_ok, Future}; use std::collections::HashMap; -use actix_web::{client, dev::Handler, http::Method, server, App, AsyncResponder, Body, Error, - HttpMessage, HttpRequest, HttpResponse, Json, Result}; +use std::time::Duration; use validator::{Validate, ValidationError}; #[derive(Debug, Validate, Deserialize, Serialize)] @@ -46,9 +46,9 @@ struct HttpBinResponse { files: HashMap, form: HashMap, headers: HashMap, - json: SomeData, + json: SomeData, origin: String, - url: String + url: String, } /// post json to httpbin, get it back in the response body, return deserialized @@ -70,15 +70,18 @@ fn step_x(data: SomeData) -> Box> { ) } -fn create_something(some_data: Json) -> Box> { +fn create_something( + some_data: Json, +) -> Box> { step_x(some_data.into_inner()) .and_then(|some_data_2| { step_x(some_data_2).and_then(|some_data_3| { - step_x(some_data_3).and_then(|d| + step_x(some_data_3).and_then(|d| { Ok(HttpResponse::Ok() - .content_type("application/json") - .body(serde_json::to_string(&d).unwrap()) - .into())) + .content_type("application/json") + .body(serde_json::to_string(&d).unwrap()) + .into()) + }) }) }) .responder() diff --git a/basics/Cargo.toml b/basics/Cargo.toml index 8811a05..157978d 100644 --- a/basics/Cargo.toml +++ b/basics/Cargo.toml @@ -8,4 +8,4 @@ workspace = "../" futures = "0.1" env_logger = "0.5" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" diff --git a/basics/src/main.rs b/basics/src/main.rs index 300d5eb..d75117b 100644 --- a/basics/src/main.rs +++ b/basics/src/main.rs @@ -1,5 +1,5 @@ #![allow(unused_variables)] -#![cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))] +#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] extern crate actix; extern crate actix_web; @@ -7,12 +7,12 @@ extern crate env_logger; extern crate futures; use futures::Stream; -use std::{io, env}; -use actix_web::{error, fs, pred, server, - App, HttpRequest, HttpResponse, Result, Error}; use actix_web::http::{header, Method, StatusCode}; -use actix_web::middleware::{self, RequestSession}; -use futures::future::{FutureResult, result}; +use actix_web::middleware::session::{self, RequestSession}; +use actix_web::{error, fs, middleware, pred, server, App, Error, HttpRequest, + HttpResponse, Result}; +use futures::future::{result, FutureResult}; +use std::{env, io}; /// favicon handler fn favicon(req: HttpRequest) -> Result { @@ -40,39 +40,36 @@ fn welcome(mut req: HttpRequest) -> Result { req.session().set("counter", counter)?; } - // response Ok(HttpResponse::build(StatusCode::OK) - .content_type("text/html; charset=utf-8") - .body(include_str!("../static/welcome.html"))) - + .content_type("text/html; charset=utf-8") + .body(include_str!("../static/welcome.html"))) } /// 404 handler fn p404(req: HttpRequest) -> Result { - Ok(fs::NamedFile::open("static/404.html")? - .set_status_code(StatusCode::NOT_FOUND)) + Ok(fs::NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND)) } - /// async handler -fn index_async(req: HttpRequest) -> FutureResult -{ +fn index_async(req: HttpRequest) -> FutureResult { println!("{:?}", req); - result(Ok(HttpResponse::Ok() - .content_type("text/html") - .body(format!("Hello {}!", req.match_info().get("name").unwrap())))) + result(Ok(HttpResponse::Ok().content_type("text/html").body( + format!("Hello {}!", req.match_info().get("name").unwrap()), + ))) } /// handler with path parameters like `/user/{name}/` -fn with_param(req: HttpRequest) -> HttpResponse -{ +fn with_param(req: HttpRequest) -> HttpResponse { println!("{:?}", req); HttpResponse::Ok() .content_type("test/plain") - .body(format!("Hello {}!", req.match_info().get("name").unwrap())) + .body(format!( + "Hello {}!", + req.match_info().get("name").unwrap() + )) } fn main() { @@ -86,8 +83,8 @@ fn main() { // enable logger .middleware(middleware::Logger::default()) // cookie session middleware - .middleware(middleware::SessionStorage::new( - middleware::CookieSessionBackend::signed(&[0; 32]).secure(false) + .middleware(session::SessionStorage::new( + session::CookieSessionBackend::signed(&[0; 32]).secure(false) )) // register favicon .resource("/favicon", |r| r.f(favicon)) diff --git a/cookie-auth/Cargo.toml b/cookie-auth/Cargo.toml index 7ffa728..c811812 100644 --- a/cookie-auth/Cargo.toml +++ b/cookie-auth/Cargo.toml @@ -6,7 +6,7 @@ workspace = "../" [dependencies] actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" cookie = { version="0.10", features=["percent-encode", "secure"] } futures = "0.1" time = "0.1" diff --git a/cookie-auth/src/auth.rs b/cookie-auth/src/auth.rs index ed747ca..a110d98 100644 --- a/cookie-auth/src/auth.rs +++ b/cookie-auth/src/auth.rs @@ -1,18 +1,16 @@ use std::rc::Rc; use cookie::{Cookie, CookieJar, Key}; +use futures::future::{err as FutErr, ok as FutOk, FutureResult}; use futures::Future; -use futures::future::{FutureResult, err as FutErr, ok as FutOk}; use time::Duration; -use actix_web::{HttpRequest, HttpResponse, Error, Result}; use actix_web::http::header::{self, HeaderValue}; use actix_web::middleware::{Middleware, Response, Started}; - +use actix_web::{Error, HttpRequest, HttpResponse, Result}; /// Trait provides identity service for the request. pub trait RequestIdentity { - /// Return the claimed identity of the user associated request or /// ``None`` if no identity can be found associated with the request. fn identity(&mut self) -> Option<&str>; @@ -20,33 +18,32 @@ pub trait RequestIdentity { /// Remember identity. fn remember(&mut self, identity: String); - /// This method is used to 'forget' the current identity on subsequent requests. + /// This method is used to 'forget' the current identity on subsequent + /// requests. fn forget(&mut self); - } impl RequestIdentity for HttpRequest { fn identity(&mut self) -> Option<&str> { if let Some(id) = self.extensions().get::() { - return id.0.identity() + return id.0.identity(); } None } fn remember(&mut self, identity: String) { - if let Some(id) = self.extensions().get_mut::() { - return id.0.remember(identity) + if let Some(id) = self.extensions_mut().get_mut::() { + return id.0.remember(identity); } } fn forget(&mut self) { - if let Some(id) = self.extensions().get_mut::() { - return id.0.forget() + if let Some(id) = self.extensions_mut().get_mut::() { + return id.0.forget(); } } } - /// An identity pub trait Identity: 'static { fn identity(&self) -> Option<&str>; @@ -87,7 +84,6 @@ unsafe impl Send for IdentityBox {} #[doc(hidden)] unsafe impl Sync for IdentityBox {} - impl> Middleware for IdentityService { fn start(&self, req: &mut HttpRequest) -> Result { let mut req = req.clone(); @@ -96,7 +92,7 @@ impl> Middleware for IdentityService { .from_request(&mut req) .then(move |res| match res { Ok(id) => { - req.extensions().insert(IdentityBox(Box::new(id))); + req.extensions_mut().insert(IdentityBox(Box::new(id))); FutOk(None) } Err(err) => FutErr(err), @@ -104,8 +100,10 @@ impl> Middleware for IdentityService { Ok(Started::Future(Box::new(fut))) } - fn response(&self, req: &mut HttpRequest, resp: HttpResponse) -> Result { - if let Some(mut id) = req.extensions().remove::() { + fn response( + &self, req: &mut HttpRequest, resp: HttpResponse, + ) -> Result { + if let Some(mut id) = req.extensions_mut().remove::() { id.0.write(resp) } else { Ok(Response::Done(resp)) @@ -207,7 +205,7 @@ impl CookieIdentityInner { let cookie_opt = jar.private(&self.key).get(&self.name); if let Some(cookie) = cookie_opt { - return Some(cookie.value().into()) + return Some(cookie.value().into()); } } } @@ -220,7 +218,6 @@ impl CookieIdentityInner { pub struct CookieIdentityPolicy(Rc); impl CookieIdentityPolicy { - /// Construct new `CookieIdentityPolicy` instance. /// /// Panics if key length is less than 32 bytes. diff --git a/cookie-auth/src/main.rs b/cookie-auth/src/main.rs index 7c178a8..cba7f5a 100644 --- a/cookie-auth/src/main.rs +++ b/cookie-auth/src/main.rs @@ -1,15 +1,15 @@ extern crate actix; extern crate actix_web; extern crate cookie; -extern crate time; -extern crate futures; extern crate env_logger; +extern crate futures; +extern crate time; -use actix_web::{server, middleware, App, HttpRequest, HttpResponse}; +use actix_web::middleware::session::RequestSession; +use actix_web::{middleware, server, App, HttpRequest, HttpResponse}; mod auth; -use auth::{RequestIdentity, IdentityService, CookieIdentityPolicy}; - +use auth::{CookieIdentityPolicy, IdentityService, RequestIdentity}; fn index(mut req: HttpRequest) -> String { format!("Hello {}", req.identity().unwrap_or("Anonymous")) @@ -17,16 +17,12 @@ fn index(mut req: HttpRequest) -> String { fn login(mut req: HttpRequest) -> HttpResponse { req.remember("user1".to_owned()); - HttpResponse::Found() - .header("location", "/") - .finish() + HttpResponse::Found().header("location", "/").finish() } fn logout(mut req: HttpRequest) -> HttpResponse { req.forget(); - HttpResponse::Found() - .header("location", "/") - .finish() + HttpResponse::Found().header("location", "/").finish() } fn main() { @@ -34,18 +30,19 @@ fn main() { env_logger::init(); let sys = actix::System::new("cookie-auth"); - server::new( - || App::new() + server::new(|| { + App::new() .middleware(middleware::Logger::default()) .middleware(IdentityService::new( CookieIdentityPolicy::new(&[0; 32]) .name("auth-example") - .secure(false))) - + .secure(false), + )) .resource("/login", |r| r.f(login)) .resource("/logout", |r| r.f(logout)) - .resource("/", |r| r.f(index))) - .bind("127.0.0.1:8080").unwrap() + .resource("/", |r| r.f(index)) + }).bind("127.0.0.1:8080") + .unwrap() .start(); println!("Started http server: 127.0.0.1:8080"); diff --git a/cookie-session/Cargo.toml b/cookie-session/Cargo.toml index 42483d7..45042ca 100644 --- a/cookie-session/Cargo.toml +++ b/cookie-session/Cargo.toml @@ -6,7 +6,7 @@ workspace = "../" [dependencies] actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" futures = "0.1" time = "0.1" env_logger = "0.5" diff --git a/cookie-session/src/main.rs b/cookie-session/src/main.rs index 7d17ac7..0dd10a0 100644 --- a/cookie-session/src/main.rs +++ b/cookie-session/src/main.rs @@ -10,12 +10,12 @@ extern crate actix_web; extern crate env_logger; extern crate futures; -use actix_web::middleware::{self, RequestSession}; -use actix_web::{server, App, HttpRequest, Result}; +use actix_web::middleware::session::{self, RequestSession}; +use actix_web::{middleware, server, App, HttpRequest, Result}; use std::env; /// simple index handler with session -fn index(mut req: HttpRequest) -> Result<&'static str> { +fn index(req: HttpRequest) -> Result<&'static str> { println!("{:?}", req); // RequestSession trait is used for session access @@ -41,8 +41,8 @@ fn main() { // enable logger .middleware(middleware::Logger::default()) // cookie session middleware - .middleware(middleware::SessionStorage::new( - middleware::CookieSessionBackend::signed(&[0; 32]).secure(false) + .middleware(session::SessionStorage::new( + session::CookieSessionBackend::signed(&[0; 32]).secure(false) )) .resource("/", |r| r.f(index)) }).bind("127.0.0.1:8080") diff --git a/diesel/Cargo.toml b/diesel/Cargo.toml index 44e5252..d9bfc4c 100644 --- a/diesel/Cargo.toml +++ b/diesel/Cargo.toml @@ -7,7 +7,7 @@ workspace = "../" [dependencies] env_logger = "0.5" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" futures = "0.1" uuid = { version = "0.5", features = ["serde", "v4"] } diff --git a/diesel/src/db.rs b/diesel/src/db.rs index 81c745d..de3dd17 100644 --- a/diesel/src/db.rs +++ b/diesel/src/db.rs @@ -1,10 +1,10 @@ //! Db executor actor -use uuid; -use diesel; -use actix_web::*; use actix::prelude::*; +use actix_web::*; +use diesel; use diesel::prelude::*; -use diesel::r2d2::{Pool, ConnectionManager}; +use diesel::r2d2::{ConnectionManager, Pool}; +use uuid; use models; use schema; @@ -12,8 +12,8 @@ use schema; /// This is db executor actor. We are going to run 3 of them in parallel. pub struct DbExecutor(pub Pool>); -/// This is only message that this actor can handle, but it is easy to extend number of -/// messages. +/// This is only message that this actor can handle, but it is easy to extend +/// number of messages. pub struct CreateUser { pub name: String, } diff --git a/diesel/src/main.rs b/diesel/src/main.rs index 2fd7087..d4e6613 100644 --- a/diesel/src/main.rs +++ b/diesel/src/main.rs @@ -1,28 +1,28 @@ //! Actix web diesel example //! //! Diesel does not support tokio, so we have to run it in separate threads. -//! Actix supports sync actors by default, so we going to create sync actor that use diesel. -//! Technically sync actors are worker style actors, multiple of them -//! can run in parallel and process messages from same queue. +//! Actix supports sync actors by default, so we going to create sync actor +//! that use diesel. Technically sync actors are worker style actors, multiple +//! of them can run in parallel and process messages from same queue. extern crate serde; extern crate serde_json; #[macro_use] extern crate serde_derive; #[macro_use] extern crate diesel; -extern crate r2d2; -extern crate uuid; -extern crate futures; extern crate actix; extern crate actix_web; extern crate env_logger; +extern crate futures; +extern crate r2d2; +extern crate uuid; use actix::prelude::*; -use actix_web::{http, server, middleware, - App, Path, State, HttpResponse, AsyncResponder, FutureResponse}; +use actix_web::{http, middleware, server, App, AsyncResponder, FutureResponse, + HttpResponse, Path, State}; use diesel::prelude::*; -use diesel::r2d2::{ Pool, ConnectionManager }; +use diesel::r2d2::{ConnectionManager, Pool}; use futures::future::Future; mod db; @@ -31,7 +31,6 @@ mod schema; use db::{CreateUser, DbExecutor}; - /// State with DbExecutor address struct AppState { db: Addr, @@ -40,13 +39,15 @@ struct AppState { /// Async request handler fn index(name: Path, state: State) -> FutureResponse { // send async `CreateUser` message to a `DbExecutor` - state.db.send(CreateUser{name: name.into_inner()}) + state + .db + .send(CreateUser { + name: name.into_inner(), + }) .from_err() - .and_then(|res| { - match res { - Ok(user) => Ok(HttpResponse::Ok().json(user)), - Err(_) => Ok(HttpResponse::InternalServerError().into()) - } + .and_then(|res| match res { + Ok(user) => Ok(HttpResponse::Ok().json(user)), + Err(_) => Ok(HttpResponse::InternalServerError().into()), }) .responder() } @@ -58,19 +59,20 @@ fn main() { // Start 3 db executor actors let manager = ConnectionManager::::new("test.db"); - let pool = r2d2::Pool::builder().build(manager).expect("Failed to create pool."); + let pool = r2d2::Pool::builder() + .build(manager) + .expect("Failed to create pool."); - let addr = SyncArbiter::start(3, move || { - DbExecutor(pool.clone()) - }); + let addr = SyncArbiter::start(3, move || DbExecutor(pool.clone())); // Start http server server::new(move || { App::with_state(AppState{db: addr.clone()}) // enable logger .middleware(middleware::Logger::default()) - .resource("/{name}", |r| r.method(http::Method::GET).with2(index))}) - .bind("127.0.0.1:8080").unwrap() + .resource("/{name}", |r| r.method(http::Method::GET).with2(index)) + }).bind("127.0.0.1:8080") + .unwrap() .start(); println!("Started http server: 127.0.0.1:8080"); diff --git a/hello-world/Cargo.toml b/hello-world/Cargo.toml index cee5485..a1b1731 100644 --- a/hello-world/Cargo.toml +++ b/hello-world/Cargo.toml @@ -7,4 +7,4 @@ workspace = "../" [dependencies] env_logger = "0.5" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" diff --git a/hello-world/src/main.rs b/hello-world/src/main.rs index 2af4789..d7a5ecd 100644 --- a/hello-world/src/main.rs +++ b/hello-world/src/main.rs @@ -2,8 +2,7 @@ extern crate actix; extern crate actix_web; extern crate env_logger; -use actix_web::{App, HttpRequest, server, middleware}; - +use actix_web::{middleware, server, App, HttpRequest}; fn index(_req: HttpRequest) -> &'static str { "Hello world!" @@ -14,13 +13,14 @@ fn main() { env_logger::init(); let sys = actix::System::new("hello-world"); - server::new( - || App::new() + server::new(|| { + App::new() // enable logger .middleware(middleware::Logger::default()) .resource("/index.html", |r| r.f(|_| "Hello world!")) - .resource("/", |r| r.f(index))) - .bind("127.0.0.1:8080").unwrap() + .resource("/", |r| r.f(index)) + }).bind("127.0.0.1:8080") + .unwrap() .start(); println!("Started http server: 127.0.0.1:8080"); diff --git a/http-proxy/Cargo.toml b/http-proxy/Cargo.toml index eb3e3d5..6a9de25 100644 --- a/http-proxy/Cargo.toml +++ b/http-proxy/Cargo.toml @@ -16,4 +16,4 @@ path = "src/server.rs" env_logger = "0.5" futures = "0.1" actix = "0.5" -actix-web = { version = "^0.5", features=["alpn"] } +actix-web = { version = "^0.6", features=["alpn"] } diff --git a/http-proxy/src/main.rs b/http-proxy/src/main.rs index 2007e02..bf3b35b 100644 --- a/http-proxy/src/main.rs +++ b/http-proxy/src/main.rs @@ -3,8 +3,8 @@ extern crate actix_web; extern crate env_logger; extern crate futures; -use actix_web::{client, middleware, server, App, AsyncResponder, Body, Error, HttpMessage, - HttpRequest, HttpResponse}; +use actix_web::{client, middleware, server, App, AsyncResponder, Body, Error, + HttpMessage, HttpRequest, HttpResponse}; use futures::{Future, Stream}; /// Stream client request response and then send body to a server response @@ -48,7 +48,7 @@ fn main() { .middleware(middleware::Logger::default()) .resource("/streaming", |r| r.f(streaming)) .resource("/", |r| r.f(index)) - }).threads(1) + }).workers(1) .bind("127.0.0.1:8080") .unwrap() .start(); diff --git a/http-proxy/src/server.rs b/http-proxy/src/server.rs index a5ba79e..4748c4b 100644 --- a/http-proxy/src/server.rs +++ b/http-proxy/src/server.rs @@ -24,7 +24,7 @@ fn main() { .middleware(middleware::Logger::default()) .resource("/index.html", |r| r.f(|_| "Hello world!")) .resource("/", |r| r.f(index)) - }).threads(1) + }).workers(1) .bind("127.0.0.1:8081") .unwrap() .start(); diff --git a/json/Cargo.toml b/json/Cargo.toml index 8d56a24..e331dfe 100644 --- a/json/Cargo.toml +++ b/json/Cargo.toml @@ -15,4 +15,4 @@ serde_derive = "1.0" json = "*" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" diff --git a/json/src/main.rs b/json/src/main.rs index f864e00..67c7725 100644 --- a/json/src/main.rs +++ b/json/src/main.rs @@ -1,15 +1,16 @@ extern crate actix; extern crate actix_web; extern crate bytes; -extern crate futures; extern crate env_logger; +extern crate futures; extern crate serde_json; -#[macro_use] extern crate serde_derive; -#[macro_use] extern crate json; +#[macro_use] +extern crate serde_derive; +#[macro_use] +extern crate json; -use actix_web::{ - middleware, http, error, server, - App, AsyncResponder, HttpRequest, HttpResponse, HttpMessage, Error, Json}; +use actix_web::{error, http, middleware, server, App, AsyncResponder, Error, + HttpMessage, HttpRequest, HttpResponse, Json}; use bytes::BytesMut; use futures::{Future, Stream}; @@ -22,7 +23,7 @@ struct MyObj { } /// This handler uses `HttpRequest::json()` for loading json object. -fn index(req: HttpRequest) -> Box> { +fn index(req: HttpRequest) -> Box> { req.json() .from_err() // convert all errors into `Error` .and_then(|val: MyObj| { @@ -35,13 +36,13 @@ fn index(req: HttpRequest) -> Box> { /// This handler uses json extractor fn extract_item(item: Json) -> HttpResponse { println!("model: {:?}", &item); - HttpResponse::Ok().json(item.0) // <- send response + HttpResponse::Ok().json(item.0) // <- send response } -const MAX_SIZE: usize = 262_144; // max payload size is 256k +const MAX_SIZE: usize = 262_144; // max payload size is 256k /// This handler manually load request payload and parse json object -fn index_manual(req: HttpRequest) -> Box> { +fn index_manual(req: HttpRequest) -> Box> { // HttpRequest is stream of Bytes objects req // `Future::from_err` acts like `?` in that it coerces the error type from @@ -70,13 +71,16 @@ fn index_manual(req: HttpRequest) -> Box> } /// This handler manually load request payload and parse json-rust -fn index_mjsonrust(req: HttpRequest) -> Box> { +fn index_mjsonrust(req: HttpRequest) -> Box> { req.concat2() .from_err() .and_then(|body| { // body is loaded, now we can deserialize json-rust let result = json::parse(std::str::from_utf8(&body).unwrap()); // return Result - let injson: JsonValue = match result { Ok(v) => v, Err(e) => object!{"err" => e.to_string() } }; + let injson: JsonValue = match result { + Ok(v) => v, + Err(e) => object!{"err" => e.to_string() }, + }; Ok(HttpResponse::Ok() .content_type("application/json") .body(injson.dump())) @@ -100,8 +104,9 @@ fn main() { }) .resource("/manual", |r| r.method(http::Method::POST).f(index_manual)) .resource("/mjsonrust", |r| r.method(http::Method::POST).f(index_mjsonrust)) - .resource("/", |r| r.method(http::Method::POST).f(index))}) - .bind("127.0.0.1:8080").unwrap() + .resource("/", |r| r.method(http::Method::POST).f(index)) + }).bind("127.0.0.1:8080") + .unwrap() .shutdown_timeout(1) .start(); diff --git a/juniper/Cargo.toml b/juniper/Cargo.toml index cfcb58e..95e022b 100644 --- a/juniper/Cargo.toml +++ b/juniper/Cargo.toml @@ -7,7 +7,7 @@ workspace = "../" [dependencies] env_logger = "0.5" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" futures = "0.1" serde = "1.0" diff --git a/juniper/src/main.rs b/juniper/src/main.rs index a92ce3f..df5ef28 100644 --- a/juniper/src/main.rs +++ b/juniper/src/main.rs @@ -7,23 +7,22 @@ extern crate serde_json; extern crate serde_derive; #[macro_use] extern crate juniper; -extern crate futures; extern crate actix; extern crate actix_web; extern crate env_logger; +extern crate futures; use actix::prelude::*; -use actix_web::{ - middleware, http, server, - App, AsyncResponder, HttpRequest, HttpResponse, FutureResponse, Error, State, Json}; +use actix_web::{http, middleware, server, App, AsyncResponder, Error, FutureResponse, + HttpRequest, HttpResponse, Json, State}; +use futures::future::Future; use juniper::http::graphiql::graphiql_source; use juniper::http::GraphQLRequest; -use futures::future::Future; mod schema; -use schema::Schema; use schema::create_schema; +use schema::Schema; struct AppState { executor: Addr, @@ -37,14 +36,12 @@ impl Message for GraphQLData { } pub struct GraphQLExecutor { - schema: std::sync::Arc + schema: std::sync::Arc, } impl GraphQLExecutor { fn new(schema: std::sync::Arc) -> GraphQLExecutor { - GraphQLExecutor { - schema: schema, - } + GraphQLExecutor { schema: schema } } } @@ -62,23 +59,24 @@ impl Handler for GraphQLExecutor { } } -fn graphiql(_req: HttpRequest) -> Result { +fn graphiql(_req: HttpRequest) -> Result { let html = graphiql_source("http://127.0.0.1:8080/graphql"); Ok(HttpResponse::Ok() - .content_type("text/html; charset=utf-8") - .body(html)) + .content_type("text/html; charset=utf-8") + .body(html)) } -fn graphql(st: State, data: Json) -> FutureResponse { - st.executor.send(data.0) +fn graphql( + st: State, data: Json, +) -> FutureResponse { + st.executor + .send(data.0) .from_err() - .and_then(|res| { - match res { - Ok(user) => Ok(HttpResponse::Ok() - .content_type("application/json") - .body(user)), - Err(_) => Ok(HttpResponse::InternalServerError().into()) - } + .and_then(|res| match res { + Ok(user) => Ok(HttpResponse::Ok() + .content_type("application/json") + .body(user)), + Err(_) => Ok(HttpResponse::InternalServerError().into()), }) .responder() } @@ -89,9 +87,7 @@ fn main() { let sys = actix::System::new("juniper-example"); let schema = std::sync::Arc::new(create_schema()); - let addr = SyncArbiter::start(3, move || { - GraphQLExecutor::new(schema.clone()) - }); + let addr = SyncArbiter::start(3, move || GraphQLExecutor::new(schema.clone())); // Start http server server::new(move || { @@ -99,8 +95,9 @@ fn main() { // enable logger .middleware(middleware::Logger::default()) .resource("/graphql", |r| r.method(http::Method::POST).with2(graphql)) - .resource("/graphiql", |r| r.method(http::Method::GET).h(graphiql))}) - .bind("127.0.0.1:8080").unwrap() + .resource("/graphiql", |r| r.method(http::Method::GET).h(graphiql)) + }).bind("127.0.0.1:8080") + .unwrap() .start(); println!("Started http server: 127.0.0.1:8080"); diff --git a/multipart/Cargo.toml b/multipart/Cargo.toml index f08194f..0f133d1 100644 --- a/multipart/Cargo.toml +++ b/multipart/Cargo.toml @@ -12,4 +12,4 @@ path = "src/main.rs" env_logger = "*" futures = "0.1" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" diff --git a/multipart/src/main.rs b/multipart/src/main.rs index 75f2896..dd07d66 100644 --- a/multipart/src/main.rs +++ b/multipart/src/main.rs @@ -5,16 +5,13 @@ extern crate env_logger; extern crate futures; use actix::*; -use actix_web::{ - http, middleware, multipart, server, - App, AsyncResponder, HttpRequest, HttpResponse, HttpMessage, Error}; +use actix_web::{http, middleware, multipart, server, App, AsyncResponder, Error, + HttpMessage, HttpRequest, HttpResponse}; -use futures::{Future, Stream}; use futures::future::{result, Either}; +use futures::{Future, Stream}; - -fn index(req: HttpRequest) -> Box> -{ +fn index(req: HttpRequest) -> Box> { println!("{:?}", req); req.multipart() // <- get multipart stream for current request @@ -49,11 +46,12 @@ fn main() { let _ = env_logger::init(); let sys = actix::System::new("multipart-example"); - server::new( - || App::new() + server::new(|| { + App::new() .middleware(middleware::Logger::default()) // <- logger - .resource("/multipart", |r| r.method(http::Method::POST).a(index))) - .bind("127.0.0.1:8080").unwrap() + .resource("/multipart", |r| r.method(http::Method::POST).a(index)) + }).bind("127.0.0.1:8080") + .unwrap() .start(); println!("Starting http server: 127.0.0.1:8080"); diff --git a/protobuf/Cargo.toml b/protobuf/Cargo.toml index 6c8584a..566260b 100644 --- a/protobuf/Cargo.toml +++ b/protobuf/Cargo.toml @@ -14,4 +14,4 @@ prost = "0.2.0" prost-derive = "0.2.0" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" diff --git a/protobuf/src/main.rs b/protobuf/src/main.rs index c0a2abb..6da9999 100644 --- a/protobuf/src/main.rs +++ b/protobuf/src/main.rs @@ -9,26 +9,23 @@ extern crate prost; #[macro_use] extern crate prost_derive; +use actix_web::{http, middleware, server, App, AsyncResponder, Error, HttpRequest, + HttpResponse}; use futures::Future; -use actix_web::{ - http, middleware, server, - App, AsyncResponder, HttpRequest, HttpResponse, Error}; mod protobuf; use protobuf::ProtoBufResponseBuilder; - #[derive(Clone, Debug, PartialEq, Message)] pub struct MyObj { - #[prost(int32, tag="1")] + #[prost(int32, tag = "1")] pub number: i32, - #[prost(string, tag="2")] + #[prost(string, tag = "2")] pub name: String, } - /// This handler uses `ProtoBufMessage` for loading protobuf object. -fn index(req: HttpRequest) -> Box> { +fn index(req: HttpRequest) -> Box> { protobuf::ProtoBufMessage::new(req) .from_err() // convert all errors into `Error` .and_then(|val: MyObj| { @@ -38,7 +35,6 @@ fn index(req: HttpRequest) -> Box> { .responder() } - fn main() { ::std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); @@ -47,8 +43,9 @@ fn main() { server::new(|| { App::new() .middleware(middleware::Logger::default()) - .resource("/", |r| r.method(http::Method::POST).f(index))}) - .bind("127.0.0.1:8080").unwrap() + .resource("/", |r| r.method(http::Method::POST).f(index)) + }).bind("127.0.0.1:8080") + .unwrap() .shutdown_timeout(1) .start(); diff --git a/protobuf/src/protobuf.rs b/protobuf/src/protobuf.rs index 2b117fe..1e46a6d 100644 --- a/protobuf/src/protobuf.rs +++ b/protobuf/src/protobuf.rs @@ -1,38 +1,36 @@ use bytes::{Bytes, BytesMut}; -use futures::{Poll, Future, Stream}; +use futures::{Future, Poll, Stream}; use bytes::IntoBuf; -use prost::Message; use prost::DecodeError as ProtoBufDecodeError; use prost::EncodeError as ProtoBufEncodeError; +use prost::Message; -use actix_web::http::header::{CONTENT_TYPE, CONTENT_LENGTH}; -use actix_web::{Responder, HttpMessage, HttpRequest, HttpResponse}; use actix_web::dev::HttpResponseBuilder; use actix_web::error::{Error, PayloadError, ResponseError}; - +use actix_web::http::header::{CONTENT_LENGTH, CONTENT_TYPE}; +use actix_web::{HttpMessage, HttpRequest, HttpResponse, Responder}; #[derive(Fail, Debug)] pub enum ProtoBufPayloadError { /// Payload size is bigger than 256k - #[fail(display="Payload size is bigger than 256k")] + #[fail(display = "Payload size is bigger than 256k")] Overflow, /// Content type error - #[fail(display="Content type error")] + #[fail(display = "Content type error")] ContentType, /// Serialize error - #[fail(display="ProtoBud serialize error: {}", _0)] + #[fail(display = "ProtoBud serialize error: {}", _0)] Serialize(#[cause] ProtoBufEncodeError), /// Deserialize error - #[fail(display="ProtoBud deserialize error: {}", _0)] + #[fail(display = "ProtoBud deserialize error: {}", _0)] Deserialize(#[cause] ProtoBufDecodeError), /// Payload error - #[fail(display="Error that occur during reading payload: {}", _0)] + #[fail(display = "Error that occur during reading payload: {}", _0)] Payload(#[cause] PayloadError), } impl ResponseError for ProtoBufPayloadError { - fn error_response(&self) -> HttpResponse { match *self { ProtoBufPayloadError::Overflow => HttpResponse::PayloadTooLarge().into(), @@ -60,31 +58,31 @@ impl Responder for ProtoBuf { type Item = HttpResponse; type Error = Error; - fn respond_to(self, _: HttpRequest) -> Result { + fn respond_to(self, _: &HttpRequest) -> Result { let mut buf = Vec::new(); - self.0.encode(&mut buf) + self.0 + .encode(&mut buf) .map_err(|e| Error::from(ProtoBufPayloadError::Serialize(e))) .and_then(|()| { Ok(HttpResponse::Ok() - .content_type("application/protobuf") + .content_type("application/protobuf") .body(buf) .into()) - }) + }) } } -pub struct ProtoBufMessage{ +pub struct ProtoBufMessage { limit: usize, ct: &'static str, req: Option, - fut: Option>>, + fut: Option>>, } impl ProtoBufMessage { - /// Create `ProtoBufMessage` for request. pub fn new(req: T) -> Self { - ProtoBufMessage{ + ProtoBufMessage { limit: 262_144, req: Some(req), fut: None, @@ -109,7 +107,8 @@ impl ProtoBufMessage { } impl Future for ProtoBufMessage - where T: HttpMessage + Stream + 'static +where + T: HttpMessage + Stream + 'static, { type Item = U; type Error = ProtoBufPayloadError; @@ -129,7 +128,7 @@ impl Future for ProtoBufMessage } // check content-type if !self.ct.is_empty() && req.content_type() != self.ct { - return Err(ProtoBufPayloadError::ContentType) + return Err(ProtoBufPayloadError::ContentType); } let limit = self.limit; @@ -146,23 +145,25 @@ impl Future for ProtoBufMessage self.fut = Some(Box::new(fut)); } - self.fut.as_mut().expect("ProtoBufBody could not be used second time").poll() + self.fut + .as_mut() + .expect("ProtoBufBody could not be used second time") + .poll() } } - pub trait ProtoBufResponseBuilder { - fn protobuf(&mut self, value: T) -> Result; } impl ProtoBufResponseBuilder for HttpResponseBuilder { - fn protobuf(&mut self, value: T) -> Result { self.header(CONTENT_TYPE, "application/protobuf"); let mut body = Vec::new(); - value.encode(&mut body).map_err(|e| ProtoBufPayloadError::Serialize(e))?; + value + .encode(&mut body) + .map_err(|e| ProtoBufPayloadError::Serialize(e))?; Ok(self.body(body)) } } diff --git a/r2d2/src/db.rs b/r2d2/src/db.rs index 6e2ddc0..340d28b 100644 --- a/r2d2/src/db.rs +++ b/r2d2/src/db.rs @@ -1,17 +1,16 @@ //! Db executor actor -use std::io; -use uuid; -use actix_web::*; use actix::prelude::*; +use actix_web::*; use r2d2::Pool; use r2d2_sqlite::SqliteConnectionManager; - +use std::io; +use uuid; /// This is db executor actor. We are going to run 3 of them in parallel. pub struct DbExecutor(pub Pool); -/// This is only message that this actor can handle, but it is easy to extend number of -/// messages. +/// This is only message that this actor can handle, but it is easy to extend +/// number of messages. pub struct CreateUser { pub name: String, } @@ -31,11 +30,15 @@ impl Handler for DbExecutor { let conn = self.0.get().unwrap(); let uuid = format!("{}", uuid::Uuid::new_v4()); - conn.execute("INSERT INTO users (id, name) VALUES ($1, $2)", - &[&uuid, &msg.name]).unwrap(); + conn.execute( + "INSERT INTO users (id, name) VALUES ($1, $2)", + &[&uuid, &msg.name], + ).unwrap(); - Ok(conn.query_row("SELECT name FROM users WHERE id=$1", &[&uuid], |row| { - row.get(0) - }).map_err(|_| io::Error::new(io::ErrorKind::Other, "db error"))?) + Ok(conn.query_row( + "SELECT name FROM users WHERE id=$1", + &[&uuid], + |row| row.get(0), + ).map_err(|_| io::Error::new(io::ErrorKind::Other, "db error"))?) } } diff --git a/r2d2/src/main.rs b/r2d2/src/main.rs index 5e6d07f..439ed8e 100644 --- a/r2d2/src/main.rs +++ b/r2d2/src/main.rs @@ -1,41 +1,42 @@ //! Actix web r2d2 example -extern crate serde; -extern crate serde_json; -extern crate uuid; -extern crate futures; extern crate actix; extern crate actix_web; extern crate env_logger; +extern crate futures; extern crate r2d2; extern crate r2d2_sqlite; extern crate rusqlite; +extern crate serde; +extern crate serde_json; +extern crate uuid; use actix::prelude::*; -use actix_web::{ - middleware, http, server, App, AsyncResponder, HttpRequest, HttpResponse, Error}; +use actix_web::{http, middleware, server, App, AsyncResponder, Error, HttpRequest, + HttpResponse}; use futures::future::Future; use r2d2_sqlite::SqliteConnectionManager; mod db; use db::{CreateUser, DbExecutor}; - /// State with DbExecutor address struct State { db: Addr, } /// Async request handler -fn index(req: HttpRequest) -> Box> { +fn index(req: HttpRequest) -> Box> { let name = &req.match_info()["name"]; - req.state().db.send(CreateUser{name: name.to_owned()}) + req.state() + .db + .send(CreateUser { + name: name.to_owned(), + }) .from_err() - .and_then(|res| { - match res { - Ok(user) => Ok(HttpResponse::Ok().json(user)), - Err(_) => Ok(HttpResponse::InternalServerError().into()) - } + .and_then(|res| match res { + Ok(user) => Ok(HttpResponse::Ok().json(user)), + Err(_) => Ok(HttpResponse::InternalServerError().into()), }) .responder() } @@ -57,8 +58,9 @@ fn main() { App::with_state(State{db: addr.clone()}) // enable logger .middleware(middleware::Logger::default()) - .resource("/{name}", |r| r.method(http::Method::GET).a(index))}) - .bind("127.0.0.1:8080").unwrap() + .resource("/{name}", |r| r.method(http::Method::GET).a(index)) + }).bind("127.0.0.1:8080") + .unwrap() .start(); let _ = sys.run(); diff --git a/redis-session/Cargo.toml b/redis-session/Cargo.toml index 80db31a..e8dbe81 100644 --- a/redis-session/Cargo.toml +++ b/redis-session/Cargo.toml @@ -7,5 +7,5 @@ workspace = "../" [dependencies] env_logger = "0.5" actix = "0.5" -actix-web = "^0.5" -actix-redis = { version = "0.3", features = ["web"] } +actix-web = "^0.6" +actix-redis = { version = "0.4", features = ["web"] } diff --git a/redis-session/src/main.rs b/redis-session/src/main.rs index 02d8d56..e996814 100644 --- a/redis-session/src/main.rs +++ b/redis-session/src/main.rs @@ -7,11 +7,11 @@ extern crate actix_web; extern crate env_logger; use actix_redis::RedisSessionBackend; -use actix_web::middleware::{Logger, RequestSession, SessionStorage}; -use actix_web::{server, App, HttpRequest, HttpResponse, Result}; +use actix_web::middleware::session::{RequestSession, SessionStorage}; +use actix_web::{middleware, server, App, HttpRequest, HttpResponse, Result}; /// simple handler -fn index(mut req: HttpRequest) -> Result { +fn index(req: HttpRequest) -> Result { println!("{:?}", req); // session @@ -33,7 +33,7 @@ fn main() { server::new(|| { App::new() // enable logger - .middleware(Logger::default()) + .middleware(middleware::Logger::default()) // redis session middleware .middleware(SessionStorage::new( RedisSessionBackend::new("127.0.0.1:6379", &[0; 32]) diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..6db67ed --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,5 @@ +max_width = 89 +reorder_imports = true +wrap_comments = true +fn_args_density = "Compressed" +#use_small_heuristics = false diff --git a/state/Cargo.toml b/state/Cargo.toml index 9529e9c..c031e7f 100644 --- a/state/Cargo.toml +++ b/state/Cargo.toml @@ -8,4 +8,4 @@ workspace = "../" futures = "0.1" env_logger = "0.5" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" diff --git a/state/src/main.rs b/state/src/main.rs index 4403231..309de26 100644 --- a/state/src/main.rs +++ b/state/src/main.rs @@ -3,11 +3,11 @@ //! that is shared across all handlers within same Application. //! And individual handler can have state. //! -//! > **Note**: http server accepts an application factory rather than an application -//! > instance. Http server constructs an application instance for each thread, -//! > thus application state -//! > must be constructed multiple times. If you want to share state between different -//! > threads, a shared object should be used, e.g. `Arc`. +//! > **Note**: http server accepts an application factory rather than an +//! application > instance. Http server constructs an application instance for +//! each thread, > thus application state +//! > must be constructed multiple times. If you want to share state between +//! different > threads, a shared object should be used, e.g. `Arc`. //! //! Check [user guide](https://actix.rs/book/actix-web/sec-2-application.html) for more info. @@ -17,8 +17,7 @@ extern crate env_logger; use std::cell::Cell; -use actix::prelude::*; -use actix_web::{http, middleware, server, ws, App, HttpRequest, HttpResponse}; +use actix_web::{middleware, server, App, HttpRequest, HttpResponse}; /// Application state struct AppState { @@ -30,7 +29,10 @@ fn index(req: HttpRequest) -> HttpResponse { println!("{:?}", req); req.state().counter.set(req.state().counter.get() + 1); - HttpResponse::Ok().body(format!("Num of requests: {}", req.state().counter.get())) + HttpResponse::Ok().body(format!( + "Num of requests: {}", + req.state().counter.get() + )) } fn main() { diff --git a/template_askama/Cargo.toml b/template_askama/Cargo.toml index 16798ee..8648253 100644 --- a/template_askama/Cargo.toml +++ b/template_askama/Cargo.toml @@ -7,8 +7,8 @@ workspace = "../" [dependencies] env_logger = "0.5" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" askama = "0.6" [build-dependencies] -askama = "0.6" +askama = "0.6" \ No newline at end of file diff --git a/template_askama/src/main.rs b/template_askama/src/main.rs index 097f18b..08683ad 100644 --- a/template_askama/src/main.rs +++ b/template_askama/src/main.rs @@ -3,8 +3,9 @@ extern crate actix_web; #[macro_use] extern crate askama; -use actix::prelude::*; -use actix_web::{http, server, App, HttpRequest, HttpResponse, Result}; +use std::collections::HashMap; + +use actix_web::{http, server, App, HttpResponse, Query, Result}; use askama::Template; #[derive(Template)] @@ -18,8 +19,8 @@ struct UserTemplate<'a> { #[template(path = "index.html")] struct Index; -fn index(req: HttpRequest) -> Result { - let s = if let Some(name) = req.query().get("name") { +fn index(query: Query>) -> Result { + let s = if let Some(name) = query.get("name") { UserTemplate { name: name, text: "Welcome!", @@ -32,11 +33,12 @@ fn index(req: HttpRequest) -> Result { } fn main() { - let sys = System::new("template-askama"); + let sys = actix::System::new("template-askama"); // start http server - server::new(move || App::new().resource("/", |r| r.method(http::Method::GET).f(index))) - .bind("0.0.0.0:8080") + server::new(move || { + App::new().resource("/", |r| r.method(http::Method::GET).with(index)) + }).bind("0.0.0.0:8080") .unwrap() .start(); diff --git a/template_tera/Cargo.toml b/template_tera/Cargo.toml index 206afa3..c228866 100644 --- a/template_tera/Cargo.toml +++ b/template_tera/Cargo.toml @@ -7,5 +7,5 @@ workspace = "../" [dependencies] env_logger = "0.5" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" tera = "*" diff --git a/template_tera/src/main.rs b/template_tera/src/main.rs index e1a738d..3ceb7d5 100644 --- a/template_tera/src/main.rs +++ b/template_tera/src/main.rs @@ -4,28 +4,33 @@ extern crate env_logger; #[macro_use] extern crate tera; -use actix_web::{ - http, error, middleware, server, App, HttpRequest, HttpResponse, Error}; +use std::collections::HashMap; +use actix_web::{error, http, middleware, server, App, Error, HttpResponse, Query, State}; -struct State { - template: tera::Tera, // <- store tera template in application state +struct AppState { + template: tera::Tera, // <- store tera template in application state } -fn index(req: HttpRequest) -> Result { - let s = if let Some(name) = req.query().get("name") { // <- submitted form +fn index( + state: State, query: Query>, +) -> Result { + let s = if let Some(name) = query.get("name") { + // <- submitted form let mut ctx = tera::Context::new(); ctx.add("name", &name.to_owned()); ctx.add("text", &"Welcome!".to_owned()); - req.state().template.render("user.html", &ctx) + state + .template + .render("user.html", &ctx) .map_err(|_| error::ErrorInternalServerError("Template error"))? } else { - req.state().template.render("index.html", &tera::Context::new()) + state + .template + .render("index.html", &tera::Context::new()) .map_err(|_| error::ErrorInternalServerError("Template error"))? }; - Ok(HttpResponse::Ok() - .content_type("text/html") - .body(s)) + Ok(HttpResponse::Ok().content_type("text/html").body(s)) } fn main() { @@ -34,13 +39,15 @@ fn main() { let sys = actix::System::new("tera-example"); server::new(|| { - let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")); + let tera = + compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")); - App::with_state(State{template: tera}) + App::with_state(AppState{template: tera}) // enable logger .middleware(middleware::Logger::default()) - .resource("/", |r| r.method(http::Method::GET).f(index))}) - .bind("127.0.0.1:8080").unwrap() + .resource("/", |r| r.method(http::Method::GET).with2(index)) + }).bind("127.0.0.1:8080") + .unwrap() .start(); println!("Started http server: 127.0.0.1:8080"); diff --git a/tls/Cargo.toml b/tls/Cargo.toml index e77d8db..4a689a9 100644 --- a/tls/Cargo.toml +++ b/tls/Cargo.toml @@ -11,5 +11,5 @@ path = "src/main.rs" [dependencies] env_logger = "0.5" actix = "0.5" -actix-web = { version = "^0.5", features=["alpn"] } +actix-web = { version = "^0.6", features=["alpn"] } openssl = { version="0.10" } diff --git a/tls/src/main.rs b/tls/src/main.rs index 479ef8c..8e9d720 100644 --- a/tls/src/main.rs +++ b/tls/src/main.rs @@ -4,17 +4,15 @@ extern crate actix_web; extern crate env_logger; extern crate openssl; -use openssl::ssl::{SslMethod, SslAcceptor, SslFiletype}; -use actix_web::{ - http, middleware, server, App, HttpRequest, HttpResponse, Error}; - +use actix_web::{http, middleware, server, App, Error, HttpRequest, HttpResponse}; +use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod}; /// simple handle fn index(req: HttpRequest) -> Result { println!("{:?}", req); Ok(HttpResponse::Ok() - .content_type("text/plain") - .body("Welcome!")) + .content_type("text/plain") + .body("Welcome!")) } fn main() { @@ -26,11 +24,15 @@ fn main() { // load ssl keys let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap(); - builder.set_private_key_file("key.pem", SslFiletype::PEM).unwrap(); - builder.set_certificate_chain_file("cert.pem").unwrap(); + builder + .set_private_key_file("key.pem", SslFiletype::PEM) + .unwrap(); + builder + .set_certificate_chain_file("cert.pem") + .unwrap(); - server::new( - || App::new() + server::new(|| { + App::new() // enable logger .middleware(middleware::Logger::default()) // register simple handler, handle all methods @@ -40,9 +42,10 @@ fn main() { HttpResponse::Found() .header("LOCATION", "/index.html") .finish() - }))) - .bind("127.0.0.1:8443").unwrap() - .start_ssl(builder).unwrap(); + })) + }).bind_ssl("127.0.0.1:8443", builder) + .unwrap() + .start(); println!("Started http server: 127.0.0.1:8443"); let _ = sys.run(); diff --git a/unix-socket/Cargo.toml b/unix-socket/Cargo.toml index ea0dfde..af3d325 100644 --- a/unix-socket/Cargo.toml +++ b/unix-socket/Cargo.toml @@ -7,5 +7,5 @@ workspace = "../" [dependencies] env_logger = "0.5" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" tokio-uds = "0.1" diff --git a/unix-socket/src/main.rs b/unix-socket/src/main.rs index c307184..e583e5e 100644 --- a/unix-socket/src/main.rs +++ b/unix-socket/src/main.rs @@ -7,7 +7,6 @@ use actix::*; use actix_web::{middleware, server, App, HttpRequest}; use tokio_uds::UnixListener; - fn index(_req: HttpRequest) -> &'static str { "Hello world!" } @@ -17,15 +16,15 @@ fn main() { env_logger::init(); let sys = actix::System::new("unix-socket"); - let listener = UnixListener::bind( - "/tmp/actix-uds.socket", Arbiter::handle()).expect("bind failed"); - server::new( - || App::new() + let listener = UnixListener::bind("/tmp/actix-uds.socket", Arbiter::handle()) + .expect("bind failed"); + server::new(|| { + App::new() // enable logger .middleware(middleware::Logger::default()) .resource("/index.html", |r| r.f(|_| "Hello world!")) - .resource("/", |r| r.f(index))) - .start_incoming(listener.incoming(), false); + .resource("/", |r| r.f(index)) + }).start_incoming(listener.incoming(), false); println!("Started http server: /tmp/actix-uds.socket"); let _ = sys.run(); diff --git a/web-cors/backend/Cargo.toml b/web-cors/backend/Cargo.toml index c41f1f7..8645ab6 100644 --- a/web-cors/backend/Cargo.toml +++ b/web-cors/backend/Cargo.toml @@ -11,7 +11,7 @@ serde_json = "1.0" http = "0.1" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" dotenv = "0.10" env_logger = "0.5" futures = "0.1" diff --git a/web-cors/backend/src/main.rs b/web-cors/backend/src/main.rs index 7a97ed2..7290eaf 100644 --- a/web-cors/backend/src/main.rs +++ b/web-cors/backend/src/main.rs @@ -1,36 +1,45 @@ -#[macro_use] extern crate serde_derive; -extern crate serde; -extern crate serde_json; -extern crate futures; +#[macro_use] +extern crate serde_derive; extern crate actix; extern crate actix_web; extern crate env_logger; +extern crate futures; +extern crate serde; +extern crate serde_json; +use actix_web::{http::{header, Method}, + middleware, + middleware::cors::Cors, + server, + App}; use std::env; -use actix_web::{server, App, http::{header, Method}, middleware, middleware::cors::Cors}; mod user; use user::info; - fn main() { env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); let sys = actix::System::new("Actix-web-CORS"); - server::new( - move || App::new() + server::new(move || { + App::new() .middleware(middleware::Logger::default()) - .configure(|app| Cors::for_app(app) - .allowed_origin("http://localhost:1234") - .allowed_methods(vec!["GET", "POST"]) - .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT]) - .allowed_header(header::CONTENT_TYPE) - .max_age(3600) - .resource("/user/info", |r| { r.method(Method::POST).with(info);}) - .register())) - .bind("127.0.0.1:8000").unwrap() + .configure(|app| { + Cors::for_app(app) + .allowed_origin("http://localhost:1234") + .allowed_methods(vec!["GET", "POST"]) + .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT]) + .allowed_header(header::CONTENT_TYPE) + .max_age(3600) + .resource("/user/info", |r| { + r.method(Method::POST).with(info); + }) + .register() + }) + }).bind("127.0.0.1:8000") + .unwrap() .shutdown_timeout(2) .start(); diff --git a/web-cors/backend/src/user.rs b/web-cors/backend/src/user.rs index 1a06004..aca44cb 100644 --- a/web-cors/backend/src/user.rs +++ b/web-cors/backend/src/user.rs @@ -1,6 +1,6 @@ -use actix_web::{Result, Json}; +use actix_web::{Json, Result}; -#[derive(Deserialize,Serialize, Debug)] +#[derive(Deserialize, Serialize, Debug)] pub struct Info { username: String, email: String, @@ -10,11 +10,10 @@ pub struct Info { pub fn info(info: Json) -> Result> { println!("=========={:?}=========", info); - Ok(Json(Info{ - username: info.username.clone(), - email: info.email.clone(), - password: info.password.clone(), - confirm_password: info.confirm_password.clone(), - })) + Ok(Json(Info { + username: info.username.clone(), + email: info.email.clone(), + password: info.password.clone(), + confirm_password: info.confirm_password.clone(), + })) } - diff --git a/websocket-chat/Cargo.toml b/websocket-chat/Cargo.toml index 9ebc8a6..2542a51 100644 --- a/websocket-chat/Cargo.toml +++ b/websocket-chat/Cargo.toml @@ -26,4 +26,4 @@ serde_json = "1.0" serde_derive = "1.0" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" diff --git a/websocket-chat/src/client.rs b/websocket-chat/src/client.rs index e2e6a7c..afe9b1d 100644 --- a/websocket-chat/src/client.rs +++ b/websocket-chat/src/client.rs @@ -1,26 +1,27 @@ -#[macro_use] extern crate actix; -extern crate bytes; +#[macro_use] +extern crate actix; extern crate byteorder; +extern crate bytes; extern crate futures; -extern crate tokio_io; -extern crate tokio_core; extern crate serde; extern crate serde_json; -#[macro_use] extern crate serde_derive; +extern crate tokio_core; +extern crate tokio_io; +#[macro_use] +extern crate serde_derive; -use std::{io, net, process, thread}; +use actix::prelude::*; +use futures::Future; use std::str::FromStr; use std::time::Duration; -use futures::Future; -use tokio_io::AsyncRead; -use tokio_io::io::WriteHalf; -use tokio_io::codec::FramedRead; +use std::{io, net, process, thread}; use tokio_core::net::TcpStream; -use actix::prelude::*; +use tokio_io::codec::FramedRead; +use tokio_io::io::WriteHalf; +use tokio_io::AsyncRead; mod codec; - fn main() { let sys = actix::System::new("chat-client"); @@ -31,22 +32,28 @@ fn main() { .and_then(|stream| { let addr: Addr = ChatClient::create(|ctx| { let (r, w) = stream.split(); - ChatClient::add_stream(FramedRead::new(r, codec::ClientChatCodec), ctx); - ChatClient{ + ChatClient::add_stream( + FramedRead::new(r, codec::ClientChatCodec), + ctx, + ); + ChatClient { framed: actix::io::FramedWrite::new( - w, codec::ClientChatCodec, ctx)}}); + w, + codec::ClientChatCodec, + ctx, + ), + } + }); // start console loop - thread::spawn(move|| { - loop { - let mut cmd = String::new(); - if io::stdin().read_line(&mut cmd).is_err() { - println!("error"); - return - } - - addr.do_send(ClientCommand(cmd)); + thread::spawn(move || loop { + let mut cmd = String::new(); + if io::stdin().read_line(&mut cmd).is_err() { + println!("error"); + return; } + + addr.do_send(ClientCommand(cmd)); }); futures::future::ok(()) @@ -54,14 +61,13 @@ fn main() { .map_err(|e| { println!("Can not connect to server: {}", e); process::exit(1) - }) + }), ); println!("Running chat client"); sys.run(); } - struct ChatClient { framed: actix::io::FramedWrite, codec::ClientChatCodec>, } @@ -103,7 +109,7 @@ impl Handler for ChatClient { fn handle(&mut self, msg: ClientCommand, _: &mut Context) { let m = msg.0.trim(); if m.is_empty() { - return + return; } // we check for /sss type of messages @@ -112,18 +118,20 @@ impl Handler for ChatClient { match v[0] { "/list" => { self.framed.write(codec::ChatRequest::List); - }, + } "/join" => { if v.len() == 2 { - self.framed.write(codec::ChatRequest::Join(v[1].to_owned())); + self.framed + .write(codec::ChatRequest::Join(v[1].to_owned())); } else { println!("!!! room name is required"); } - }, + } _ => println!("!!! unknown command"), } } else { - self.framed.write(codec::ChatRequest::Message(m.to_owned())); + self.framed + .write(codec::ChatRequest::Message(m.to_owned())); } } } @@ -131,7 +139,6 @@ impl Handler for ChatClient { /// Server communication impl StreamHandler for ChatClient { - fn handle(&mut self, msg: codec::ChatResponse, _: &mut Context) { match msg { codec::ChatResponse::Message(ref msg) => { diff --git a/websocket-chat/src/codec.rs b/websocket-chat/src/codec.rs index 0363824..0670d6a 100644 --- a/websocket-chat/src/codec.rs +++ b/websocket-chat/src/codec.rs @@ -1,13 +1,13 @@ #![allow(dead_code)] -use std::io; +use byteorder::{BigEndian, ByteOrder}; +use bytes::{BufMut, BytesMut}; use serde_json as json; -use byteorder::{BigEndian , ByteOrder}; -use bytes::{BytesMut, BufMut}; -use tokio_io::codec::{Encoder, Decoder}; +use std::io; +use tokio_io::codec::{Decoder, Encoder}; /// Client request #[derive(Serialize, Deserialize, Debug, Message)] -#[serde(tag="cmd", content="data")] +#[serde(tag = "cmd", content = "data")] pub enum ChatRequest { /// List rooms List, @@ -16,12 +16,12 @@ pub enum ChatRequest { /// Send message Message(String), /// Ping - Ping + Ping, } /// Server response #[derive(Serialize, Deserialize, Debug, Message)] -#[serde(tag="cmd", content="data")] +#[serde(tag = "cmd", content = "data")] pub enum ChatResponse { Ping, @@ -38,15 +38,14 @@ pub enum ChatResponse { /// Codec for Client -> Server transport pub struct ChatCodec; -impl Decoder for ChatCodec -{ +impl Decoder for ChatCodec { type Item = ChatRequest; type Error = io::Error; fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { let size = { if src.len() < 2 { - return Ok(None) + return Ok(None); } BigEndian::read_u16(src.as_ref()) as usize }; @@ -61,12 +60,13 @@ impl Decoder for ChatCodec } } -impl Encoder for ChatCodec -{ +impl Encoder for ChatCodec { type Item = ChatResponse; type Error = io::Error; - fn encode(&mut self, msg: ChatResponse, dst: &mut BytesMut) -> Result<(), Self::Error> { + fn encode( + &mut self, msg: ChatResponse, dst: &mut BytesMut, + ) -> Result<(), Self::Error> { let msg = json::to_string(&msg).unwrap(); let msg_ref: &[u8] = msg.as_ref(); @@ -78,19 +78,17 @@ impl Encoder for ChatCodec } } - /// Codec for Server -> Client transport pub struct ClientChatCodec; -impl Decoder for ClientChatCodec -{ +impl Decoder for ClientChatCodec { type Item = ChatResponse; type Error = io::Error; fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { let size = { if src.len() < 2 { - return Ok(None) + return Ok(None); } BigEndian::read_u16(src.as_ref()) as usize }; @@ -105,12 +103,13 @@ impl Decoder for ClientChatCodec } } -impl Encoder for ClientChatCodec -{ +impl Encoder for ClientChatCodec { type Item = ChatRequest; type Error = io::Error; - fn encode(&mut self, msg: ChatRequest, dst: &mut BytesMut) -> Result<(), Self::Error> { + fn encode( + &mut self, msg: ChatRequest, dst: &mut BytesMut, + ) -> Result<(), Self::Error> { let msg = json::to_string(&msg).unwrap(); let msg_ref: &[u8] = msg.as_ref(); diff --git a/websocket-chat/src/main.rs b/websocket-chat/src/main.rs index 5cd3e6e..be9dfa7 100644 --- a/websocket-chat/src/main.rs +++ b/websocket-chat/src/main.rs @@ -1,14 +1,15 @@ #![allow(unused_variables)] -extern crate rand; -extern crate bytes; extern crate byteorder; -extern crate futures; -extern crate tokio_io; -extern crate tokio_core; +extern crate bytes; extern crate env_logger; +extern crate futures; +extern crate rand; extern crate serde; extern crate serde_json; -#[macro_use] extern crate serde_derive; +extern crate tokio_core; +extern crate tokio_io; +#[macro_use] +extern crate serde_derive; #[macro_use] extern crate actix; @@ -18,14 +19,15 @@ use std::time::Instant; use actix::*; use actix_web::server::HttpServer; -use actix_web::{http, fs, ws, App, HttpRequest, HttpResponse, Error}; +use actix_web::ws::WsWriter; +use actix_web::{fs, http, ws, App, Error, HttpRequest, HttpResponse}; mod codec; mod server; mod session; -/// This is our websocket route state, this state is shared with all route instances -/// via `HttpContext::state()` +/// This is our websocket route state, this state is shared with all route +/// instances via `HttpContext::state()` struct WsChatSessionState { addr: Addr, } @@ -38,13 +40,16 @@ fn chat_route(req: HttpRequest) -> Result = ctx.address(); - ctx.state().addr.send(server::Connect{addr: addr.recipient()}) + ctx.state() + .addr + .send(server::Connect { + addr: addr.recipient(), + }) .into_actor(self) .then(|res, act, ctx| { match res { @@ -73,12 +82,15 @@ impl Actor for WsChatSession { _ => ctx.stop(), } fut::ok(()) - }).wait(ctx); + }) + .wait(ctx); } fn stopping(&mut self, ctx: &mut Self::Context) -> Running { // notify chat server - ctx.state().addr.do_send(server::Disconnect{id: self.id}); + ctx.state() + .addr + .do_send(server::Disconnect { id: self.id }); Running::Stop } } @@ -94,7 +106,6 @@ impl Handler for WsChatSession { /// WebSocket message handler impl StreamHandler for WsChatSession { - fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { println!("WEBSOCKET MESSAGE: {:?}", msg); match msg { @@ -107,9 +118,12 @@ impl StreamHandler for WsChatSession { let v: Vec<&str> = m.splitn(2, ' ').collect(); match v[0] { "/list" => { - // Send ListRooms message to chat server and wait for response + // Send ListRooms message to chat server and wait for + // response println!("List rooms"); - ctx.state().addr.send(server::ListRooms) + ctx.state() + .addr + .send(server::ListRooms) .into_actor(self) .then(|res, _, ctx| { match res { @@ -117,33 +131,36 @@ impl StreamHandler for WsChatSession { for room in rooms { ctx.text(room); } - }, + } _ => println!("Something is wrong"), } fut::ok(()) - }).wait(ctx) + }) + .wait(ctx) // .wait(ctx) pauses all events in context, // so actor wont receive any new messages until it get list // of rooms back - }, + } "/join" => { if v.len() == 2 { self.room = v[1].to_owned(); - ctx.state().addr.do_send( - server::Join{id: self.id, name: self.room.clone()}); + ctx.state().addr.do_send(server::Join { + id: self.id, + name: self.room.clone(), + }); ctx.text("joined"); } else { ctx.text("!!! room name is required"); } - }, + } "/name" => { if v.len() == 2 { self.name = Some(v[1].to_owned()); } else { ctx.text("!!! name is required"); } - }, + } _ => ctx.text(format!("!!! unknown command: {:?}", m)), } } else { @@ -153,14 +170,14 @@ impl StreamHandler for WsChatSession { m.to_owned() }; // send message to chat server - ctx.state().addr.do_send( - server::Message{id: self.id, - msg: msg, - room: self.room.clone()}) + ctx.state().addr.do_send(server::Message { + id: self.id, + msg: msg, + room: self.room.clone(), + }) } - }, - ws::Message::Binary(bin) => - println!("Unexpected binary"), + } + ws::Message::Binary(bin) => println!("Unexpected binary"), ws::Message::Close(_) => { ctx.stop(); } @@ -177,19 +194,19 @@ fn main() { // Start tcp server in separate thread let srv = server.clone(); - Arbiter::new("tcp-server").do_send::( - msgs::Execute::new(move || { - session::TcpServer::new("127.0.0.1:12345", srv); - Ok(()) - })); + Arbiter::new("tcp-server").do_send::(msgs::Execute::new(move || { + session::TcpServer::new("127.0.0.1:12345", srv); + Ok(()) + })); // Create Http server with websocket support - HttpServer::new( - move || { - // Websocket sessions state - let state = WsChatSessionState { addr: server.clone() }; + HttpServer::new(move || { + // Websocket sessions state + let state = WsChatSessionState { + addr: server.clone(), + }; - App::with_state(state) + App::with_state(state) // redirect to websocket.html .resource("/", |r| r.method(http::Method::GET).f(|_| { HttpResponse::Found() @@ -200,8 +217,8 @@ fn main() { .resource("/ws/", |r| r.route().f(chat_route)) // static resources .handler("/static/", fs::StaticFiles::new("static/")) - }) - .bind("127.0.0.1:8080").unwrap() + }).bind("127.0.0.1:8080") + .unwrap() .start(); println!("Started http server: 127.0.0.1:8080"); diff --git a/websocket-chat/src/server.rs b/websocket-chat/src/server.rs index 8b735b8..a69c49a 100644 --- a/websocket-chat/src/server.rs +++ b/websocket-chat/src/server.rs @@ -2,10 +2,10 @@ //! And manages available rooms. Peers send messages to other peers in same //! room through `ChatServer`. +use actix::prelude::*; +use rand::{self, Rng, ThreadRng}; use std::cell::RefCell; use std::collections::{HashMap, HashSet}; -use rand::{self, Rng, ThreadRng}; -use actix::prelude::*; use session; @@ -51,8 +51,8 @@ pub struct Join { pub name: String, } -/// `ChatServer` manages chat rooms and responsible for coordinating chat session. -/// implementation is super primitive +/// `ChatServer` manages chat rooms and responsible for coordinating chat +/// session. implementation is super primitive pub struct ChatServer { sessions: HashMap>, rooms: HashMap>, @@ -112,7 +112,10 @@ impl Handler for ChatServer { self.sessions.insert(id, msg.addr); // auto join session to Main room - self.rooms.get_mut(&"Main".to_owned()).unwrap().insert(id); + self.rooms + .get_mut(&"Main".to_owned()) + .unwrap() + .insert(id); // send id back id @@ -174,7 +177,7 @@ impl Handler for ChatServer { type Result = (); fn handle(&mut self, msg: Join, _: &mut Context) { - let Join {id, name} = msg; + let Join { id, name } = msg; let mut rooms = Vec::new(); // remove session from all rooms diff --git a/websocket-chat/src/session.rs b/websocket-chat/src/session.rs index 7f28c6a..d53ea2a 100644 --- a/websocket-chat/src/session.rs +++ b/websocket-chat/src/session.rs @@ -1,19 +1,18 @@ //! `ClientSession` is an actor, it manages peer tcp connection and //! proxies commands from peer to `ChatServer`. -use std::{io, net}; -use std::str::FromStr; -use std::time::{Instant, Duration}; use futures::Stream; -use tokio_io::AsyncRead; -use tokio_io::io::WriteHalf; +use std::str::FromStr; +use std::time::{Duration, Instant}; +use std::{io, net}; +use tokio_core::net::{TcpListener, TcpStream}; use tokio_io::codec::FramedRead; -use tokio_core::net::{TcpStream, TcpListener}; +use tokio_io::io::WriteHalf; +use tokio_io::AsyncRead; use actix::prelude::*; +use codec::{ChatCodec, ChatRequest, ChatResponse}; use server::{self, ChatServer}; -use codec::{ChatRequest, ChatResponse, ChatCodec}; - /// Chat server sends this messages to session #[derive(Message)] @@ -25,7 +24,8 @@ pub struct ChatSession { id: usize, /// this is address of chat server addr: Addr, - /// Client must send ping at least once per 10 seconds, otherwise we drop connection. + /// Client must send ping at least once per 10 seconds, otherwise we drop + /// connection. hb: Instant, /// joined room room: String, @@ -46,7 +46,10 @@ impl Actor for ChatSession { // future within context, but context waits until this future resolves // before processing any other events. let addr: Addr = ctx.address(); - self.addr.send(server::Connect{addr: addr.recipient()}) + self.addr + .send(server::Connect { + addr: addr.recipient(), + }) .into_actor(self) .then(|res, act, ctx| { match res { @@ -55,12 +58,13 @@ impl Actor for ChatSession { _ => ctx.stop(), } actix::fut::ok(()) - }).wait(ctx); + }) + .wait(ctx); } fn stopping(&mut self, ctx: &mut Self::Context) -> Running { // notify chat server - self.addr.do_send(server::Disconnect{id: self.id}); + self.addr.do_send(server::Disconnect { id: self.id }); Running::Stop } } @@ -69,49 +73,54 @@ impl actix::io::WriteHandler for ChatSession {} /// To use `Framed` we have to define Io type and Codec impl StreamHandler for ChatSession { - /// This is main event loop for client requests fn handle(&mut self, msg: ChatRequest, ctx: &mut Context) { match msg { ChatRequest::List => { // Send ListRooms message to chat server and wait for response println!("List rooms"); - self.addr.send(server::ListRooms) + self.addr + .send(server::ListRooms) .into_actor(self) .then(|res, act, ctx| { match res { Ok(rooms) => { act.framed.write(ChatResponse::Rooms(rooms)); - }, + } _ => println!("Something is wrong"), } actix::fut::ok(()) - }).wait(ctx) + }) + .wait(ctx) // .wait(ctx) pauses all events in context, // so actor wont receive any new messages until it get list of rooms back - }, + } ChatRequest::Join(name) => { println!("Join to room: {}", name); self.room = name.clone(); - self.addr.do_send(server::Join{id: self.id, name: name.clone()}); + self.addr.do_send(server::Join { + id: self.id, + name: name.clone(), + }); self.framed.write(ChatResponse::Joined(name)); - }, + } ChatRequest::Message(message) => { // send message to chat server println!("Peer message: {}", message); - self.addr.do_send( - server::Message{id: self.id, - msg: message, room: - self.room.clone()}) + self.addr.do_send(server::Message { + id: self.id, + msg: message, + room: self.room.clone(), + }) } // we update heartbeat time on ping from peer - ChatRequest::Ping => - self.hb = Instant::now(), + ChatRequest::Ping => self.hb = Instant::now(), } } } -/// Handler for Message, chat server sends this message, we just send string to peer +/// Handler for Message, chat server sends this message, we just send string to +/// peer impl Handler for ChatSession { type Result = (); @@ -123,13 +132,19 @@ impl Handler for ChatSession { /// Helper methods impl ChatSession { - - pub fn new(addr: Addr, - framed: actix::io::FramedWrite, ChatCodec>) -> ChatSession { - ChatSession {id: 0, addr: addr, hb: Instant::now(), - room: "Main".to_owned(), framed: framed} + pub fn new( + addr: Addr, + framed: actix::io::FramedWrite, ChatCodec>, + ) -> ChatSession { + ChatSession { + id: 0, + addr: addr, + hb: Instant::now(), + room: "Main".to_owned(), + framed: framed, + } } - + /// helper method that sends ping to client every second. /// /// also this method check heartbeats from client @@ -141,7 +156,7 @@ impl ChatSession { println!("Client heartbeat failed, disconnecting!"); // notify chat server - act.addr.do_send(server::Disconnect{id: act.id}); + act.addr.do_send(server::Disconnect { id: act.id }); // stop actor ctx.stop(); @@ -154,7 +169,6 @@ impl ChatSession { } } - /// Define tcp server that will accept incoming tcp connection and create /// chat actors. pub struct TcpServer { @@ -169,14 +183,18 @@ impl TcpServer { // Our chat server `Server` is an actor, first we need to start it // and then add stream on incoming tcp connections to it. - // TcpListener::incoming() returns stream of the (TcpStream, net::SocketAddr) items - // So to be able to handle this events `Server` actor has to implement - // stream handler `StreamHandler<(TcpStream, net::SocketAddr), io::Error>` + // TcpListener::incoming() returns stream of the (TcpStream, net::SocketAddr) + // items So to be able to handle this events `Server` actor has to + // implement stream handler `StreamHandler<(TcpStream, + // net::SocketAddr), io::Error>` let _: () = TcpServer::create(|ctx| { - ctx.add_message_stream(listener.incoming() - .map_err(|_| ()) - .map(|(t, a)| TcpConnect(t, a))); - TcpServer{chat: chat} + ctx.add_message_stream( + listener + .incoming() + .map_err(|_| ()) + .map(|(t, a)| TcpConnect(t, a)), + ); + TcpServer { chat: chat } }); } } diff --git a/websocket/Cargo.toml b/websocket/Cargo.toml index 47a7a8c..2736d7e 100644 --- a/websocket/Cargo.toml +++ b/websocket/Cargo.toml @@ -17,4 +17,4 @@ env_logger = "*" futures = "0.1" tokio-core = "0.1" actix = "0.5" -actix-web = "^0.5" +actix-web = "^0.6" diff --git a/websocket/src/client.rs b/websocket/src/client.rs index 34ff243..15c0dcd 100644 --- a/websocket/src/client.rs +++ b/websocket/src/client.rs @@ -7,13 +7,13 @@ extern crate env_logger; extern crate futures; extern crate tokio_core; -use std::{io, thread}; use std::time::Duration; +use std::{io, thread}; use actix::*; +use actix_web::ws::WsWriter; +use actix_web::ws::{Client, ClientWriter, Message, ProtocolError}; use futures::Future; -use actix_web::ws::{Message, ProtocolError, Client, ClientWriter}; - fn main() { ::std::env::set_var("RUST_LOG", "actix_web=info"); @@ -34,25 +34,22 @@ fn main() { }); // start console loop - thread::spawn(move|| { - loop { - let mut cmd = String::new(); - if io::stdin().read_line(&mut cmd).is_err() { - println!("error"); - return - } - addr.do_send(ClientCommand(cmd)); + thread::spawn(move || loop { + let mut cmd = String::new(); + if io::stdin().read_line(&mut cmd).is_err() { + println!("error"); + return; } + addr.do_send(ClientCommand(cmd)); }); () - }) + }), ); let _ = sys.run(); } - struct ChatClient(ClientWriter); #[derive(Message)] @@ -94,11 +91,10 @@ impl Handler for ChatClient { /// Handle server websocket messages impl StreamHandler for ChatClient { - fn handle(&mut self, msg: Message, ctx: &mut Context) { match msg { Message::Text(txt) => println!("Server: {:?}", txt), - _ => () + _ => (), } } diff --git a/websocket/src/main.rs b/websocket/src/main.rs index b31ae81..69857d5 100644 --- a/websocket/src/main.rs +++ b/websocket/src/main.rs @@ -9,8 +9,8 @@ extern crate actix_web; extern crate env_logger; use actix::prelude::*; -use actix_web::{ - http, middleware, server, fs, ws, App, HttpRequest, HttpResponse, Error}; +use actix_web::ws::WsWriter; +use actix_web::{fs, http, middleware, server, ws, App, Error, HttpRequest, HttpResponse}; /// do websocket handshake and start `MyWebSocket` actor fn ws_index(r: HttpRequest) -> Result { @@ -27,7 +27,6 @@ impl Actor for MyWebSocket { /// Handler for `ws::Message` impl StreamHandler for MyWebSocket { - fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { // process websocket messages println!("WS: {:?}", msg);