| 8 | |
| 9 | #[get("/")] |
| 10 | async fn fetch_image(client: ThinData<Client>) -> HttpResponse { |
| 11 | let start = Instant::now(); |
| 12 | |
| 13 | let mut res = client.get(MAP_URL).send().await.unwrap(); |
| 14 | |
| 15 | if !res.status().is_success() { |
| 16 | log::error!("Wikipedia did not return expected image"); |
| 17 | return HttpResponse::InternalServerError().finish(); |
| 18 | } |
| 19 | |
| 20 | let payload = res |
| 21 | .body() |
| 22 | // expected image is larger than default body limit, set up a higher |
| 23 | // limit of 20 MB |
| 24 | .limit(20_000_000) |
| 25 | .await |
| 26 | .unwrap(); |
| 27 | |
| 28 | log::info!( |
| 29 | "It took {}ms to download image to memory", |
| 30 | start.elapsed().as_millis() |
| 31 | ); |
| 32 | |
| 33 | HttpResponse::Ok() |
| 34 | .content_type(mime::IMAGE_JPEG) |
| 35 | .body(payload) |
| 36 | } |
| 37 | |
| 38 | #[actix_web::main] |
| 39 | async fn main() -> std::io::Result<()> { |