Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get different file with different parameters in get request #526

Closed
ohaggmyr opened this issue Apr 7, 2020 · 2 comments
Closed

Get different file with different parameters in get request #526

ohaggmyr opened this issue Apr 7, 2020 · 2 comments

Comments

@ohaggmyr
Copy link

ohaggmyr commented Apr 7, 2020

I am trying to write a get request where the user can enter something like host/id/<image_id> and get the desired image with that id. I read the documentation and tried this:

let get_image = warp::path::param().and_then(|id: String| async move {
    if id != "0" {
        Ok(warp::fs::file(format!("./images/{}.png", id)))
    } else {
        Err(warp::reject::not_found())
    }
});
let routes = get_image;
warp::serve(routes).run(([0, 0, 0, 0], 3030)).await;

This gave me an error:

the trait bound impl warp::filter::FilterClone: warp::reply::Reply is not satisfied
required because of the requirements on the impl of warp::reply::Reply for (impl warp::filter::FilterClone,)rustc(E0277)
server.rs(25, 17): required by this bound in warp::server::serve

Can you help me explain how I can solve this?

thank you so much!

Best regards

Oscar

@jonas-hagen
Copy link

I have the same issue. I would like to serve a file under a certain URL, but the path on local storage comes from the database.
Something like this:

use warp::Filter;
use warp::Rejection;

pub async fn get_path(_id: String) -> Result<String, Rejection> {
    // could make call to database
    Ok("./README.md".into())
}

#[tokio::main]
async fn main() {
    pretty_env_logger::init();

    let file = warp::path::param().and_then(get_path).map(|path: String| {
        // now serving the file at path
        // warp::fs::file_reply(...) ????
        // warp::reply::file(...) ????
        // analogous to:
        warp::reply::html(path)
    });

    warp::serve(file).run(([127, 0, 0, 1], 3030)).await;
}

Do I see correctly that warp currently has no built in (public) reply helpers to do this? Even though something like this must already be somewhere since the warp::filters::fs::file filter exists?

@jxs
Copy link
Collaborator

jxs commented Jul 8, 2020

Hi, yeah the fs filter can't be used like that, see #171 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants