rustrust-warp

When I use warp,it just work on localhost, but not public network


I wrote a Web application based on rust-warp,it works well.But it can just access by localhost:3030(127.0.0.1:3030).Now I want to access it by public network(101.35.56.79)/Local Area Network(192.168.1.18),but it didn't work.I can't find the relevant information to this problem.Here's my code:

    //#![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;
}

I thought the problem is on

        .run(([127, 0, 0, 1], 80))
        .await;

but I can't find more details about it in warp's document(https://docs.rs/warp/latest/warp/).I need your help,Thanks.


Solution

  • 你好。You are correct about where the error lies. In the provided code, the server is binding to 127.0.0.1 on port 80. To make the server accessible from all interfaces, change the binding address to 0.0.0.0 for the same port. The new code should read.

    warp::serve(routes)
        .run(([0, 0, 0, 0], 80))
        .await;