Giter Club home page Giter Club logo

Comments (2)

chrislearn avatar chrislearn commented on August 31, 2024

Salvo 中的 Router 本身是嵌套的,路径只是 Router 中的一个 Filter,所以,是给 Router 命名还是给 Router 中的路径命名?如果Router 不包含 Path 的 Filter 呢?如果多个 Router 存在相同的路径,是都起同样的名字? 会不会很怪异?会不会路径不同又有相同名字?如何检测冲突?

感觉不好实现,也没太大的意义。

from salvo.

dulumao avatar dulumao commented on August 31, 2024

很有意义的,对于微服务还好,路由少,如果是一个相对复杂的项目,比如一个电商吧,涉及的路由就很多了,后端经常需要生成url,返还给前端,这个时候,去拼凑uri就很麻烦了, 很多类似的框架都有这种功能,其实有个简单的办法,就是PathFilter里的字段给pub,或者将PathParser给设置成pub,这样就可以重新parse,类似:
let filter = PathFilter::new("/api/v1/test/handle_filter_path/guid:my_custom_guid/edit")
因为PathFilter内部自己调用了PathParser,也就无法进行进一步的获取计算了,代码被写死了,
这样,可以自己扩充request trait的方法,增加一个uri拼凑路由,这样就不需要router name了
比如重写一个

#[handler]
// https://0.0.0.0/edit/<user_id>/<article_id>?ref=test_user&ref_code=abc123
async fn handle_edit(req: &mut Request, res: &mut Response) {
    // before url https://0.0.0.0/edit/10/5?ref=test_user&ref_code=abc123
    let params = HashMap::from([("user_id", "1"), ("article_id","100")]);
    let queries = HashMap::from([("ref", "admin"), ("ref_code","11111111")]);
    let url = req.make_uri(req, params, queries);
    // or let url = req.make_uri(“/edit/<user_id>/<article_id>”, params, queries);
    // after url = https://0.0.0.0/edit/1/100?ref=admin&ref_code=11111111

    res.render(Text::Json(
        json!({
            "code": 200,
            "success": true,
            "payload": {
                "redirect_url": url
            }
        })
        .to_string(),
    ))
}

from salvo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.