我写了一个基于 rust-warp 的 Web 应用程序,它运行良好。但它只能通过 localhost:3030(127.0.0.1:3030) 访问。现在我想通过公共网络(101.35.56.79)/局域网访问它(192.168.1.18),但它不起作用。我找不到这个问题的相关信息。这是我的代码:
//#![deny(warnings)]
#![allow(non_snake_case)]
#![allow(unused)]
use warp::Filter;
use warp::body::json;
use warp::reply;
use serde_json::json;
// struct Article{
// articleTitle: String,
// articleText: String,
// }
// struct ArticleList {
// articles: Vec<Article>,
// }
#[tokio::main]
async fn main() {
println!("Welcome to little guy's sever console!");
//展示主页
//let show_HomePage = warp::fs::dir("../");
//成功连接后发送一条欢迎信息
let say_hello =
warp::path::end().map(|| {
println!("Someone connected!");
"Welcome to Little Guy's HomePage~
try to visit ./HomePage.html".replace(" ", "")
});
//获取文章
let get_article =
warp::path("getArticles")
.and(warp::path::param())//参数是文章分区
.map(|article_partition: String| {
format!("The article_partition you request is: {}", article_partition);
//let article_list = ArticleList{articles: vec![Article{articleTitle:String::from("title1"), articleText: String::from("text1"})]};
//let article_list = articles{vec![{articleTitle: "tt1"},]};
let article_list = json!({
"articles":[
{
"articleTitle": "tt1",
"articleText": "text1",
},
{
"articleTitle": "tt2",
"articleText": "text2",
}
]
});
warp::reply::json(&article_list)
});
let get_introduction =
warp::path("getIntroduction")
.map(||{
let introduction = json!({
"introduction": {
"introduction": "This is Little Guy's introduction",
}
});
warp::reply::json(&introduction)
});
let routes =
//show_HomePage
say_hello
.or(get_article)
.or(get_introduction);
warp::serve(routes)
.run(([127, 0, 0, 1], 80))
.await;
}
我以为问题出在
.run(([127, 0, 0, 1], 80))
.await;
但我在 warp 的文档( https://docs.rs/warp/latest/warp/ )中找不到有关它的更多详细信息。我需要你的帮助,谢谢。