当我使用 warp 时,它只适用于本地主机,但不适用于 public 网络
When I use warp,it just work on localhost, but not public network
我写了一个基于 rust-warp 的 Web 应用程序,它工作 well.But 它只能通过 localhost:3030(127.0.0.1:3030) 访问。现在我想通过 public network(101.35.56.79)/Local Area Network(192.168.1.18),但是没有work.I找不到相关信息到这个problem.Here我的代码:
//#![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/)。我需要你的帮助,谢谢。
你好。你说的对,错在哪里。在提供的代码中,服务器绑定到端口 80
上的 127.0.0.1
。要使服务器可从所有接口访问,请将同一端口的绑定地址更改为 0.0.0.0
。新代码应为。
warp::serve(routes)
.run(([0, 0, 0, 0], 80))
.await;
我写了一个基于 rust-warp 的 Web 应用程序,它工作 well.But 它只能通过 localhost:3030(127.0.0.1:3030) 访问。现在我想通过 public network(101.35.56.79)/Local Area Network(192.168.1.18),但是没有work.I找不到相关信息到这个problem.Here我的代码:
//#![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/)。我需要你的帮助,谢谢。
你好。你说的对,错在哪里。在提供的代码中,服务器绑定到端口 80
上的 127.0.0.1
。要使服务器可从所有接口访问,请将同一端口的绑定地址更改为 0.0.0.0
。新代码应为。
warp::serve(routes)
.run(([0, 0, 0, 0], 80))
.await;