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

Update basics/basics to v4 (#474)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Luca Palmieri 2022-02-02 01:24:28 +00:00 committed by GitHub
parent 9aef457a78
commit 8dcc610d23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 201 additions and 73 deletions

216
Cargo.lock generated
View File

@ -183,9 +183,9 @@ dependencies = [
[[package]]
name = "actix-files"
version = "0.6.0-beta.15"
version = "0.6.0-beta.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66862400e0b79538bc52926dadd965d01c9320b0477eecedac2d3d6bda5e9b92"
checksum = "0b49f1b48724a52605ba40b67ede24f5a6cbc246817f9278d280d393a28e8b0e"
dependencies = [
"actix-http 3.0.0-rc.1",
"actix-service 2.0.2",
@ -327,9 +327,9 @@ dependencies = [
[[package]]
name = "actix-multipart"
version = "0.4.0-beta.12"
version = "0.4.0-beta.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8362a002d669c85fea5504737a68b9f2d5538069ddd9ccd7d6d5f3eaa62cc000"
checksum = "c59b1f14a8b2bc14df9be544d173f5390da5b62d531e406fd0f0ce9b825fea5a"
dependencies = [
"actix-utils 3.0.0",
"actix-web 4.0.0-rc.1",
@ -367,7 +367,7 @@ dependencies = [
"actix-http 2.2.2",
"actix-rt 1.1.1",
"actix-service 1.0.6",
"actix-session",
"actix-session 0.4.1",
"actix-utils 2.0.0",
"actix-web 3.3.3",
"backoff",
@ -510,6 +510,23 @@ dependencies = [
"time 0.2.27",
]
[[package]]
name = "actix-session"
version = "0.5.0-beta.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8984fa6256873b5075143ef662898ed6699bcd0ca094d8a6e40048c12af8d591"
dependencies = [
"actix-service 2.0.2",
"actix-utils 3.0.0",
"actix-web 4.0.0-rc.1",
"derive_more",
"futures-util",
"log",
"serde 1.0.136",
"serde_json",
"time 0.3.7",
]
[[package]]
name = "actix-testing"
version = "1.0.1"
@ -579,7 +596,7 @@ name = "actix-todo"
version = "2.0.0"
dependencies = [
"actix-files 0.3.0",
"actix-session",
"actix-session 0.4.1",
"actix-web 3.3.3",
"diesel",
"dotenv",
@ -800,6 +817,15 @@ dependencies = [
"generic-array 0.14.5",
]
[[package]]
name = "aead"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b613b8e1e3cf911a086f53f03bf286f52fd7a7258e4fa606f0ef220d39d8877"
dependencies = [
"generic-array 0.14.5",
]
[[package]]
name = "aes"
version = "0.6.0"
@ -808,7 +834,19 @@ checksum = "884391ef1066acaa41e766ba8f596341b96e93ce34f9a43e7d24bf0a0eaf0561"
dependencies = [
"aes-soft",
"aesni",
"cipher",
"cipher 0.2.5",
]
[[package]]
name = "aes"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e8b47f52ea9bae42228d07ec09eb676433d7c4ed1ebdf0f1d1c29ed446f1ab8"
dependencies = [
"cfg-if 1.0.0",
"cipher 0.3.0",
"cpufeatures",
"opaque-debug 0.3.0",
]
[[package]]
@ -817,11 +855,25 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5278b5fabbb9bd46e24aa69b2fdea62c99088e0a950a9be40e3e0101298f88da"
dependencies = [
"aead",
"aes",
"cipher",
"ctr",
"ghash",
"aead 0.3.2",
"aes 0.6.0",
"cipher 0.2.5",
"ctr 0.6.0",
"ghash 0.3.1",
"subtle",
]
[[package]]
name = "aes-gcm"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df5f85a83a7d8b0442b6aa7b504b8212c1733da07b98aae43d4bc21b2cb3cdf6"
dependencies = [
"aead 0.4.3",
"aes 0.7.5",
"cipher 0.3.0",
"ctr 0.8.0",
"ghash 0.4.4",
"subtle",
]
@ -831,7 +883,7 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be14c7498ea50828a38d0e24a765ed2effe92a705885b57d029cd67d45744072"
dependencies = [
"cipher",
"cipher 0.2.5",
"opaque-debug 0.3.0",
]
@ -841,7 +893,7 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea2e11f5e94c2f7d386164cc2aa1f97823fed6f259e486940a71c174dd01b0ce"
dependencies = [
"cipher",
"cipher 0.2.5",
"opaque-debug 0.3.0",
]
@ -1336,11 +1388,13 @@ checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
name = "basics"
version = "2.0.0"
dependencies = [
"actix-files 0.3.0",
"actix-session",
"actix-utils 2.0.0",
"actix-web 3.3.3",
"env_logger 0.8.4",
"actix-files 0.6.0-beta.16",
"actix-session 0.5.0-beta.7",
"actix-utils 3.0.0",
"actix-web 4.0.0-rc.1",
"env_logger 0.9.0",
"tokio 1.16.1",
"tokio-stream",
]
[[package]]
@ -1622,7 +1676,7 @@ dependencies = [
"num-integer",
"num-traits 0.2.14",
"serde 1.0.136",
"time 0.1.44",
"time 0.1.43",
"winapi 0.3.9",
]
@ -1657,6 +1711,15 @@ dependencies = [
"generic-array 0.14.5",
]
[[package]]
name = "cipher"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ee52072ec15386f770805afd189a01c8841be8696bed250fa2f13c4c0d6dfb7"
dependencies = [
"generic-array 0.14.5",
]
[[package]]
name = "clap"
version = "2.34.0"
@ -1759,9 +1822,9 @@ version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03a5d7b21829bc7b4bf4754a978a241ae54ea55a40f92bb20216e54096f4b951"
dependencies = [
"aes-gcm",
"aes-gcm 0.8.0",
"base64 0.13.0",
"hkdf",
"hkdf 0.10.0",
"hmac 0.10.1",
"percent-encoding",
"rand 0.8.4",
@ -1776,7 +1839,14 @@ version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94d4706de1b0fa5b132270cddffa8585166037822e260a944fe161acd137ca05"
dependencies = [
"aes-gcm 0.9.4",
"base64 0.13.0",
"hkdf 0.12.2",
"hmac 0.12.0",
"percent-encoding",
"rand 0.8.4",
"sha2 0.10.1",
"subtle",
"time 0.3.7",
"version_check 0.9.4",
]
@ -1795,7 +1865,7 @@ dependencies = [
name = "cookie-session"
version = "1.0.0"
dependencies = [
"actix-session",
"actix-session 0.4.1",
"actix-web 3.3.3",
"env_logger 0.8.4",
]
@ -1968,7 +2038,16 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb4a30d54f7443bf3d6191dcd486aca19e67cb3c49fa7a06a319966346707e7f"
dependencies = [
"cipher",
"cipher 0.2.5",
]
[[package]]
name = "ctr"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "049bb91fb4aaf0e3c7efa6cd5ef877dbbbd15b39dad06d9948de4ec8a75761ea"
dependencies = [
"cipher 0.3.0",
]
[[package]]
@ -2187,6 +2266,7 @@ dependencies = [
"block-buffer 0.10.0",
"crypto-common",
"generic-array 0.14.5",
"subtle",
]
[[package]]
@ -2597,7 +2677,7 @@ checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"wasi 0.10.2+wasi-snapshot-preview1",
]
[[package]]
@ -2607,7 +2687,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97304e4cd182c3846f7575ced3890c53012ce534ad9114046b0a9e00bb30a375"
dependencies = [
"opaque-debug 0.3.0",
"polyval",
"polyval 0.4.5",
]
[[package]]
name = "ghash"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1583cc1656d7839fd3732b80cf4f38850336cdb9b8ded1cd399ca62958de3c99"
dependencies = [
"opaque-debug 0.3.0",
"polyval 0.5.3",
]
[[package]]
@ -2764,6 +2854,15 @@ dependencies = [
"hmac 0.10.1",
]
[[package]]
name = "hkdf"
version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "158bc31e00a68e380286904cc598715f861f2b0ccf7aa6fe20c6d0c49ca5d0f6"
dependencies = [
"hmac 0.12.0",
]
[[package]]
name = "hmac"
version = "0.9.0"
@ -2794,6 +2893,15 @@ dependencies = [
"digest 0.9.0",
]
[[package]]
name = "hmac"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddca131f3e7f2ce2df364b57949a9d47915cfbd35e46cfee355ccebbf794d6a2"
dependencies = [
"digest 0.10.1",
]
[[package]]
name = "hostname"
version = "0.3.1"
@ -3741,7 +3849,7 @@ dependencies = [
"serde_json",
"sha1",
"sha2 0.8.2",
"time 0.1.44",
"time 0.1.43",
"twox-hash",
"uuid 0.8.2",
]
@ -4237,6 +4345,18 @@ dependencies = [
"universal-hash",
]
[[package]]
name = "polyval"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8419d2b623c7c0896ff2d5d96e2cb4ede590fed28fcc34934f4c33c036e620a1"
dependencies = [
"cfg-if 1.0.0",
"cpufeatures",
"opaque-debug 0.3.0",
"universal-hash",
]
[[package]]
name = "postgres-protocol"
version = "0.5.3"
@ -4579,7 +4699,7 @@ dependencies = [
"actix-redis",
"actix-rt 1.1.1",
"actix-service 1.0.6",
"actix-session",
"actix-session 0.4.1",
"actix-web 3.3.3",
"env_logger 0.8.4",
"rand 0.7.3",
@ -4866,9 +4986,9 @@ checksum = "3e52c148ef37f8c375d49d5a73aa70713125b7f19095948a923f80afdeb22ec2"
[[package]]
name = "rust_decimal"
version = "1.20.0"
version = "1.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0593ce4677e3800ddafb3de917e8397b1348e06e688128ade722d88fbe11ebf"
checksum = "4214023b1223d02a4aad9f0bb9828317634a56530870a2eaf7200a99c0c10f68"
dependencies = [
"arrayvec 0.7.2",
"num-traits 0.2.14",
@ -4940,7 +5060,7 @@ dependencies = [
name = "rustls-example"
version = "1.0.0"
dependencies = [
"actix-files 0.6.0-beta.15",
"actix-files 0.6.0-beta.16",
"actix-web 4.0.0-rc.1",
"env_logger 0.9.0",
"rustls 0.20.2",
@ -5034,9 +5154,9 @@ dependencies = [
[[package]]
name = "security-framework"
version = "2.5.0"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d09d3c15d814eda1d6a836f2f2b56a6abc1446c8a34351cb3180d3db92ffe4ce"
checksum = "3fed7948b6c68acbb6e20c334f55ad635dc0f75506963de4464289fbd3b051ac"
dependencies = [
"bitflags",
"core-foundation",
@ -5047,9 +5167,9 @@ dependencies = [
[[package]]
name = "security-framework-sys"
version = "2.5.0"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e90dd10c41c6bfc633da6e0c659bd25d31e0791e5974ac42970267d59eba87f7"
checksum = "a57321bf8bc2362081b2599912d2961fe899c0efadf1b4b2f8d48b3e253bb96c"
dependencies = [
"core-foundation-sys",
"libc",
@ -5266,6 +5386,17 @@ dependencies = [
"opaque-debug 0.3.0",
]
[[package]]
name = "sha2"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99c3bd8169c58782adad9290a9af5939994036b76187f7b4f0e6de91dbbfc0ec"
dependencies = [
"cfg-if 1.0.0",
"cpufeatures",
"digest 0.10.1",
]
[[package]]
name = "shlex"
version = "1.1.0"
@ -5540,7 +5671,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
name = "static_index"
version = "2.0.0"
dependencies = [
"actix-files 0.6.0-beta.15",
"actix-files 0.6.0-beta.16",
"actix-web 4.0.0-rc.1",
"env_logger 0.9.0",
]
@ -5839,12 +5970,11 @@ dependencies = [
[[package]]
name = "time"
version = "0.1.44"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi 0.3.9",
]
@ -6670,9 +6800,9 @@ checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "wasm-bindgen"
@ -7100,9 +7230,9 @@ dependencies = [
[[package]]
name = "zeroize"
version = "1.5.1"
version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4062c749be08d90be727e9c5895371c3a0e49b90ba2b9592dc7afda95cc2b719"
checksum = "7c88870063c39ee00ec285a2f8d6a966e5b6fb2becc4e8dac77ed0d370ed6006"
[[package]]
name = "zstd"

View File

@ -5,8 +5,10 @@ authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
edition = "2018"
[dependencies]
actix-web = "3"
actix-files = "0.3"
actix-session = "0.4"
actix-utils = "2"
env_logger = "0.8"
actix-web = "4.0.0-beta.21"
actix-files = "0.6.0-beta.15"
actix-session = "0.5.0-beta.7"
actix-utils = "3"
tokio = "1"
tokio-stream = "0.1"
env_logger = "0.9"

View File

@ -1,12 +1,13 @@
use actix_files as fs;
use actix_session::{CookieSession, Session};
use actix_utils::mpsc;
use actix_web::http::{header, Method, StatusCode};
use actix_web::{
error, get, guard, middleware, web, App, Error, HttpRequest, HttpResponse,
error, get, middleware, web, App, Either, Error, HttpRequest, HttpResponse,
HttpServer, Result,
};
use std::{env, io};
use tokio::sync::mpsc;
use tokio_stream::wrappers::UnboundedReceiverStream;
/// favicon handler
#[get("/favicon")]
@ -27,7 +28,7 @@ async fn welcome(session: Session, req: HttpRequest) -> Result<HttpResponse> {
}
// set counter to session
session.set("counter", counter)?;
session.insert("counter", counter)?;
// response
Ok(HttpResponse::build(StatusCode::OK)
@ -35,31 +36,36 @@ async fn welcome(session: Session, req: HttpRequest) -> Result<HttpResponse> {
.body(include_str!("../static/welcome.html")))
}
/// 404 handler
async fn p404() -> Result<fs::NamedFile> {
Ok(fs::NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND))
async fn default_handler(
request: HttpRequest,
) -> Result<Either<fs::NamedFile, HttpResponse>> {
match *request.method() {
actix_web::http::Method::GET => {
let file = fs::NamedFile::open("static/404.html")?
.set_status_code(StatusCode::NOT_FOUND);
Ok(Either::Left(file))
}
_ => Ok(Either::Right(HttpResponse::MethodNotAllowed().finish())),
}
}
/// response body
async fn response_body(path: web::Path<String>) -> HttpResponse {
let text = format!("Hello {}!", *path);
let (tx, rx_body) = mpsc::channel();
let (tx, rx_body) = mpsc::unbounded_channel();
let _ = tx.send(Ok::<_, Error>(web::Bytes::from(text)));
HttpResponse::Ok().streaming(rx_body)
HttpResponse::Ok().streaming(UnboundedReceiverStream::from(rx_body))
}
/// handler with path parameters like `/user/{name}/`
async fn with_param(
req: HttpRequest,
web::Path((name,)): web::Path<(String,)>,
) -> HttpResponse {
async fn with_param(req: HttpRequest, path: web::Path<(String,)>) -> HttpResponse {
println!("{:?}", req);
HttpResponse::Ok()
.content_type("text/plain")
.body(format!("Hello {}!", name))
.body(format!("Hello {}!", path.0))
}
#[actix_web::main]
@ -102,21 +108,11 @@ async fn main() -> io::Result<()> {
.service(web::resource("/").route(web::get().to(|req: HttpRequest| {
println!("{:?}", req);
HttpResponse::Found()
.header(header::LOCATION, "static/welcome.html")
.insert_header((header::LOCATION, "static/welcome.html"))
.finish()
})))
// default
.default_service(
// 404 for GET request
web::resource("")
.route(web::get().to(p404))
// all requests that are not `GET`
.route(
web::route()
.guard(guard::Not(guard::Get()))
.to(HttpResponse::MethodNotAllowed),
),
)
.default_service(web::route().to(default_handler))
})
.bind("127.0.0.1:8080")?
.run()