1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

upgrade actix-web to 0.6

This commit is contained in:
Nikolay Kim 2018-05-08 11:08:43 -07:00
parent 27de52b5d5
commit bbeb262a5c
55 changed files with 689 additions and 518 deletions

154
Cargo.lock generated
View File

@ -22,11 +22,11 @@ dependencies = [
[[package]] [[package]]
name = "actix-redis" name = "actix-redis"
version = "0.3.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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-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-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)", "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)", "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)", "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" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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]] [[package]]
name = "backoff" name = "backoff"
version = "0.1.2" version = "0.1.2"
@ -239,7 +300,7 @@ name = "basics"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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]] [[package]]
name = "cargo_metadata" name = "cargo_metadata"
version = "0.3.3" version = "0.3.3"
@ -339,7 +409,7 @@ name = "cookie-auth"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
@ -351,7 +421,7 @@ name = "cookie-session"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
@ -475,7 +545,7 @@ name = "diesel-example"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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]] [[package]]
name = "indexmap" name = "indexmap"
version = "1.0.1" version = "1.0.1"
@ -843,7 +918,7 @@ name = "json-example"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
@ -873,7 +948,7 @@ name = "juniper-example"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "juniper 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1066,7 +1141,7 @@ name = "multipart-example"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -1275,7 +1350,7 @@ name = "protobuf-example"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1410,8 +1485,8 @@ name = "redis-session"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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-redis 0.4.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-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)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -1658,7 +1733,7 @@ name = "state"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -1729,7 +1804,7 @@ name = "template-askama"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "tera 0.11.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -1805,7 +1880,7 @@ name = "tls-example"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "openssl 0.10.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -2062,7 +2137,7 @@ name = "unix-socket"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.3" version = "0.2.3"
@ -2144,7 +2245,7 @@ name = "websocket"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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" version = "0.1.0"
dependencies = [ dependencies = [
"actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2237,8 +2338,9 @@ dependencies = [
[metadata] [metadata]
"checksum actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e767e3170dc7cdec50fe1b74d22fd9d2f4b78b97b2052a254b5acb07dae68634" "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.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 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 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" "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 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 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 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 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 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" "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 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 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 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 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 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" "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 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.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 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 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 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" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"

View File

@ -1,6 +1,7 @@
[workspace] [workspace]
members = [ members = [
"./", "./",
"async_ex1",
"basics", "basics",
"cookie-auth", "cookie-auth",
"cookie-session", "cookie-session",

View File

@ -5,7 +5,7 @@ authors = ["dowwie <dkcdkg@gmail.com>"]
[dependencies] [dependencies]
actix = "0.5.6" actix = "0.5.6"
actix-web = { version = "^0.5", features=["alpn"] } actix-web = { version = "^0.6", features=["alpn"] }
futures = "0.1" futures = "0.1"
serde = "1.0.43" serde = "1.0.43"
serde_derive = "1.0.43" serde_derive = "1.0.43"

View File

@ -1,8 +1,8 @@
// This is a contrived example intended to illustrate actix-web features. // This is a contrived example intended to illustrate actix-web features.
// *Imagine* that you have a process that involves 3 steps. The steps here // *Imagine* that you have a process that involves 3 steps. The steps here
// are dumb in that they do nothing other than call an // 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 // httpbin endpoint that returns the json that was posted to it. The intent
// is to illustrate how to chain these steps together as futures and return // here is to illustrate how to chain these steps together as futures and return
// a final result in a response. // a final result in a response.
// //
// Actix-web features illustrated here include: // Actix-web features illustrated here include:
@ -20,15 +20,15 @@ extern crate serde_derive;
extern crate serde_json; extern crate serde_json;
#[macro_use] #[macro_use]
extern crate validator_derive; extern crate validator_derive;
extern crate env_logger;
extern crate futures; extern crate futures;
extern crate validator; 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 futures::{future::ok as fut_ok, Future};
use std::collections::HashMap; use std::collections::HashMap;
use actix_web::{client, dev::Handler, http::Method, server, App, AsyncResponder, Body, Error, use std::time::Duration;
HttpMessage, HttpRequest, HttpResponse, Json, Result};
use validator::{Validate, ValidationError}; use validator::{Validate, ValidationError};
#[derive(Debug, Validate, Deserialize, Serialize)] #[derive(Debug, Validate, Deserialize, Serialize)]
@ -48,7 +48,7 @@ struct HttpBinResponse {
headers: HashMap<String, String>, headers: HashMap<String, String>,
json: SomeData, json: SomeData,
origin: String, origin: String,
url: String url: String,
} }
/// post json to httpbin, get it back in the response body, return deserialized /// post json to httpbin, get it back in the response body, return deserialized
@ -70,15 +70,18 @@ fn step_x(data: SomeData) -> Box<Future<Item = SomeData, Error = Error>> {
) )
} }
fn create_something(some_data: Json<SomeData>) -> Box<Future<Item = HttpResponse, Error = Error>> { fn create_something(
some_data: Json<SomeData>,
) -> Box<Future<Item = HttpResponse, Error = Error>> {
step_x(some_data.into_inner()) step_x(some_data.into_inner())
.and_then(|some_data_2| { .and_then(|some_data_2| {
step_x(some_data_2).and_then(|some_data_3| { 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() Ok(HttpResponse::Ok()
.content_type("application/json") .content_type("application/json")
.body(serde_json::to_string(&d).unwrap()) .body(serde_json::to_string(&d).unwrap())
.into())) .into())
})
}) })
}) })
.responder() .responder()

View File

@ -8,4 +8,4 @@ workspace = "../"
futures = "0.1" futures = "0.1"
env_logger = "0.5" env_logger = "0.5"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"

View File

