Hello,
I'm trying to make a very simple HTTP server returning one result page that includes an image created by the server. I'm using the simple example (Static Files) but I get errors.
Here's an excerpt of the code:
use actix_web::{web, App, HttpResponse, HttpServer, HttpRequest, Error};
use actix_web::Result as ActixResult;
use actix_files::NamedFile;
fn main() {
let server = HttpServer::new(|| {
App::new()
.route("/", web::get().to(get_index))
.route("/{id}.png", web::get().to(get_image))
.route("/calc", web::post().to(post_temp_rh))
});
// ...
server
.bind(url).expect("error binding server to address")
.run().expect("error running server");
}
// ...
fn get_image(req_: HttpRequest) -> ActixResult<NamedFile> {
Ok(NamedFile::open("result.png")?)
}
Just for information, the HTML created by post_temp_rh()
has an image tag, that I'm using in the .route("/{id}.png",
and which looks like this, where <id>
is a timestamp (to avoid caching side-effects) :
<img src="result.<id>.png">
There is only one image, so I'm not bothering parsing the filename in get_image
.
Before I added the route to get_image
, it was compiling and working perfectly. When I added this route, I got these messages:
error[E0277]: the trait bound `fn(HttpRequest) -> Result<NamedFile, actix_web::Error> {get_image}: Factory<_, _, _>` is not satisfied
--> src\main.rs:68:54
|
68 | .route("/result.gif\\?.*", web::get().to(get_image))
| -- ^^^^^^^^^ the trait `Factory<_, _, _>` is not implemented for `fn(HttpRequest) -> Result<NamedFile, actix_web::Error> {get_image}`
| |
| required by a bound introduced by this call
|
note: required by a bound in `Route::to`
--> C:\Users\tom\.cargo\registry\src\github.com-1ecc6299db9ec823\actix-web-2.0.0\src\route.rs:228:12
|
228 | F: Factory<T, R, U>,
| ^^^^^^^^^^^^^^^^ required by this bound in `Route::to`
First I found out that I had to replace Result
by ActixResult
, an unfortunate choice of type since Result
is already used in Rust and needs 2 parameters. I tried by making it async, but that didn't change anything (and I don't need that).
Any idea about what I'm doing wrong?