1
0
mirror of https://github.com/actix/examples synced 2025-02-02 17:39:05 +01:00

41 lines
1.1 KiB
Rust
Raw Normal View History

use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
use awc::{Client, Connector};
use openssl::ssl::{SslConnector, SslMethod};
2020-03-01 08:55:00 -05:00
2020-04-03 16:14:30 +09:00
async fn index(_req: HttpRequest) -> HttpResponse {
2020-04-03 16:16:17 +09:00
let builder = SslConnector::builder(SslMethod::tls()).unwrap();
2020-03-01 08:55:00 -05:00
2020-09-12 16:49:45 +01:00
let client = Client::builder()
.connector(Connector::new().openssl(builder.build()))
2020-03-01 08:55:00 -05:00
.finish();
2020-04-03 16:16:17 +09:00
let now = std::time::Instant::now();
2020-03-01 08:55:00 -05:00
let payload =
client
.get("https://upload.wikimedia.org/wikipedia/commons/b/b9/Pizigani_1367_Chart_1MB.jpg")
2020-03-01 08:55:00 -05:00
.send()
.await
.unwrap()
.body()
.limit(20_000_000) // sets max allowable payload size
.await
.unwrap();
2020-04-03 16:16:17 +09:00
println!(
"awc time elapsed while reading bytes into memory: {} ms",
now.elapsed().as_millis()
);
2020-03-01 08:55:00 -05:00
2020-04-03 16:16:17 +09:00
HttpResponse::Ok().content_type("image/jpeg").body(payload)
2020-03-01 08:55:00 -05:00
}
2020-09-12 16:49:45 +01:00
#[actix_web::main]
2020-03-01 08:55:00 -05:00
async fn main() -> std::io::Result<()> {
let port = 3000;
2020-04-03 16:16:17 +09:00
HttpServer::new(|| App::new().service(web::resource("/").to(index)))
.bind(("0.0.0.0", port))?
.run()
.await
2020-03-01 08:55:00 -05:00
}