@ -1,5 +1,5 @@
#![allow(unused_variables)] #![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;
extern crate actix_web; extern crate actix_web;
@ -7,12 +7,12 @@ extern crate env_logger;
extern crate futures; extern crate futures;
use futures::Stream; 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::http::{header, Method, StatusCode};
use actix_web::middleware::{self, RequestSession}; use actix_web::middleware::session::{self, RequestSession};
use futures::future::{FutureResult, result}; use actix_web::{error, fs, middleware, pred, server, App, Error, HttpRequest,
HttpResponse, Result};
use futures::future::{result, FutureResult};
use std::{env, io};
/// favicon handler /// favicon handler
fn favicon(req: HttpRequest) -> Result<fs::NamedFile> { fn favicon(req: HttpRequest) -> Result<fs::NamedFile> {
@ -40,39 +40,36 @@ fn welcome(mut req: HttpRequest) -> Result<HttpResponse> {
req.session().set("counter", counter)?; req.session().set("counter", counter)?;
} }
// response // response
Ok(HttpResponse::build(StatusCode::OK) Ok(HttpResponse::build(StatusCode::OK)
.content_type("text/html; charset=utf-8") .content_type("text/html; charset=utf-8")
.body(include_str!("../static/welcome.html"))) .body(include_str!("../static/welcome.html")))
} }
/// 404 handler /// 404 handler
fn p404(req: HttpRequest) -> Result<fs::NamedFile> { fn p404(req: HttpRequest) -> Result<fs::NamedFile> {
Ok(fs::NamedFile::open("static/404.html")? Ok(fs::NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND))
.set_status_code(StatusCode::NOT_FOUND))
} }
/// async handler /// async handler
fn index_async(req: HttpRequest) -> FutureResult<HttpResponse, Error> fn index_async(req: HttpRequest) -> FutureResult<HttpResponse, Error> {
{
println!("{:?}", req); println!("{:?}", req);
result(Ok(HttpResponse::Ok() result(Ok(HttpResponse::Ok().content_type("text/html").body(
.content_type("text/html") format!("Hello {}!", req.match_info().get("name").unwrap()),
.body(format!("Hello {}!", req.match_info().get("name").unwrap())))) )))
} }
/// handler with path parameters like `/user/{name}/` /// handler with path parameters like `/user/{name}/`
fn with_param(req: HttpRequest) -> HttpResponse fn with_param(req: HttpRequest) -> HttpResponse {
{
println!("{:?}", req); println!("{:?}", req);
HttpResponse::Ok() HttpResponse::Ok()
.content_type("test/plain") .content_type("test/plain")
.body(format!("Hello {}!", req.match_info().get("name").unwrap())) .body(format!(
"Hello {}!",
req.match_info().get("name").unwrap()
))
} }
fn main() { fn main() {
@ -86,8 +83,8 @@ fn main() {
// enable logger // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
// cookie session middleware // cookie session middleware
.middleware(middleware::SessionStorage::new( .middleware(session::SessionStorage::new(
middleware::CookieSessionBackend::signed(&[0; 32]).secure(false) session::CookieSessionBackend::signed(&[0; 32]).secure(false)
)) ))
// register favicon // register favicon
.resource("/favicon", |r| r.f(favicon)) .resource("/favicon", |r| r.f(favicon))

View File

@ -6,7 +6,7 @@ workspace = "../"
[dependencies] [dependencies]
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"
cookie = { version="0.10", features=["percent-encode", "secure"] } cookie = { version="0.10", features=["percent-encode", "secure"] }
futures = "0.1" futures = "0.1"
time = "0.1" time = "0.1"

View File

@ -1,18 +1,16 @@
use std::rc::Rc; use std::rc::Rc;
use cookie::{Cookie, CookieJar, Key}; use cookie::{Cookie, CookieJar, Key};
use futures::future::{err as FutErr, ok as FutOk, FutureResult};
use futures::Future; use futures::Future;
use futures::future::{FutureResult, err as FutErr, ok as FutOk};
use time::Duration; use time::Duration;
use actix_web::{HttpRequest, HttpResponse, Error, Result};
use actix_web::http::header::{self, HeaderValue}; use actix_web::http::header::{self, HeaderValue};
use actix_web::middleware::{Middleware, Response, Started}; use actix_web::middleware::{Middleware, Response, Started};
use actix_web::{Error, HttpRequest, HttpResponse, Result};
/// Trait provides identity service for the request. /// Trait provides identity service for the request.
pub trait RequestIdentity { pub trait RequestIdentity {
/// Return the claimed identity of the user associated request or /// Return the claimed identity of the user associated request or
/// ``None`` if no identity can be found associated with the request. /// ``None`` if no identity can be found associated with the request.
fn identity(&mut self) -> Option<&str>; fn identity(&mut self) -> Option<&str>;
@ -20,33 +18,32 @@ pub trait RequestIdentity {
/// Remember identity. /// Remember identity.
fn remember(&mut self, identity: String); 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); fn forget(&mut self);
} }
impl<S> RequestIdentity for HttpRequest<S> { impl<S> RequestIdentity for HttpRequest<S> {
fn identity(&mut self) -> Option<&str> { fn identity(&mut self) -> Option<&str> {
if let Some(id) = self.extensions().get::<IdentityBox>() { if let Some(id) = self.extensions().get::<IdentityBox>() {
return id.0.identity() return id.0.identity();
} }
None None
} }
fn remember(&mut self, identity: String) { fn remember(&mut self, identity: String) {
if let Some(id) = self.extensions().get_mut::<IdentityBox>() { if let Some(id) = self.extensions_mut().get_mut::<IdentityBox>() {
return id.0.remember(identity) return id.0.remember(identity);
} }
} }
fn forget(&mut self) { fn forget(&mut self) {
if let Some(id) = self.extensions().get_mut::<IdentityBox>() { if let Some(id) = self.extensions_mut().get_mut::<IdentityBox>() {
return id.0.forget() return id.0.forget();
} }
} }
} }
/// An identity /// An identity
pub trait Identity: 'static { pub trait Identity: 'static {
fn identity(&self) -> Option<&str>; fn identity(&self) -> Option<&str>;
@ -87,7 +84,6 @@ unsafe impl Send for IdentityBox {}
#[doc(hidden)] #[doc(hidden)]
unsafe impl Sync for IdentityBox {} unsafe impl Sync for IdentityBox {}
impl<S: 'static, T: IdentityPolicy<S>> Middleware<S> for IdentityService<T> { impl<S: 'static, T: IdentityPolicy<S>> Middleware<S> for IdentityService<T> {
fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> { fn start(&self, req: &mut HttpRequest<S>) -> Result<Started> {
let mut req = req.clone(); let mut req = req.clone();
@ -96,7 +92,7 @@ impl<S: 'static, T: IdentityPolicy<S>> Middleware<S> for IdentityService<T> {
.from_request(&mut req) .from_request(&mut req)
.then(move |res| match res { .then(move |res| match res {
Ok(id) => { Ok(id) => {
req.extensions().insert(IdentityBox(Box::new(id))); req.extensions_mut().insert(IdentityBox(Box::new(id)));
FutOk(None) FutOk(None)
} }
Err(err) => FutErr(err), Err(err) => FutErr(err),
@ -104,8 +100,10 @@ impl<S: 'static, T: IdentityPolicy<S>> Middleware<S> for IdentityService<T> {
Ok(Started::Future(Box::new(fut))) Ok(Started::Future(Box::new(fut)))
} }
fn response(&self, req: &mut HttpRequest<S>, resp: HttpResponse) -> Result<Response> { fn response(
if let Some(mut id) = req.extensions().remove::<IdentityBox>() { &self, req: &mut HttpRequest<S>, resp: HttpResponse,
) -> Result<Response> {
if let Some(mut id) = req.extensions_mut().remove::<IdentityBox>() {
id.0.write(resp) id.0.write(resp)
} else { } else {
Ok(Response::Done(resp)) Ok(Response::Done(resp))
@ -207,7 +205,7 @@ impl CookieIdentityInner {
let cookie_opt = jar.private(&self.key).get(&self.name); let cookie_opt = jar.private(&self.key).get(&self.name);
if let Some(cookie) = cookie_opt { 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<CookieIdentityInner>); pub struct CookieIdentityPolicy(Rc<CookieIdentityInner>);
impl CookieIdentityPolicy { impl CookieIdentityPolicy {
/// Construct new `CookieIdentityPolicy` instance. /// Construct new `CookieIdentityPolicy` instance.
/// ///
/// Panics if key length is less than 32 bytes. /// Panics if key length is less than 32 bytes.

View File

@ -1,15 +1,15 @@
extern crate actix; extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate cookie; extern crate cookie;
extern crate time;
extern crate futures;
extern crate env_logger; 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; mod auth;
use auth::{RequestIdentity, IdentityService, CookieIdentityPolicy}; use auth::{CookieIdentityPolicy, IdentityService, RequestIdentity};
fn index(mut req: HttpRequest) -> String { fn index(mut req: HttpRequest) -> String {
format!("Hello {}", req.identity().unwrap_or("Anonymous")) format!("Hello {}", req.identity().unwrap_or("Anonymous"))
@ -17,16 +17,12 @@ fn index(mut req: HttpRequest) -> String {
fn login(mut req: HttpRequest) -> HttpResponse { fn login(mut req: HttpRequest) -> HttpResponse {
req.remember("user1".to_owned()); req.remember("user1".to_owned());
HttpResponse::Found() HttpResponse::Found().header("location", "/").finish()
.header("location", "/")
.finish()
} }
fn logout(mut req: HttpRequest) -> HttpResponse { fn logout(mut req: HttpRequest) -> HttpResponse {
req.forget(); req.forget();
HttpResponse::Found() HttpResponse::Found().header("location", "/").finish()
.header("location", "/")
.finish()
} }
fn main() { fn main() {
@ -34,18 +30,19 @@ fn main() {
env_logger::init(); env_logger::init();
let sys = actix::System::new("cookie-auth"); let sys = actix::System::new("cookie-auth");
server::new( server::new(|| {
|| App::new() App::new()
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.middleware(IdentityService::new( .middleware(IdentityService::new(
CookieIdentityPolicy::new(&[0; 32]) CookieIdentityPolicy::new(&[0; 32])
.name("auth-example") .name("auth-example")
.secure(false))) .secure(false),
))
.resource("/login", |r| r.f(login)) .resource("/login", |r| r.f(login))
.resource("/logout", |r| r.f(logout)) .resource("/logout", |r| r.f(logout))
.resource("/", |r| r.f(index))) .resource("/", |r| r.f(index))
.bind("127.0.0.1:8080").unwrap() }).bind("127.0.0.1:8080")
.unwrap()
.start(); .start();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");

View File

@ -6,7 +6,7 @@ workspace = "../"
[dependencies] [dependencies]
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"
futures = "0.1" futures = "0.1"
time = "0.1" time = "0.1"
env_logger = "0.5" env_logger = "0.5"

View File

@ -10,12 +10,12 @@ extern crate actix_web;
extern crate env_logger; extern crate env_logger;
extern crate futures; extern crate futures;
use actix_web::middleware::{self, RequestSession}; use actix_web::middleware::session::{self, RequestSession};
use actix_web::{server, App, HttpRequest, Result}; use actix_web::{middleware, server, App, HttpRequest, Result};
use std::env; use std::env;
/// simple index handler with session /// simple index handler with session
fn index(mut req: HttpRequest) -> Result<&'static str> { fn index(req: HttpRequest) -> Result<&'static str> {
println!("{:?}", req); println!("{:?}", req);
// RequestSession trait is used for session access // RequestSession trait is used for session access
@ -41,8 +41,8 @@ fn main() {
// enable logger // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
// cookie session middleware // cookie session middleware
.middleware(middleware::SessionStorage::new( .middleware(session::SessionStorage::new(
middleware::CookieSessionBackend::signed(&[0; 32]).secure(false) session::CookieSessionBackend::signed(&[0; 32]).secure(false)
)) ))
.resource("/", |r| r.f(index)) .resource("/", |r| r.f(index))
}).bind("127.0.0.1:8080") }).bind("127.0.0.1:8080")

View File

@ -7,7 +7,7 @@ workspace = "../"
[dependencies] [dependencies]
env_logger = "0.5" env_logger = "0.5"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"
futures = "0.1" futures = "0.1"
uuid = { version = "0.5", features = ["serde", "v4"] } uuid = { version = "0.5", features = ["serde", "v4"] }

View File

@ -1,10 +1,10 @@
//! Db executor actor //! Db executor actor
use uuid;
use diesel;
use actix_web::*;
use actix::prelude::*; use actix::prelude::*;
use actix_web::*;
use diesel;
use diesel::prelude::*; use diesel::prelude::*;
use diesel::r2d2::{Pool, ConnectionManager}; use diesel::r2d2::{ConnectionManager, Pool};
use uuid;
use models; use models;
use schema; use schema;
@ -12,8 +12,8 @@ use schema;
/// This is db executor actor. We are going to run 3 of them in parallel. /// This is db executor actor. We are going to run 3 of them in parallel.
pub struct DbExecutor(pub Pool<ConnectionManager<SqliteConnection>>); pub struct DbExecutor(pub Pool<ConnectionManager<SqliteConnection>>);
/// This is only message that this actor can handle, but it is easy to extend number of /// This is only message that this actor can handle, but it is easy to extend
/// messages. /// number of messages.
pub struct CreateUser { pub struct CreateUser {
pub name: String, pub name: String,
} }

View File

@ -1,28 +1,28 @@
//! Actix web diesel example //! Actix web diesel example
//! //!
//! Diesel does not support tokio, so we have to run it in separate threads. //! 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. //! Actix supports sync actors by default, so we going to create sync actor
//! Technically sync actors are worker style actors, multiple of them //! that use diesel. Technically sync actors are worker style actors, multiple
//! can run in parallel and process messages from same queue. //! of them can run in parallel and process messages from same queue.
extern crate serde; extern crate serde;
extern crate serde_json; extern crate serde_json;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
#[macro_use] #[macro_use]
extern crate diesel; extern crate diesel;
extern crate r2d2;
extern crate uuid;
extern crate futures;
extern crate actix; extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate env_logger; extern crate env_logger;
extern crate futures;
extern crate r2d2;
extern crate uuid;
use actix::prelude::*; use actix::prelude::*;
use actix_web::{http, server, middleware, use actix_web::{http, middleware, server, App, AsyncResponder, FutureResponse,
App, Path, State, HttpResponse, AsyncResponder, FutureResponse}; HttpResponse, Path, State};
use diesel::prelude::*; use diesel::prelude::*;
use diesel::r2d2::{ Pool, ConnectionManager }; use diesel::r2d2::{ConnectionManager, Pool};
use futures::future::Future; use futures::future::Future;
mod db; mod db;
@ -31,7 +31,6 @@ mod schema;
use db::{CreateUser, DbExecutor}; use db::{CreateUser, DbExecutor};
/// State with DbExecutor address /// State with DbExecutor address
struct AppState { struct AppState {
db: Addr<Syn, DbExecutor>, db: Addr<Syn, DbExecutor>,
@ -40,13 +39,15 @@ struct AppState {
/// Async request handler /// Async request handler
fn index(name: Path<String>, state: State<AppState>) -> FutureResponse<HttpResponse> { fn index(name: Path<String>, state: State<AppState>) -> FutureResponse<HttpResponse> {
// send async `CreateUser` message to a `DbExecutor` // 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() .from_err()
.and_then(|res| { .and_then(|res| match res {
match res { Ok(user) => Ok(HttpResponse::Ok().json(user)),
Ok(user) => Ok(HttpResponse::Ok().json(user)), Err(_) => Ok(HttpResponse::InternalServerError().into()),
Err(_) => Ok(HttpResponse::InternalServerError().into())
}
}) })
.responder() .responder()
} }
@ -58,19 +59,20 @@ fn main() {
// Start 3 db executor actors // Start 3 db executor actors
let manager = ConnectionManager::<SqliteConnection>::new("test.db"); let manager = ConnectionManager::<SqliteConnection>::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 || { let addr = SyncArbiter::start(3, move || DbExecutor(pool.clone()));
DbExecutor(pool.clone())
});
// Start http server // Start http server
server::new(move || { server::new(move || {
App::with_state(AppState{db: addr.clone()}) App::with_state(AppState{db: addr.clone()})
// enable logger // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.resource("/{name}", |r| r.method(http::Method::GET).with2(index))}) .resource("/{name}", |r| r.method(http::Method::GET).with2(index))
.bind("127.0.0.1:8080").unwrap() }).bind("127.0.0.1:8080")
.unwrap()
.start(); .start();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");

View File

@ -7,4 +7,4 @@ workspace = "../"
[dependencies] [dependencies]
env_logger = "0.5" env_logger = "0.5"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"

View File

@ -2,8 +2,7 @@ extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate env_logger; extern crate env_logger;
use actix_web::{App, HttpRequest, server, middleware}; use actix_web::{middleware, server, App, HttpRequest};
fn index(_req: HttpRequest) -> &'static str { fn index(_req: HttpRequest) -> &'static str {
"Hello world!" "Hello world!"
@ -14,13 +13,14 @@ fn main() {
env_logger::init(); env_logger::init();
let sys = actix::System::new("hello-world"); let sys = actix::System::new("hello-world");
server::new( server::new(|| {
|| App::new() App::new()
// enable logger // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.resource("/index.html", |r| r.f(|_| "Hello world!")) .resource("/index.html", |r| r.f(|_| "Hello world!"))
.resource("/", |r| r.f(index))) .resource("/", |r| r.f(index))
.bind("127.0.0.1:8080").unwrap() }).bind("127.0.0.1:8080")
.unwrap()
.start(); .start();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");

View File

@ -16,4 +16,4 @@ path = "src/server.rs"
env_logger = "0.5" env_logger = "0.5"
futures = "0.1" futures = "0.1"
actix = "0.5" actix = "0.5"
actix-web = { version = "^0.5", features=["alpn"] } actix-web = { version = "^0.6", features=["alpn"] }

View File

@ -3,8 +3,8 @@ extern crate actix_web;
extern crate env_logger; extern crate env_logger;
extern crate futures; extern crate futures;
use actix_web::{client, middleware, server, App, AsyncResponder, Body, Error, HttpMessage, use actix_web::{client, middleware, server, App, AsyncResponder, Body, Error,
HttpRequest, HttpResponse}; HttpMessage, HttpRequest, HttpResponse};
use futures::{Future, Stream}; use futures::{Future, Stream};
/// Stream client request response and then send body to a server response /// Stream client request response and then send body to a server response
@ -48,7 +48,7 @@ fn main() {
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.resource("/streaming", |r| r.f(streaming)) .resource("/streaming", |r| r.f(streaming))
.resource("/", |r| r.f(index)) .resource("/", |r| r.f(index))
}).threads(1) }).workers(1)
.bind("127.0.0.1:8080") .bind("127.0.0.1:8080")
.unwrap() .unwrap()
.start(); .start();

View File

@ -24,7 +24,7 @@ fn main() {
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.resource("/index.html", |r| r.f(|_| "Hello world!")) .resource("/index.html", |r| r.f(|_| "Hello world!"))
.resource("/", |r| r.f(index)) .resource("/", |r| r.f(index))
}).threads(1) }).workers(1)
.bind("127.0.0.1:8081") .bind("127.0.0.1:8081")
.unwrap() .unwrap()
.start(); .start();

View File

@ -15,4 +15,4 @@ serde_derive = "1.0"
json = "*" json = "*"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"

View File

@ -1,15 +1,16 @@
extern crate actix; extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate bytes; extern crate bytes;
extern crate futures;
extern crate env_logger; extern crate env_logger;
extern crate futures;
extern crate serde_json; extern crate serde_json;
#[macro_use] extern crate serde_derive; #[macro_use]
#[macro_use] extern crate json; extern crate serde_derive;
#[macro_use]
extern crate json;
use actix_web::{ use actix_web::{error, http, middleware, server, App, AsyncResponder, Error,
middleware, http, error, server, HttpMessage, HttpRequest, HttpResponse, Json};
App, AsyncResponder, HttpRequest, HttpResponse, HttpMessage, Error, Json};
use bytes::BytesMut; use bytes::BytesMut;
use futures::{Future, Stream}; use futures::{Future, Stream};
@ -22,7 +23,7 @@ struct MyObj {
} }
/// This handler uses `HttpRequest::json()` for loading json object. /// This handler uses `HttpRequest::json()` for loading json object.
fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> { fn index(req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
req.json() req.json()
.from_err() // convert all errors into `Error` .from_err() // convert all errors into `Error`
.and_then(|val: MyObj| { .and_then(|val: MyObj| {
@ -35,13 +36,13 @@ fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
/// This handler uses json extractor /// This handler uses json extractor
fn extract_item(item: Json<MyObj>) -> HttpResponse { fn extract_item(item: Json<MyObj>) -> HttpResponse {
println!("model: {:?}", &item); 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 /// This handler manually load request payload and parse json object
fn index_manual(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> { fn index_manual(req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
// HttpRequest is stream of Bytes objects // HttpRequest is stream of Bytes objects
req req
// `Future::from_err` acts like `?` in that it coerces the error type from // `Future::from_err` acts like `?` in that it coerces the error type from
@ -70,13 +71,16 @@ fn index_manual(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>>
} }
/// This handler manually load request payload and parse json-rust /// This handler manually load request payload and parse json-rust
fn index_mjsonrust(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> { fn index_mjsonrust(req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
req.concat2() req.concat2()
.from_err() .from_err()
.and_then(|body| { .and_then(|body| {
// body is loaded, now we can deserialize json-rust // body is loaded, now we can deserialize json-rust
let result = json::parse(std::str::from_utf8(&body).unwrap()); // return Result 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() Ok(HttpResponse::Ok()
.content_type("application/json") .content_type("application/json")
.body(injson.dump())) .body(injson.dump()))
@ -100,8 +104,9 @@ fn main() {
}) })
.resource("/manual", |r| r.method(http::Method::POST).f(index_manual)) .resource("/manual", |r| r.method(http::Method::POST).f(index_manual))
.resource("/mjsonrust", |r| r.method(http::Method::POST).f(index_mjsonrust)) .resource("/mjsonrust", |r| r.method(http::Method::POST).f(index_mjsonrust))
.resource("/", |r| r.method(http::Method::POST).f(index))}) .resource("/", |r| r.method(http::Method::POST).f(index))
.bind("127.0.0.1:8080").unwrap() }).bind("127.0.0.1:8080")
.unwrap()
.shutdown_timeout(1) .shutdown_timeout(1)
.start(); .start();

View File

@ -7,7 +7,7 @@ workspace = "../"
[dependencies] [dependencies]
env_logger = "0.5" env_logger = "0.5"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"
futures = "0.1" futures = "0.1"
serde = "1.0" serde = "1.0"

View File

@ -7,23 +7,22 @@ extern crate serde_json;
extern crate serde_derive; extern crate serde_derive;
#[macro_use] #[macro_use]
extern crate juniper; extern crate juniper;
extern crate futures;
extern crate actix; extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate env_logger; extern crate env_logger;
extern crate futures;
use actix::prelude::*; use actix::prelude::*;
use actix_web::{ use actix_web::{http, middleware, server, App, AsyncResponder, Error, FutureResponse,
middleware, http, server, HttpRequest, HttpResponse, Json, State};
App, AsyncResponder, HttpRequest, HttpResponse, FutureResponse, Error, State, Json}; use futures::future::Future;
use juniper::http::graphiql::graphiql_source; use juniper::http::graphiql::graphiql_source;
use juniper::http::GraphQLRequest; use juniper::http::GraphQLRequest;
use futures::future::Future;
mod schema; mod schema;
use schema::Schema;
use schema::create_schema; use schema::create_schema;
use schema::Schema;
struct AppState { struct AppState {
executor: Addr<Syn, GraphQLExecutor>, executor: Addr<Syn, GraphQLExecutor>,
@ -37,14 +36,12 @@ impl Message for GraphQLData {
} }
pub struct GraphQLExecutor { pub struct GraphQLExecutor {
schema: std::sync::Arc<Schema> schema: std::sync::Arc<Schema>,
} }
impl GraphQLExecutor { impl GraphQLExecutor {
fn new(schema: std::sync::Arc<Schema>) -> GraphQLExecutor { fn new(schema: std::sync::Arc<Schema>) -> GraphQLExecutor {
GraphQLExecutor { GraphQLExecutor { schema: schema }
schema: schema,
}
} }
} }
@ -62,23 +59,24 @@ impl Handler<GraphQLData> for GraphQLExecutor {
} }
} }
fn graphiql(_req: HttpRequest<AppState>) -> Result<HttpResponse, Error> { fn graphiql(_req: HttpRequest<AppState>) -> Result<HttpResponse, Error> {
let html = graphiql_source("http://127.0.0.1:8080/graphql"); let html = graphiql_source("http://127.0.0.1:8080/graphql");
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8") .content_type("text/html; charset=utf-8")
.body(html)) .body(html))
} }
fn graphql(st: State<AppState>, data: Json<GraphQLData>) -> FutureResponse<HttpResponse> { fn graphql(
st.executor.send(data.0) st: State<AppState>, data: Json<GraphQLData>,
) -> FutureResponse<HttpResponse> {
st.executor
.send(data.0)
.from_err() .from_err()
.and_then(|res| { .and_then(|res| match res {
match res { Ok(user) => Ok(HttpResponse::Ok()
Ok(user) => Ok(HttpResponse::Ok() .content_type("application/json")
.content_type("application/json") .body(user)),
.body(user)), Err(_) => Ok(HttpResponse::InternalServerError().into()),
Err(_) => Ok(HttpResponse::InternalServerError().into())
}
}) })
.responder() .responder()
} }
@ -89,9 +87,7 @@ fn main() {
let sys = actix::System::new("juniper-example"); let sys = actix::System::new("juniper-example");
let schema = std::sync::Arc::new(create_schema()); let schema = std::sync::Arc::new(create_schema());
let addr = SyncArbiter::start(3, move || { let addr = SyncArbiter::start(3, move || GraphQLExecutor::new(schema.clone()));
GraphQLExecutor::new(schema.clone())
});
// Start http server // Start http server
server::new(move || { server::new(move || {
@ -99,8 +95,9 @@ fn main() {
// enable logger // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.resource("/graphql", |r| r.method(http::Method::POST).with2(graphql)) .resource("/graphql", |r| r.method(http::Method::POST).with2(graphql))
.resource("/graphiql", |r| r.method(http::Method::GET).h(graphiql))}) .resource("/graphiql", |r| r.method(http::Method::GET).h(graphiql))
.bind("127.0.0.1:8080").unwrap() }).bind("127.0.0.1:8080")
.unwrap()
.start(); .start();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");

View File

@ -12,4 +12,4 @@ path = "src/main.rs"
env_logger = "*" env_logger = "*"
futures = "0.1" futures = "0.1"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"

View File

@ -5,16 +5,13 @@ extern crate env_logger;
extern crate futures; extern crate futures;
use actix::*; use actix::*;
use actix_web::{ use actix_web::{http, middleware, multipart, server, App, AsyncResponder, Error,
http, middleware, multipart, server, HttpMessage, HttpRequest, HttpResponse};
App, AsyncResponder, HttpRequest, HttpResponse, HttpMessage, Error};
use futures::{Future, Stream};
use futures::future::{result, Either}; use futures::future::{result, Either};
use futures::{Future, Stream};
fn index(req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>>
{
println!("{:?}", req); println!("{:?}", req);
req.multipart() // <- get multipart stream for current request req.multipart() // <- get multipart stream for current request
@ -49,11 +46,12 @@ fn main() {
let _ = env_logger::init(); let _ = env_logger::init();
let sys = actix::System::new("multipart-example"); let sys = actix::System::new("multipart-example");
server::new( server::new(|| {
|| App::new() App::new()
.middleware(middleware::Logger::default()) // <- logger .middleware(middleware::Logger::default()) // <- logger
.resource("/multipart", |r| r.method(http::Method::POST).a(index))) .resource("/multipart", |r| r.method(http::Method::POST).a(index))
.bind("127.0.0.1:8080").unwrap() }).bind("127.0.0.1:8080")
.unwrap()
.start(); .start();
println!("Starting http server: 127.0.0.1:8080"); println!("Starting http server: 127.0.0.1:8080");

View File

@ -14,4 +14,4 @@ prost = "0.2.0"
prost-derive = "0.2.0" prost-derive = "0.2.0"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"

View File

@ -9,26 +9,23 @@ extern crate prost;
#[macro_use] #[macro_use]
extern crate prost_derive; extern crate prost_derive;
use actix_web::{http, middleware, server, App, AsyncResponder, Error, HttpRequest,
HttpResponse};
use futures::Future; use futures::Future;
use actix_web::{
http, middleware, server,
App, AsyncResponder, HttpRequest, HttpResponse, Error};
mod protobuf; mod protobuf;
use protobuf::ProtoBufResponseBuilder; use protobuf::ProtoBufResponseBuilder;
#[derive(Clone, Debug, PartialEq, Message)] #[derive(Clone, Debug, PartialEq, Message)]
pub struct MyObj { pub struct MyObj {
#[prost(int32, tag="1")] #[prost(int32, tag = "1")]
pub number: i32, pub number: i32,
#[prost(string, tag="2")] #[prost(string, tag = "2")]
pub name: String, pub name: String,
} }
/// This handler uses `ProtoBufMessage` for loading protobuf object. /// This handler uses `ProtoBufMessage` for loading protobuf object.
fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> { fn index(req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
protobuf::ProtoBufMessage::new(req) protobuf::ProtoBufMessage::new(req)
.from_err() // convert all errors into `Error` .from_err() // convert all errors into `Error`
.and_then(|val: MyObj| { .and_then(|val: MyObj| {
@ -38,7 +35,6 @@ fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
.responder() .responder()
} }
fn main() { fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info"); ::std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init(); env_logger::init();
@ -47,8 +43,9 @@ fn main() {
server::new(|| { server::new(|| {
App::new() App::new()
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.resource("/", |r| r.method(http::Method::POST).f(index))}) .resource("/", |r| r.method(http::Method::POST).f(index))
.bind("127.0.0.1:8080").unwrap() }).bind("127.0.0.1:8080")
.unwrap()
.shutdown_timeout(1) .shutdown_timeout(1)
.start(); .start();

View File

@ -1,38 +1,36 @@
use bytes::{Bytes, BytesMut}; use bytes::{Bytes, BytesMut};
use futures::{Poll, Future, Stream}; use futures::{Future, Poll, Stream};
use bytes::IntoBuf; use bytes::IntoBuf;
use prost::Message;
use prost::DecodeError as ProtoBufDecodeError; use prost::DecodeError as ProtoBufDecodeError;
use prost::EncodeError as ProtoBufEncodeError; 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::dev::HttpResponseBuilder;
use actix_web::error::{Error, PayloadError, ResponseError}; 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)] #[derive(Fail, Debug)]
pub enum ProtoBufPayloadError { pub enum ProtoBufPayloadError {
/// Payload size is bigger than 256k /// Payload size is bigger than 256k
#[fail(display="Payload size is bigger than 256k")] #[fail(display = "Payload size is bigger than 256k")]
Overflow, Overflow,
/// Content type error /// Content type error
#[fail(display="Content type error")] #[fail(display = "Content type error")]
ContentType, ContentType,
/// Serialize error /// Serialize error
#[fail(display="ProtoBud serialize error: {}", _0)] #[fail(display = "ProtoBud serialize error: {}", _0)]
Serialize(#[cause] ProtoBufEncodeError), Serialize(#[cause] ProtoBufEncodeError),
/// Deserialize error /// Deserialize error
#[fail(display="ProtoBud deserialize error: {}", _0)] #[fail(display = "ProtoBud deserialize error: {}", _0)]
Deserialize(#[cause] ProtoBufDecodeError), Deserialize(#[cause] ProtoBufDecodeError),
/// Payload error /// Payload error
#[fail(display="Error that occur during reading payload: {}", _0)] #[fail(display = "Error that occur during reading payload: {}", _0)]
Payload(#[cause] PayloadError), Payload(#[cause] PayloadError),
} }
impl ResponseError for ProtoBufPayloadError { impl ResponseError for ProtoBufPayloadError {
fn error_response(&self) -> HttpResponse { fn error_response(&self) -> HttpResponse {
match *self { match *self {
ProtoBufPayloadError::Overflow => HttpResponse::PayloadTooLarge().into(), ProtoBufPayloadError::Overflow => HttpResponse::PayloadTooLarge().into(),
@ -60,31 +58,31 @@ impl<T: Message> Responder for ProtoBuf<T> {
type Item = HttpResponse; type Item = HttpResponse;
type Error = Error; type Error = Error;
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, Error> { fn respond_to<S>(self, _: &HttpRequest<S>) -> Result<HttpResponse, Error> {
let mut buf = Vec::new(); let mut buf = Vec::new();
self.0.encode(&mut buf) self.0
.encode(&mut buf)
.map_err(|e| Error::from(ProtoBufPayloadError::Serialize(e))) .map_err(|e| Error::from(ProtoBufPayloadError::Serialize(e)))
.and_then(|()| { .and_then(|()| {
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.content_type("application/protobuf") .content_type("application/protobuf")
.body(buf) .body(buf)
.into()) .into())
}) })
} }
} }
pub struct ProtoBufMessage<T, U: Message + Default>{ pub struct ProtoBufMessage<T, U: Message + Default> {
limit: usize, limit: usize,
ct: &'static str, ct: &'static str,
req: Option<T>, req: Option<T>,
fut: Option<Box<Future<Item=U, Error=ProtoBufPayloadError>>>, fut: Option<Box<Future<Item = U, Error = ProtoBufPayloadError>>>,
} }
impl<T, U: Message + Default> ProtoBufMessage<T, U> { impl<T, U: Message + Default> ProtoBufMessage<T, U> {
/// Create `ProtoBufMessage` for request. /// Create `ProtoBufMessage` for request.
pub fn new(req: T) -> Self { pub fn new(req: T) -> Self {
ProtoBufMessage{ ProtoBufMessage {
limit: 262_144, limit: 262_144,
req: Some(req), req: Some(req),
fut: None, fut: None,
@ -109,7 +107,8 @@ impl<T, U: Message + Default> ProtoBufMessage<T, U> {
} }
impl<T, U: Message + Default + 'static> Future for ProtoBufMessage<T, U> impl<T, U: Message + Default + 'static> Future for ProtoBufMessage<T, U>
where T: HttpMessage + Stream<Item=Bytes, Error=PayloadError> + 'static where
T: HttpMessage + Stream<Item = Bytes, Error = PayloadError> + 'static,
{ {
type Item = U; type Item = U;
type Error = ProtoBufPayloadError; type Error = ProtoBufPayloadError;
@ -129,7 +128,7 @@ impl<T, U: Message + Default + 'static> Future for ProtoBufMessage<T, U>
} }
// check content-type // check content-type
if !self.ct.is_empty() && req.content_type() != self.ct { if !self.ct.is_empty() && req.content_type() != self.ct {
return Err(ProtoBufPayloadError::ContentType) return Err(ProtoBufPayloadError::ContentType);
} }
let limit = self.limit; let limit = self.limit;
@ -146,23 +145,25 @@ impl<T, U: Message + Default + 'static> Future for ProtoBufMessage<T, U>
self.fut = Some(Box::new(fut)); 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 { pub trait ProtoBufResponseBuilder {
fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error>; fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error>;
} }
impl ProtoBufResponseBuilder for HttpResponseBuilder { impl ProtoBufResponseBuilder for HttpResponseBuilder {
fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error> { fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error> {
self.header(CONTENT_TYPE, "application/protobuf"); self.header(CONTENT_TYPE, "application/protobuf");
let mut body = Vec::new(); 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)) Ok(self.body(body))
} }
} }

View File

@ -1,17 +1,16 @@
//! Db executor actor //! Db executor actor
use std::io;
use uuid;
use actix_web::*;
use actix::prelude::*; use actix::prelude::*;
use actix_web::*;
use r2d2::Pool; use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager; use r2d2_sqlite::SqliteConnectionManager;
use std::io;
use uuid;
/// This is db executor actor. We are going to run 3 of them in parallel. /// This is db executor actor. We are going to run 3 of them in parallel.
pub struct DbExecutor(pub Pool<SqliteConnectionManager>); pub struct DbExecutor(pub Pool<SqliteConnectionManager>);
/// This is only message that this actor can handle, but it is easy to extend number of /// This is only message that this actor can handle, but it is easy to extend
/// messages. /// number of messages.
pub struct CreateUser { pub struct CreateUser {
pub name: String, pub name: String,
} }
@ -31,11 +30,15 @@ impl Handler<CreateUser> for DbExecutor {
let conn = self.0.get().unwrap(); let conn = self.0.get().unwrap();
let uuid = format!("{}", uuid::Uuid::new_v4()); let uuid = format!("{}", uuid::Uuid::new_v4());
conn.execute("INSERT INTO users (id, name) VALUES ($1, $2)", conn.execute(
&[&uuid, &msg.name]).unwrap(); "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| { Ok(conn.query_row(
row.get(0) "SELECT name FROM users WHERE id=$1",
}).map_err(|_| io::Error::new(io::ErrorKind::Other, "db error"))?) &[&uuid],
|row| row.get(0),
).map_err(|_| io::Error::new(io::ErrorKind::Other, "db error"))?)
} }
} }

View File

@ -1,41 +1,42 @@
//! Actix web r2d2 example //! Actix web r2d2 example
extern crate serde;
extern crate serde_json;
extern crate uuid;
extern crate futures;
extern crate actix; extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate env_logger; extern crate env_logger;
extern crate futures;
extern crate r2d2; extern crate r2d2;
extern crate r2d2_sqlite; extern crate r2d2_sqlite;
extern crate rusqlite; extern crate rusqlite;
extern crate serde;
extern crate serde_json;
extern crate uuid;
use actix::prelude::*; use actix::prelude::*;
use actix_web::{ use actix_web::{http, middleware, server, App, AsyncResponder, Error, HttpRequest,
middleware, http, server, App, AsyncResponder, HttpRequest, HttpResponse, Error}; HttpResponse};
use futures::future::Future; use futures::future::Future;
use r2d2_sqlite::SqliteConnectionManager; use r2d2_sqlite::SqliteConnectionManager;
mod db; mod db;
use db::{CreateUser, DbExecutor}; use db::{CreateUser, DbExecutor};
/// State with DbExecutor address /// State with DbExecutor address
struct State { struct State {
db: Addr<Syn, DbExecutor>, db: Addr<Syn, DbExecutor>,
} }
/// Async request handler /// Async request handler
fn index(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> { fn index(req: HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Error>> {
let name = &req.match_info()["name"]; 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() .from_err()
.and_then(|res| { .and_then(|res| match res {
match res { Ok(user) => Ok(HttpResponse::Ok().json(user)),
Ok(user) => Ok(HttpResponse::Ok().json(user)), Err(_) => Ok(HttpResponse::InternalServerError().into()),
Err(_) => Ok(HttpResponse::InternalServerError().into())
}
}) })
.responder() .responder()
} }
@ -57,8 +58,9 @@ fn main() {
App::with_state(State{db: addr.clone()}) App::with_state(State{db: addr.clone()})
// enable logger // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.resource("/{name}", |r| r.method(http::Method::GET).a(index))}) .resource("/{name}", |r| r.method(http::Method::GET).a(index))
.bind("127.0.0.1:8080").unwrap() }).bind("127.0.0.1:8080")
.unwrap()
.start(); .start();
let _ = sys.run(); let _ = sys.run();

View File

@ -7,5 +7,5 @@ workspace = "../"
[dependencies] [dependencies]
env_logger = "0.5" env_logger = "0.5"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"
actix-redis = { version = "0.3", features = ["web"] } actix-redis = { version = "0.4", features = ["web"] }

View File

@ -7,11 +7,11 @@ extern crate actix_web;
extern crate env_logger; extern crate env_logger;
use actix_redis::RedisSessionBackend; use actix_redis::RedisSessionBackend;
use actix_web::middleware::{Logger, RequestSession, SessionStorage}; use actix_web::middleware::session::{RequestSession, SessionStorage};
use actix_web::{server, App, HttpRequest, HttpResponse, Result}; use actix_web::{middleware, server, App, HttpRequest, HttpResponse, Result};
/// simple handler /// simple handler
fn index(mut req: HttpRequest) -> Result<HttpResponse> { fn index(req: HttpRequest) -> Result<HttpResponse> {
println!("{:?}", req); println!("{:?}", req);
// session // session
@ -33,7 +33,7 @@ fn main() {
server::new(|| { server::new(|| {
App::new() App::new()
// enable logger // enable logger
.middleware(Logger::default()) .middleware(middleware::Logger::default())
// redis session middleware // redis session middleware
.middleware(SessionStorage::new( .middleware(SessionStorage::new(
RedisSessionBackend::new("127.0.0.1:6379", &[0; 32]) RedisSessionBackend::new("127.0.0.1:6379", &[0; 32])

5
rustfmt.toml Normal file
View File

@ -0,0 +1,5 @@
max_width = 89
reorder_imports = true
wrap_comments = true
fn_args_density = "Compressed"
#use_small_heuristics = false

View File

@ -8,4 +8,4 @@ workspace = "../"
futures = "0.1" futures = "0.1"
env_logger = "0.5" env_logger = "0.5"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"

View File

@ -3,11 +3,11 @@
//! that is shared across all handlers within same Application. //! that is shared across all handlers within same Application.
//! And individual handler can have state. //! And individual handler can have state.
//! //!
//! > **Note**: http server accepts an application factory rather than an application //! > **Note**: http server accepts an application factory rather than an
//! > instance. Http server constructs an application instance for each thread, //! application > instance. Http server constructs an application instance for
//! > thus application state //! each thread, > thus application state
//! > must be constructed multiple times. If you want to share state between different //! > must be constructed multiple times. If you want to share state between
//! > threads, a shared object should be used, e.g. `Arc`. //! 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. //! 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 std::cell::Cell;
use actix::prelude::*; use actix_web::{middleware, server, App, HttpRequest, HttpResponse};
use actix_web::{http, middleware, server, ws, App, HttpRequest, HttpResponse};
/// Application state /// Application state
struct AppState { struct AppState {
@ -30,7 +29,10 @@ fn index(req: HttpRequest<AppState>) -> HttpResponse {
println!("{:?}", req); println!("{:?}", req);
req.state().counter.set(req.state().counter.get() + 1); 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() { fn main() {

View File

@ -7,7 +7,7 @@ workspace = "../"
[dependencies] [dependencies]
env_logger = "0.5" env_logger = "0.5"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"
askama = "0.6" askama = "0.6"
[build-dependencies] [build-dependencies]

View File

@ -3,8 +3,9 @@ extern crate actix_web;
#[macro_use] #[macro_use]
extern crate askama; extern crate askama;
use actix::prelude::*; use std::collections::HashMap;
use actix_web::{http, server, App, HttpRequest, HttpResponse, Result};
use actix_web::{http, server, App, HttpResponse, Query, Result};
use askama::Template; use askama::Template;
#[derive(Template)] #[derive(Template)]
@ -18,8 +19,8 @@ struct UserTemplate<'a> {
#[template(path = "index.html")] #[template(path = "index.html")]
struct Index; struct Index;
fn index(req: HttpRequest) -> Result<HttpResponse> { fn index(query: Query<HashMap<String, String>>) -> Result<HttpResponse> {
let s = if let Some(name) = req.query().get("name") { let s = if let Some(name) = query.get("name") {
UserTemplate { UserTemplate {
name: name, name: name,
text: "Welcome!", text: "Welcome!",
@ -32,11 +33,12 @@ fn index(req: HttpRequest) -> Result<HttpResponse> {
} }
fn main() { fn main() {
let sys = System::new("template-askama"); let sys = actix::System::new("template-askama");
// start http server // start http server
server::new(move || App::new().resource("/", |r| r.method(http::Method::GET).f(index))) server::new(move || {
.bind("0.0.0.0:8080") App::new().resource("/", |r| r.method(http::Method::GET).with(index))
}).bind("0.0.0.0:8080")
.unwrap() .unwrap()
.start(); .start();

View File

@ -7,5 +7,5 @@ workspace = "../"
[dependencies] [dependencies]
env_logger = "0.5" env_logger = "0.5"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"
tera = "*" tera = "*"

View File

@ -4,28 +4,33 @@ extern crate env_logger;
#[macro_use] #[macro_use]
extern crate tera; extern crate tera;
use actix_web::{ use std::collections::HashMap;
http, error, middleware, server, App, HttpRequest, HttpResponse, Error};
use actix_web::{error, http, middleware, server, App, Error, HttpResponse, Query, State};
struct State { struct AppState {
template: tera::Tera, // <- store tera template in application state template: tera::Tera, // <- store tera template in application state
} }
fn index(req: HttpRequest<State>) -> Result<HttpResponse, Error> { fn index(
let s = if let Some(name) = req.query().get("name") { // <- submitted form state: State<AppState>, query: Query<HashMap<String, String>>,
) -> Result<HttpResponse, Error> {
let s = if let Some(name) = query.get("name") {
// <- submitted form
let mut ctx = tera::Context::new(); let mut ctx = tera::Context::new();
ctx.add("name", &name.to_owned()); ctx.add("name", &name.to_owned());
ctx.add("text", &"Welcome!".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"))? .map_err(|_| error::ErrorInternalServerError("Template error"))?
} else { } else {
req.state().template.render("index.html", &tera::Context::new()) state
.template
.render("index.html", &tera::Context::new())
.map_err(|_| error::ErrorInternalServerError("Template error"))? .map_err(|_| error::ErrorInternalServerError("Template error"))?
}; };
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok().content_type("text/html").body(s))
.content_type("text/html")
.body(s))
} }
fn main() { fn main() {
@ -34,13 +39,15 @@ fn main() {
let sys = actix::System::new("tera-example"); let sys = actix::System::new("tera-example");
server::new(|| { 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 // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.resource("/", |r| r.method(http::Method::GET).f(index))}) .resource("/", |r| r.method(http::Method::GET).with2(index))
.bind("127.0.0.1:8080").unwrap() }).bind("127.0.0.1:8080")
.unwrap()
.start(); .start();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");

View File

@ -11,5 +11,5 @@ path = "src/main.rs"
[dependencies] [dependencies]
env_logger = "0.5" env_logger = "0.5"
actix = "0.5" actix = "0.5"
actix-web = { version = "^0.5", features=["alpn"] } actix-web = { version = "^0.6", features=["alpn"] }
openssl = { version="0.10" } openssl = { version="0.10" }

View File

@ -4,17 +4,15 @@ extern crate actix_web;
extern crate env_logger; extern crate env_logger;
extern crate openssl; extern crate openssl;
use openssl::ssl::{SslMethod, SslAcceptor, SslFiletype}; use actix_web::{http, middleware, server, App, Error, HttpRequest, HttpResponse};
use actix_web::{ use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
http, middleware, server, App, HttpRequest, HttpResponse, Error};
/// simple handle /// simple handle
fn index(req: HttpRequest) -> Result<HttpResponse, Error> { fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
println!("{:?}", req); println!("{:?}", req);
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.content_type("text/plain") .content_type("text/plain")
.body("Welcome!")) .body("Welcome!"))
} }
fn main() { fn main() {
@ -26,11 +24,15 @@ fn main() {
// load ssl keys // load ssl keys
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap(); let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder.set_private_key_file("key.pem", SslFiletype::PEM).unwrap(); builder
builder.set_certificate_chain_file("cert.pem").unwrap(); .set_private_key_file("key.pem", SslFiletype::PEM)
.unwrap();
builder
.set_certificate_chain_file("cert.pem")
.unwrap();
server::new( server::new(|| {
|| App::new() App::new()
// enable logger // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
// register simple handler, handle all methods // register simple handler, handle all methods
@ -40,9 +42,10 @@ fn main() {
HttpResponse::Found() HttpResponse::Found()
.header("LOCATION", "/index.html") .header("LOCATION", "/index.html")
.finish() .finish()
}))) }))
.bind("127.0.0.1:8443").unwrap() }).bind_ssl("127.0.0.1:8443", builder)
.start_ssl(builder).unwrap(); .unwrap()
.start();
println!("Started http server: 127.0.0.1:8443"); println!("Started http server: 127.0.0.1:8443");
let _ = sys.run(); let _ = sys.run();

View File

@ -7,5 +7,5 @@ workspace = "../"
[dependencies] [dependencies]
env_logger = "0.5" env_logger = "0.5"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"
tokio-uds = "0.1" tokio-uds = "0.1"

View File

@ -7,7 +7,6 @@ use actix::*;
use actix_web::{middleware, server, App, HttpRequest}; use actix_web::{middleware, server, App, HttpRequest};
use tokio_uds::UnixListener; use tokio_uds::UnixListener;
fn index(_req: HttpRequest) -> &'static str { fn index(_req: HttpRequest) -> &'static str {
"Hello world!" "Hello world!"
} }
@ -17,15 +16,15 @@ fn main() {
env_logger::init(); env_logger::init();
let sys = actix::System::new("unix-socket"); let sys = actix::System::new("unix-socket");
let listener = UnixListener::bind( let listener = UnixListener::bind("/tmp/actix-uds.socket", Arbiter::handle())
"/tmp/actix-uds.socket", Arbiter::handle()).expect("bind failed"); .expect("bind failed");
server::new( server::new(|| {
|| App::new() App::new()
// enable logger // enable logger
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.resource("/index.html", |r| r.f(|_| "Hello world!")) .resource("/index.html", |r| r.f(|_| "Hello world!"))
.resource("/", |r| r.f(index))) .resource("/", |r| r.f(index))
.start_incoming(listener.incoming(), false); }).start_incoming(listener.incoming(), false);
println!("Started http server: /tmp/actix-uds.socket"); println!("Started http server: /tmp/actix-uds.socket");
let _ = sys.run(); let _ = sys.run();

View File

@ -11,7 +11,7 @@ serde_json = "1.0"
http = "0.1" http = "0.1"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"
dotenv = "0.10" dotenv = "0.10"
env_logger = "0.5" env_logger = "0.5"
futures = "0.1" futures = "0.1"

View File

@ -1,36 +1,45 @@
#[macro_use] extern crate serde_derive; #[macro_use]
extern crate serde; extern crate serde_derive;
extern crate serde_json;
extern crate futures;
extern crate actix; extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate env_logger; 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 std::env;
use actix_web::{server, App, http::{header, Method}, middleware, middleware::cors::Cors};
mod user; mod user;
use user::info; use user::info;
fn main() { fn main() {
env::set_var("RUST_LOG", "actix_web=info"); env::set_var("RUST_LOG", "actix_web=info");
env_logger::init(); env_logger::init();
let sys = actix::System::new("Actix-web-CORS"); let sys = actix::System::new("Actix-web-CORS");
server::new( server::new(move || {
move || App::new() App::new()
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.configure(|app| Cors::for_app(app) .configure(|app| {
.allowed_origin("http://localhost:1234") Cors::for_app(app)
.allowed_methods(vec!["GET", "POST"]) .allowed_origin("http://localhost:1234")
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT]) .allowed_methods(vec!["GET", "POST"])
.allowed_header(header::CONTENT_TYPE) .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
.max_age(3600) .allowed_header(header::CONTENT_TYPE)
.resource("/user/info", |r| { r.method(Method::POST).with(info);}) .max_age(3600)
.register())) .resource("/user/info", |r| {
.bind("127.0.0.1:8000").unwrap() r.method(Method::POST).with(info);
})
.register()
})
}).bind("127.0.0.1:8000")
.unwrap()
.shutdown_timeout(2) .shutdown_timeout(2)
.start(); .start();

View File

@ -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 { pub struct Info {
username: String, username: String,
email: String, email: String,
@ -10,11 +10,10 @@ pub struct Info {
pub fn info(info: Json<Info>) -> Result<Json<Info>> { pub fn info(info: Json<Info>) -> Result<Json<Info>> {
println!("=========={:?}=========", info); println!("=========={:?}=========", info);
Ok(Json(Info{ Ok(Json(Info {
username: info.username.clone(), username: info.username.clone(),
email: info.email.clone(), email: info.email.clone(),
password: info.password.clone(), password: info.password.clone(),
confirm_password: info.confirm_password.clone(), confirm_password: info.confirm_password.clone(),
})) }))
} }

View File

@ -26,4 +26,4 @@ serde_json = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"

View File

@ -1,26 +1,27 @@
#[macro_use] extern crate actix; #[macro_use]
extern crate bytes; extern crate actix;
extern crate byteorder; extern crate byteorder;
extern crate bytes;
extern crate futures; extern crate futures;
extern crate tokio_io;
extern crate tokio_core;
extern crate serde; extern crate serde;
extern crate serde_json; 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::str::FromStr;
use std::time::Duration; use std::time::Duration;
use futures::Future; use std::{io, net, process, thread};
use tokio_io::AsyncRead;
use tokio_io::io::WriteHalf;
use tokio_io::codec::FramedRead;
use tokio_core::net::TcpStream; use tokio_core::net::TcpStream;
use actix::prelude::*; use tokio_io::codec::FramedRead;
use tokio_io::io::WriteHalf;
use tokio_io::AsyncRead;
mod codec; mod codec;
fn main() { fn main() {
let sys = actix::System::new("chat-client"); let sys = actix::System::new("chat-client");
@ -31,22 +32,28 @@ fn main() {
.and_then(|stream| { .and_then(|stream| {
let addr: Addr<Syn, _> = ChatClient::create(|ctx| { let addr: Addr<Syn, _> = ChatClient::create(|ctx| {
let (r, w) = stream.split(); let (r, w) = stream.split();
ChatClient::add_stream(FramedRead::new(r, codec::ClientChatCodec), ctx); ChatClient::add_stream(
ChatClient{ FramedRead::new(r, codec::ClientChatCodec),
ctx,
);
ChatClient {
framed: actix::io::FramedWrite::new( framed: actix::io::FramedWrite::new(
w, codec::ClientChatCodec, ctx)}}); w,
codec::ClientChatCodec,
ctx,
),
}
});
// start console loop // start console loop
thread::spawn(move|| { thread::spawn(move || loop {
loop { let mut cmd = String::new();
let mut cmd = String::new(); if io::stdin().read_line(&mut cmd).is_err() {
if io::stdin().read_line(&mut cmd).is_err() { println!("error");
println!("error"); return;
return
}
addr.do_send(ClientCommand(cmd));
} }
addr.do_send(ClientCommand(cmd));
}); });
futures::future::ok(()) futures::future::ok(())
@ -54,14 +61,13 @@ fn main() {
.map_err(|e| { .map_err(|e| {
println!("Can not connect to server: {}", e); println!("Can not connect to server: {}", e);
process::exit(1) process::exit(1)
}) }),
); );
println!("Running chat client"); println!("Running chat client");
sys.run(); sys.run();
} }
struct ChatClient { struct ChatClient {
framed: actix::io::FramedWrite<WriteHalf<TcpStream>, codec::ClientChatCodec>, framed: actix::io::FramedWrite<WriteHalf<TcpStream>, codec::ClientChatCodec>,
} }
@ -103,7 +109,7 @@ impl Handler<ClientCommand> for ChatClient {
fn handle(&mut self, msg: ClientCommand, _: &mut Context<Self>) { fn handle(&mut self, msg: ClientCommand, _: &mut Context<Self>) {
let m = msg.0.trim(); let m = msg.0.trim();
if m.is_empty() { if m.is_empty() {
return return;
} }
// we check for /sss type of messages // we check for /sss type of messages
@ -112,18 +118,20 @@ impl Handler<ClientCommand> for ChatClient {
match v[0] { match v[0] {
"/list" => { "/list" => {
self.framed.write(codec::ChatRequest::List); self.framed.write(codec::ChatRequest::List);
}, }
"/join" => { "/join" => {
if v.len() == 2 { 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 { } else {
println!("!!! room name is required"); println!("!!! room name is required");
} }
}, }
_ => println!("!!! unknown command"), _ => println!("!!! unknown command"),
} }
} else { } 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<ClientCommand> for ChatClient {
/// Server communication /// Server communication
impl StreamHandler<codec::ChatResponse, io::Error> for ChatClient { impl StreamHandler<codec::ChatResponse, io::Error> for ChatClient {
fn handle(&mut self, msg: codec::ChatResponse, _: &mut Context<Self>) { fn handle(&mut self, msg: codec::ChatResponse, _: &mut Context<Self>) {
match msg { match msg {
codec::ChatResponse::Message(ref msg) => { codec::ChatResponse::Message(ref msg) => {

View File

@ -1,13 +1,13 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::io; use byteorder::{BigEndian, ByteOrder};
use bytes::{BufMut, BytesMut};
use serde_json as json; use serde_json as json;
use byteorder::{BigEndian , ByteOrder}; use std::io;
use bytes::{BytesMut, BufMut}; use tokio_io::codec::{Decoder, Encoder};
use tokio_io::codec::{Encoder, Decoder};
/// Client request /// Client request
#[derive(Serialize, Deserialize, Debug, Message)] #[derive(Serialize, Deserialize, Debug, Message)]
#[serde(tag="cmd", content="data")] #[serde(tag = "cmd", content = "data")]
pub enum ChatRequest { pub enum ChatRequest {
/// List rooms /// List rooms
List, List,
@ -16,12 +16,12 @@ pub enum ChatRequest {
/// Send message /// Send message
Message(String), Message(String),
/// Ping /// Ping
Ping Ping,
} }
/// Server response /// Server response
#[derive(Serialize, Deserialize, Debug, Message)] #[derive(Serialize, Deserialize, Debug, Message)]
#[serde(tag="cmd", content="data")] #[serde(tag = "cmd", content = "data")]
pub enum ChatResponse { pub enum ChatResponse {
Ping, Ping,
@ -38,15 +38,14 @@ pub enum ChatResponse {
/// Codec for Client -> Server transport /// Codec for Client -> Server transport
pub struct ChatCodec; pub struct ChatCodec;
impl Decoder for ChatCodec impl Decoder for ChatCodec {
{
type Item = ChatRequest; type Item = ChatRequest;
type Error = io::Error; type Error = io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
let size = { let size = {
if src.len() < 2 { if src.len() < 2 {
return Ok(None) return Ok(None);
} }
BigEndian::read_u16(src.as_ref()) as usize 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 Item = ChatResponse;
type Error = io::Error; 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 = json::to_string(&msg).unwrap();
let msg_ref: &[u8] = msg.as_ref(); let msg_ref: &[u8] = msg.as_ref();
@ -78,19 +78,17 @@ impl Encoder for ChatCodec
} }
} }
/// Codec for Server -> Client transport /// Codec for Server -> Client transport
pub struct ClientChatCodec; pub struct ClientChatCodec;
impl Decoder for ClientChatCodec impl Decoder for ClientChatCodec {
{
type Item = ChatResponse; type Item = ChatResponse;
type Error = io::Error; type Error = io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
let size = { let size = {
if src.len() < 2 { if src.len() < 2 {
return Ok(None) return Ok(None);
} }
BigEndian::read_u16(src.as_ref()) as usize 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 Item = ChatRequest;
type Error = io::Error; 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 = json::to_string(&msg).unwrap();
let msg_ref: &[u8] = msg.as_ref(); let msg_ref: &[u8] = msg.as_ref();

View File

@ -1,14 +1,15 @@
#![allow(unused_variables)] #![allow(unused_variables)]
extern crate rand;
extern crate bytes;
extern crate byteorder; extern crate byteorder;
extern crate futures; extern crate bytes;
extern crate tokio_io;
extern crate tokio_core;
extern crate env_logger; extern crate env_logger;
extern crate futures;
extern crate rand;
extern crate serde; extern crate serde;
extern crate serde_json; 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] #[macro_use]
extern crate actix; extern crate actix;
@ -18,14 +19,15 @@ use std::time::Instant;
use actix::*; use actix::*;
use actix_web::server::HttpServer; 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 codec;
mod server; mod server;
mod session; mod session;
/// This is our websocket route state, this state is shared with all route instances /// This is our websocket route state, this state is shared with all route
/// via `HttpContext::state()` /// instances via `HttpContext::state()`
struct WsChatSessionState { struct WsChatSessionState {
addr: Addr<Syn, server::ChatServer>, addr: Addr<Syn, server::ChatServer>,
} }
@ -38,13 +40,16 @@ fn chat_route(req: HttpRequest<WsChatSessionState>) -> Result<HttpResponse, Erro
id: 0, id: 0,
hb: Instant::now(), hb: Instant::now(),
room: "Main".to_owned(), room: "Main".to_owned(),
name: None}) name: None,
},
)
} }
struct WsChatSession { struct WsChatSession {
/// unique session id /// unique session id
id: usize, id: usize,
/// 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, hb: Instant,
/// joined room /// joined room
room: String, room: String,
@ -61,10 +66,14 @@ impl Actor for WsChatSession {
// register self in chat server. `AsyncContext::wait` register // register self in chat server. `AsyncContext::wait` register
// future within context, but context waits until this future resolves // future within context, but context waits until this future resolves
// before processing any other events. // before processing any other events.
// HttpContext::state() is instance of WsChatSessionState, state is shared across all // HttpContext::state() is instance of WsChatSessionState, state is shared
// routes within application // across all routes within application
let addr: Addr<Syn, _> = ctx.address(); let addr: Addr<Syn, _> = ctx.address();
ctx.state().addr.send(server::Connect{addr: addr.recipient()}) ctx.state()
.addr
.send(server::Connect {
addr: addr.recipient(),
})
.into_actor(self) .into_actor(self)
.then(|res, act, ctx| { .then(|res, act, ctx| {
match res { match res {
@ -73,12 +82,15 @@ impl Actor for WsChatSession {
_ => ctx.stop(), _ => ctx.stop(),
} }
fut::ok(()) fut::ok(())
}).wait(ctx); })
.wait(ctx);
} }
fn stopping(&mut self, ctx: &mut Self::Context) -> Running { fn stopping(&mut self, ctx: &mut Self::Context) -> Running {
// notify chat server // 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 Running::Stop
} }
} }
@ -94,7 +106,6 @@ impl Handler<session::Message> for WsChatSession {
/// WebSocket message handler /// WebSocket message handler
impl StreamHandler<ws::Message, ws::ProtocolError> for WsChatSession { impl StreamHandler<ws::Message, ws::ProtocolError> for WsChatSession {
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
println!("WEBSOCKET MESSAGE: {:?}", msg); println!("WEBSOCKET MESSAGE: {:?}", msg);
match msg { match msg {
@ -107,9 +118,12 @@ impl StreamHandler<ws::Message, ws::ProtocolError> for WsChatSession {
let v: Vec<&str> = m.splitn(2, ' ').collect(); let v: Vec<&str> = m.splitn(2, ' ').collect();
match v[0] { match v[0] {
"/list" => { "/list" => {
// Send ListRooms message to chat server and wait for response // Send ListRooms message to chat server and wait for
// response
println!("List rooms"); println!("List rooms");
ctx.state().addr.send(server::ListRooms) ctx.state()
.addr
.send(server::ListRooms)
.into_actor(self) .into_actor(self)
.then(|res, _, ctx| { .then(|res, _, ctx| {
match res { match res {
@ -117,33 +131,36 @@ impl StreamHandler<ws::Message, ws::ProtocolError> for WsChatSession {
for room in rooms { for room in rooms {
ctx.text(room); ctx.text(room);
} }
}, }
_ => println!("Something is wrong"), _ => println!("Something is wrong"),
} }
fut::ok(()) fut::ok(())
}).wait(ctx) })
.wait(ctx)
// .wait(ctx) pauses all events in context, // .wait(ctx) pauses all events in context,
// so actor wont receive any new messages until it get list // so actor wont receive any new messages until it get list
// of rooms back // of rooms back
}, }
"/join" => { "/join" => {
if v.len() == 2 { if v.len() == 2 {
self.room = v[1].to_owned(); self.room = v[1].to_owned();
ctx.state().addr.do_send( ctx.state().addr.do_send(server::Join {
server::Join{id: self.id, name: self.room.clone()}); id: self.id,
name: self.room.clone(),
});
ctx.text("joined"); ctx.text("joined");
} else { } else {
ctx.text("!!! room name is required"); ctx.text("!!! room name is required");
} }
}, }
"/name" => { "/name" => {
if v.len() == 2 { if v.len() == 2 {
self.name = Some(v[1].to_owned()); self.name = Some(v[1].to_owned());
} else { } else {
ctx.text("!!! name is required"); ctx.text("!!! name is required");
} }
}, }
_ => ctx.text(format!("!!! unknown command: {:?}", m)), _ => ctx.text(format!("!!! unknown command: {:?}", m)),
} }
} else { } else {
@ -153,14 +170,14 @@ impl StreamHandler<ws::Message, ws::ProtocolError> for WsChatSession {
m.to_owned() m.to_owned()
}; };
// send message to chat server // send message to chat server
ctx.state().addr.do_send( ctx.state().addr.do_send(server::Message {
server::Message{id: self.id, id: self.id,
msg: msg, msg: msg,
room: self.room.clone()}) room: self.room.clone(),
})
} }
}, }
ws::Message::Binary(bin) => ws::Message::Binary(bin) => println!("Unexpected binary"),
println!("Unexpected binary"),
ws::Message::Close(_) => { ws::Message::Close(_) => {
ctx.stop(); ctx.stop();
} }
@ -177,19 +194,19 @@ fn main() {
// Start tcp server in separate thread // Start tcp server in separate thread
let srv = server.clone(); let srv = server.clone();
Arbiter::new("tcp-server").do_send::<msgs::Execute>( Arbiter::new("tcp-server").do_send::<msgs::Execute>(msgs::Execute::new(move || {
msgs::Execute::new(move || { session::TcpServer::new("127.0.0.1:12345", srv);
session::TcpServer::new("127.0.0.1:12345", srv); Ok(())
Ok(()) }));
}));
// Create Http server with websocket support // Create Http server with websocket support
HttpServer::new( HttpServer::new(move || {
move || { // Websocket sessions state
// Websocket sessions state let state = WsChatSessionState {
let state = WsChatSessionState { addr: server.clone() }; addr: server.clone(),
};
App::with_state(state) App::with_state(state)
// redirect to websocket.html // redirect to websocket.html
.resource("/", |r| r.method(http::Method::GET).f(|_| { .resource("/", |r| r.method(http::Method::GET).f(|_| {
HttpResponse::Found() HttpResponse::Found()
@ -200,8 +217,8 @@ fn main() {
.resource("/ws/", |r| r.route().f(chat_route)) .resource("/ws/", |r| r.route().f(chat_route))
// static resources // static resources
.handler("/static/", fs::StaticFiles::new("static/")) .handler("/static/", fs::StaticFiles::new("static/"))
}) }).bind("127.0.0.1:8080")
.bind("127.0.0.1:8080").unwrap() .unwrap()
.start(); .start();
println!("Started http server: 127.0.0.1:8080"); println!("Started http server: 127.0.0.1:8080");

View File

@ -2,10 +2,10 @@
//! And manages available rooms. Peers send messages to other peers in same //! And manages available rooms. Peers send messages to other peers in same
//! room through `ChatServer`. //! room through `ChatServer`.
use actix::prelude::*;
use rand::{self, Rng, ThreadRng};
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use rand::{self, Rng, ThreadRng};
use actix::prelude::*;
use session; use session;
@ -51,8 +51,8 @@ pub struct Join {
pub name: String, pub name: String,
} }
/// `ChatServer` manages chat rooms and responsible for coordinating chat session. /// `ChatServer` manages chat rooms and responsible for coordinating chat
/// implementation is super primitive /// session. implementation is super primitive
pub struct ChatServer { pub struct ChatServer {
sessions: HashMap<usize, Recipient<Syn, session::Message>>, sessions: HashMap<usize, Recipient<Syn, session::Message>>,
rooms: HashMap<String, HashSet<usize>>, rooms: HashMap<String, HashSet<usize>>,
@ -112,7 +112,10 @@ impl Handler<Connect> for ChatServer {
self.sessions.insert(id, msg.addr); self.sessions.insert(id, msg.addr);
// auto join session to Main room // 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 // send id back
id id
@ -174,7 +177,7 @@ impl Handler<Join> for ChatServer {
type Result = (); type Result = ();
fn handle(&mut self, msg: Join, _: &mut Context<Self>) { fn handle(&mut self, msg: Join, _: &mut Context<Self>) {
let Join {id, name} = msg; let Join { id, name } = msg;
let mut rooms = Vec::new(); let mut rooms = Vec::new();
// remove session from all rooms // remove session from all rooms

View File

@ -1,19 +1,18 @@
//! `ClientSession` is an actor, it manages peer tcp connection and //! `ClientSession` is an actor, it manages peer tcp connection and
//! proxies commands from peer to `ChatServer`. //! proxies commands from peer to `ChatServer`.
use std::{io, net};
use std::str::FromStr;
use std::time::{Instant, Duration};
use futures::Stream; use futures::Stream;
use tokio_io::AsyncRead; use std::str::FromStr;
use tokio_io::io::WriteHalf; use std::time::{Duration, Instant};
use std::{io, net};
use tokio_core::net::{TcpListener, TcpStream};
use tokio_io::codec::FramedRead; use tokio_io::codec::FramedRead;
use tokio_core::net::{TcpStream, TcpListener}; use tokio_io::io::WriteHalf;
use tokio_io::AsyncRead;
use actix::prelude::*; use actix::prelude::*;
use codec::{ChatCodec, ChatRequest, ChatResponse};
use server::{self, ChatServer}; use server::{self, ChatServer};
use codec::{ChatRequest, ChatResponse, ChatCodec};
/// Chat server sends this messages to session /// Chat server sends this messages to session
#[derive(Message)] #[derive(Message)]
@ -25,7 +24,8 @@ pub struct ChatSession {
id: usize, id: usize,
/// this is address of chat server /// this is address of chat server
addr: Addr<Syn, ChatServer>, addr: Addr<Syn, ChatServer>,
/// 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, hb: Instant,
/// joined room /// joined room
room: String, room: String,
@ -46,7 +46,10 @@ impl Actor for ChatSession {
// future within context, but context waits until this future resolves // future within context, but context waits until this future resolves
// before processing any other events. // before processing any other events.
let addr: Addr<Syn, _> = ctx.address(); let addr: Addr<Syn, _> = ctx.address();
self.addr.send(server::Connect{addr: addr.recipient()}) self.addr
.send(server::Connect {
addr: addr.recipient(),
})
.into_actor(self) .into_actor(self)
.then(|res, act, ctx| { .then(|res, act, ctx| {
match res { match res {
@ -55,12 +58,13 @@ impl Actor for ChatSession {
_ => ctx.stop(), _ => ctx.stop(),
} }
actix::fut::ok(()) actix::fut::ok(())
}).wait(ctx); })
.wait(ctx);
} }
fn stopping(&mut self, ctx: &mut Self::Context) -> Running { fn stopping(&mut self, ctx: &mut Self::Context) -> Running {
// notify chat server // notify chat server
self.addr.do_send(server::Disconnect{id: self.id}); self.addr.do_send(server::Disconnect { id: self.id });
Running::Stop Running::Stop
} }
} }
@ -69,49 +73,54 @@ impl actix::io::WriteHandler<io::Error> for ChatSession {}
/// To use `Framed` we have to define Io type and Codec /// To use `Framed` we have to define Io type and Codec
impl StreamHandler<ChatRequest, io::Error> for ChatSession { impl StreamHandler<ChatRequest, io::Error> for ChatSession {
/// This is main event loop for client requests /// This is main event loop for client requests
fn handle(&mut self, msg: ChatRequest, ctx: &mut Context<Self>) { fn handle(&mut self, msg: ChatRequest, ctx: &mut Context<Self>) {
match msg { match msg {
ChatRequest::List => { ChatRequest::List => {
// Send ListRooms message to chat server and wait for response // Send ListRooms message to chat server and wait for response
println!("List rooms"); println!("List rooms");
self.addr.send(server::ListRooms) self.addr
.send(server::ListRooms)
.into_actor(self) .into_actor(self)
.then(|res, act, ctx| { .then(|res, act, ctx| {
match res { match res {
Ok(rooms) => { Ok(rooms) => {
act.framed.write(ChatResponse::Rooms(rooms)); act.framed.write(ChatResponse::Rooms(rooms));
}, }
_ => println!("Something is wrong"), _ => println!("Something is wrong"),
} }
actix::fut::ok(()) actix::fut::ok(())
}).wait(ctx) })
.wait(ctx)
// .wait(ctx) pauses all events in context, // .wait(ctx) pauses all events in context,
// so actor wont receive any new messages until it get list of rooms back // so actor wont receive any new messages until it get list of rooms back
}, }
ChatRequest::Join(name) => { ChatRequest::Join(name) => {
println!("Join to room: {}", name); println!("Join to room: {}", name);
self.room = name.clone(); 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)); self.framed.write(ChatResponse::Joined(name));
}, }
ChatRequest::Message(message) => { ChatRequest::Message(message) => {
// send message to chat server // send message to chat server
println!("Peer message: {}", message); println!("Peer message: {}", message);
self.addr.do_send( self.addr.do_send(server::Message {
server::Message{id: self.id, id: self.id,
msg: message, room: msg: message,
self.room.clone()}) room: self.room.clone(),
})
} }
// we update heartbeat time on ping from peer // we update heartbeat time on ping from peer
ChatRequest::Ping => ChatRequest::Ping => self.hb = Instant::now(),
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<Message> for ChatSession { impl Handler<Message> for ChatSession {
type Result = (); type Result = ();
@ -123,11 +132,17 @@ impl Handler<Message> for ChatSession {
/// Helper methods /// Helper methods
impl ChatSession { impl ChatSession {
pub fn new(
pub fn new(addr: Addr<Syn,ChatServer>, addr: Addr<Syn, ChatServer>,
framed: actix::io::FramedWrite<WriteHalf<TcpStream>, ChatCodec>) -> ChatSession { framed: actix::io::FramedWrite<WriteHalf<TcpStream>, ChatCodec>,
ChatSession {id: 0, addr: addr, hb: Instant::now(), ) -> ChatSession {
room: "Main".to_owned(), framed: framed} ChatSession {
id: 0,
addr: addr,
hb: Instant::now(),
room: "Main".to_owned(),
framed: framed,
}
} }
/// helper method that sends ping to client every second. /// helper method that sends ping to client every second.
@ -141,7 +156,7 @@ impl ChatSession {
println!("Client heartbeat failed, disconnecting!"); println!("Client heartbeat failed, disconnecting!");
// notify chat server // notify chat server
act.addr.do_send(server::Disconnect{id: act.id}); act.addr.do_send(server::Disconnect { id: act.id });
// stop actor // stop actor
ctx.stop(); ctx.stop();
@ -154,7 +169,6 @@ impl ChatSession {
} }
} }
/// Define tcp server that will accept incoming tcp connection and create /// Define tcp server that will accept incoming tcp connection and create
/// chat actors. /// chat actors.
pub struct TcpServer { pub struct TcpServer {
@ -169,14 +183,18 @@ impl TcpServer {
// Our chat server `Server` is an actor, first we need to start it // Our chat server `Server` is an actor, first we need to start it
// and then add stream on incoming tcp connections to it. // and then add stream on incoming tcp connections to it.
// TcpListener::incoming() returns stream of the (TcpStream, net::SocketAddr) items // TcpListener::incoming() returns stream of the (TcpStream, net::SocketAddr)
// So to be able to handle this events `Server` actor has to implement // items So to be able to handle this events `Server` actor has to
// stream handler `StreamHandler<(TcpStream, net::SocketAddr), io::Error>` // implement stream handler `StreamHandler<(TcpStream,
// net::SocketAddr), io::Error>`
let _: () = TcpServer::create(|ctx| { let _: () = TcpServer::create(|ctx| {
ctx.add_message_stream(listener.incoming() ctx.add_message_stream(
.map_err(|_| ()) listener
.map(|(t, a)| TcpConnect(t, a))); .incoming()
TcpServer{chat: chat} .map_err(|_| ())
.map(|(t, a)| TcpConnect(t, a)),
);
TcpServer { chat: chat }
}); });
} }
} }

View File

@ -17,4 +17,4 @@ env_logger = "*"
futures = "0.1" futures = "0.1"
tokio-core = "0.1" tokio-core = "0.1"
actix = "0.5" actix = "0.5"
actix-web = "^0.5" actix-web = "^0.6"

View File

@ -7,13 +7,13 @@ extern crate env_logger;
extern crate futures; extern crate futures;
extern crate tokio_core; extern crate tokio_core;
use std::{io, thread};
use std::time::Duration; use std::time::Duration;
use std::{io, thread};
use actix::*; use actix::*;
use actix_web::ws::WsWriter;
use actix_web::ws::{Client, ClientWriter, Message, ProtocolError};
use futures::Future; use futures::Future;
use actix_web::ws::{Message, ProtocolError, Client, ClientWriter};
fn main() { fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info"); ::std::env::set_var("RUST_LOG", "actix_web=info");
@ -34,25 +34,22 @@ fn main() {
}); });
// start console loop // start console loop
thread::spawn(move|| { thread::spawn(move || loop {
loop { let mut cmd = String::new();
let mut cmd = String::new(); if io::stdin().read_line(&mut cmd).is_err() {
if io::stdin().read_line(&mut cmd).is_err() { println!("error");
println!("error"); return;
return
}
addr.do_send(ClientCommand(cmd));
} }
addr.do_send(ClientCommand(cmd));
}); });
() ()
}) }),
); );
let _ = sys.run(); let _ = sys.run();
} }
struct ChatClient(ClientWriter); struct ChatClient(ClientWriter);
#[derive(Message)] #[derive(Message)]
@ -94,11 +91,10 @@ impl Handler<ClientCommand> for ChatClient {
/// Handle server websocket messages /// Handle server websocket messages
impl StreamHandler<Message, ProtocolError> for ChatClient { impl StreamHandler<Message, ProtocolError> for ChatClient {
fn handle(&mut self, msg: Message, ctx: &mut Context<Self>) { fn handle(&mut self, msg: Message, ctx: &mut Context<Self>) {
match msg { match msg {
Message::Text(txt) => println!("Server: {:?}", txt), Message::Text(txt) => println!("Server: {:?}", txt),
_ => () _ => (),
} }
} }

View File

@ -9,8 +9,8 @@ extern crate actix_web;
extern crate env_logger; extern crate env_logger;
use actix::prelude::*; use actix::prelude::*;
use actix_web::{ use actix_web::ws::WsWriter;
http, middleware, server, fs, ws, App, HttpRequest, HttpResponse, Error}; use actix_web::{fs, http, middleware, server, ws, App, Error, HttpRequest, HttpResponse};
/// do websocket handshake and start `MyWebSocket` actor /// do websocket handshake and start `MyWebSocket` actor
fn ws_index(r: HttpRequest) -> Result<HttpResponse, Error> { fn ws_index(r: HttpRequest) -> Result<HttpResponse, Error> {
@ -27,7 +27,6 @@ impl Actor for MyWebSocket {
/// Handler for `ws::Message` /// Handler for `ws::Message`
impl StreamHandler<ws::Message, ws::ProtocolError> for MyWebSocket { impl StreamHandler<ws::Message, ws::ProtocolError> for MyWebSocket {
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
// process websocket messages // process websocket messages
println!("WS: {:?}", msg); println!("WS: {:?}", msg);