问题
I have a simple web server using Warp that serves static files. I followed the examples in the Warp docs to get the following:
let index_html = warp::path("index.html").map(handlers::index_html); // reply is handled in `handlers::`
let script_js = warp::path("script.js").map(handlers::script_js);
let routes = warp::get().and(index_html.or(script_js));
warp::serve(routes).run(([127, 0, 0, 1], 8000)).await;
This returns files when requested from localhost:8000/index.html
and localhost:8000/script.js
.
I want to serve the index file from localhost:8000
rather than /index.html
, but I'm not sure how to specify the domain root with warp::path
. I've tried replacing warp::path("index.html")
with
warp::path()
warp::path("")
warp::path("/")
but with no success.
回答1:
To target the root path use warp::path::end()
. The docs have an ambiguously brief description.
For the example above the code for index_html
would be replaced with:
let index_html = warp::path::end().map( ... );
来源:https://stackoverflow.com/questions/63003414/how-can-i-match-the-root-path-with-a-warp-filter