mirror of
https://github.com/actix/examples
synced 2025-06-28 09:50:36 +02:00
Restructure folders (#411)
This commit is contained in:
committed by
GitHub
parent
9db98162b2
commit
c3407627d0
9
security/awc_https/Cargo.toml
Normal file
9
security/awc_https/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "awc_https"
|
||||
version = "0.1.0"
|
||||
authors = ["dowwie <dkcdkg@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = { version = "3", features = ["openssl"] }
|
||||
openssl = "0.10.28"
|
9
security/awc_https/README.md
Normal file
9
security/awc_https/README.md
Normal file
@ -0,0 +1,9 @@
|
||||
The goal of this example is to show you how to use the actix-web client (awc)
|
||||
for https related communication. As of actix-web 2.0.0, one must be very
|
||||
careful about setting up https communication. **You could use the default
|
||||
awc api without configuring ssl but performance will be severely diminished**.
|
||||
|
||||
This example downloads a 10MB image from wikipedia.
|
||||
|
||||
To run:
|
||||
> curl http://localhost:3000 -o image.jpg
|
42
security/awc_https/src/main.rs
Normal file
42
security/awc_https/src/main.rs
Normal file
@ -0,0 +1,42 @@
|
||||
use actix_web::{
|
||||
client::{Client, Connector},
|
||||
web, App, HttpRequest, HttpResponse, HttpServer,
|
||||
};
|
||||
use openssl::ssl::{SslConnector, SslMethod};
|
||||
|
||||
async fn index(_req: HttpRequest) -> HttpResponse {
|
||||
let builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
||||
|
||||
let client = Client::builder()
|
||||
.connector(Connector::new().ssl(builder.build()).finish())
|
||||
.finish();
|
||||
|
||||
let now = std::time::Instant::now();
|
||||
let payload =
|
||||
client
|
||||
.get("https://upload.wikimedia.org/wikipedia/commons/f/ff/Pizigani_1367_Chart_10MB.jpg")
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.body()
|
||||
.limit(20_000_000) // sets max allowable payload size
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!(
|
||||
"awc time elapsed while reading bytes into memory: {} ms",
|
||||
now.elapsed().as_millis()
|
||||
);
|
||||
|
||||
HttpResponse::Ok().content_type("image/jpeg").body(payload)
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let port = 3000;
|
||||
|
||||
HttpServer::new(|| App::new().service(web::resource("/").to(index)))
|
||||
.bind(("0.0.0.0", port))?
|
||||
.run()
|
||||
.await
|
||||
}
|
Reference in New Issue
Block a user