actix-web
actix-web copied to clipboard
not using handlers declared with routing macro should be detectable
Hello,
I've been using actix-web for a while and it's working very well! There is one little thing that's been bothering me, though. When I write services with actix-web I really like to use the macros for specifying method and path, as in the example below:
use actix_web::{get, web, App, HttpServer, Responder};
#[get("/hello/{name}")]
async fn greet(name: web::Path<String>) -> impl Responder {
format!("Hello {name}!")
}
#[actix_web::main] // or #[tokio::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().service(greet)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
I think it's great to have the path and method next to the implementation of the endpoint. However, nearly every time I add a new endpoint I forget to add the service to the app in when setting up the server, leading to a confused developer and a 404 when testing it out :)
I am not experienced enough with Rust's macro system to know if it's actually possible to do, but it would really be great if just using the macro could somehow add the service.
Magically adding services to the routing tree is a non-goal of Actix Web.
There might be something we can tweak to make them flag to cargo check as unused code though.
In particular, it might make sense to copy the original function's visibility instead of making it always "pub" (which causes rust to not do use checks).
https://github.com/actix/actix-web/blob/7b27493e4c660f41dcf3cac8a9b0580acf8df4e2/actix-web-codegen/src/route.rs#L319
@robjtede I'm interested in this feature and would like to contribute if possible. A draft implementation of your proposal has been done, but I'm not quite sure if this is the idiomatic way because it's my first time working with syn.