Warp: Using `path` vs `index` for files

Created on 15 Oct 2018  路  5Comments  路  Source: seanmonstar/warp

I might not understand the use of index() correctly. In the files.rs example the route to the file is defined with

let readme = warp::get2()
        .and(warp::index())
        .and(warp::fs::file("./README.md"));

but what if you have multiple files that should be routed to, for instance, /file1 and /file2? When I tried to use index for this:

let file1 = warp::get2()
              .and(warp::index())
             .and(warp:fs::file("./file1"));

let file2 = warp::get2()
              .and(warp::index())
             .and(warp:fs::file("./file2"));

only the first defined route in let routes = file1.or(file2) is ever rendered and navigating to /my/project/root/file2 returns file not found.

If instead I define the routes with

let file1 = warp::path("file1")
        .and(warp::fs::file("./file1"));
let file2 = warp::path("file2")
        .and(warp::fs::file("./file2"));
let routes = file1.or(file2);

I get access to both files. It might be helpful to adjust the example to show this explicitly. Also please correct me if I'm not using the index() filter correctly which caused my trouble.

Most helpful comment

It could be that the name index is less useful. It could be deprecated, and recommend using warp::path::end().

All 5 comments

The warp::index() filter specifically matches the end of the path, what some consider an "index" route. So, warp::index() matches /, warp::path("file1").and(warp::index()) matches /file1.

Why not have index take an argument, like warp::index("file1")?

Would you mind expanding a little more on what that would mean?

index is meant as an alias for an equivalent warp::path::end(). This is because you might match a specific path with something like warp::path("static").and(warp::path("file1")), which will match /static/file1, but also static/file1/oogabooga. The index filter is meant to specify that there shouldn't be any more to the path.

So warp::path("static").and(warp::path("file1")).and(warp::index()) would match /static/file1 but NOT /static/file1/oogabooga, correct? I must need to correct my understanding of "index" in this context. To me the "index" is a relative reference point, generally the url matching http:://domain.suffix/. So other pages in that domain would be appended to the index.
Maybe warp::index().and(warp::path("file1")) makes more sense to me. Warp should automatically return an error for undefined routes so index/file1/oogabooga is invalid.

It could be that the name index is less useful. It could be deprecated, and recommend using warp::path::end().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seanmonstar picture seanmonstar  路  8Comments

dylanede picture dylanede  路  3Comments

Apromixately picture Apromixately  路  3Comments

dpc picture dpc  路  7Comments

asaaki picture asaaki  路  7Comments