How can I match the root path with a Warp filter?

三世轮回 提交于 2021-01-01 09:04:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!