4 ms·
Is the example program in Actix-web considered good programming style for that framework? The reason I ask is that the equivalent in Python/Flask might look lik
by cabalamat 7y ago
Is the example program in Actix-web considered good programming style for that framework?
The reason I ask is that the equivalent in Python/Flask might look like:
@app.route("/<name>/<id>/index.html")
def index(name, id):
return form("Hello {}! id:{}", name, id)
This has the advantages that:
1. the endpoint "/<name>/<id>/index.html" goes in the code right above the function that implements it.
2. the variables are called `name` and `id` and not `info.0` and `info.1` which is a less informative naming convention.
- killercup 7y agoI can't speak for the actix-web maintainers but I did find this in their examples: #[get("/resource1/{name}/index.html")] fn index(req: HttpRequest, name: web::Path<String>) -> String { println!("REQ: {:?}", req); format!("Hello: {}!\r\n", name) } see https://github.com/actix/actix-web/blob/e399e01a22b8a848ecbbc21c362878cd59a4342e/examples/basic.rs#L7-L11 https://github.com/actix/actix-web/blob/e399e01a22b8a848ecbb...
- lemmox 7y agoThat syntax refers to fields in a tuple. Path parameters can be deserialized into a type like this: [derive(Deserialize)] struct HelloWorldParams { id: String, name: String, } fn index(info: web::Path<HelloWorldParams>) -> impl Responder { format!("Hello {}! id:{}", info.name, info.id) }
- tracker1 7y agoMight want to give rocket a look, as it's closer to what you're describing. The raw numbers aren't in line with actix-web, but definitely an interesting project. Just starting to get my toes wet with Rust, so not as familiar as some here. [1] https://rocket.rs/ https://rocket.rs/