| Crates.io | register-actix-routes |
| lib.rs | register-actix-routes |
| version | 0.1.1 |
| created_at | 2024-12-02 16:45:37.623904+00 |
| updated_at | 2024-12-02 16:45:37.623904+00 |
| description | Macro designed to simplify the registration of routes in an Actix Web application |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1468904 |
| size | 15,398 |
register_actix_routes - A Proc Macro for Dynamic Route Registration in Actix Webregister_routes is a Rust proc macro library designed to simplify the registration of routes in an Actix Web application. It enables automatic grouping and configuration of routes, reducing boilerplate code while maintaining scalability and clarity in route management.
Automatic Route Registration:
#[auto_register("/prefix")] to annotate your handler functions and group them by scope.Dynamic Service Configuration:
register_service functions that automatically configure routes under their respective scopes.Route Listing:
list_routes.Support for All HTTP Verbs:
#[get], #[post], #[put], #[delete], and #[patch].Error Handling and Debugging:
Customizable Tabled Output:
tabled crate.Add the register_routes crate to your project:
[dependencies]
register_routes = "0.1.1"
tabled = "0.17.0" # Required for route listing
#[auto_register]Add #[auto_register("/scope")] to your handler functions to group them by a shared prefix.
use actix_web::{get, web, Responder};
use register_routes::auto_register;
#[auto_register("/events")]
#[get("/search")]
pub async fn search() -> impl Responder {
"Search handler"
}
#[auto_register("/events")]
#[post("/create")]
pub async fn create() -> impl Responder {
"Create handler"
}
register_serviceUse the generate_register_service macro to create a function that registers all handlers for a specific scope.
You'll need it at the end of your handlers file.
When integrating the configuration with an existing scope
use register_routes::generate_register_service;
generate_register_service!(["/events"]);
This will generate a register_service function like:
pub fn register_service(cfg: &mut actix_web::web::ServiceConfig) {
cfg.service(
actix_web::web::scope("")
.service(search)
.service(create)
);
}
If you need the scope created with the path:
use register_routes::generate_register_service;
generate_register_service!(["/events", use_scope = true ]);
This will generate a register_service function like:
pub fn register_service(cfg: &mut actix_web::web::ServiceConfig) {
cfg.service(
actix_web::web::scope("/events")
.service(search)
.service(create)
);
}
Use the generated register_service functions to configure your Actix Web app:
Here an example if you used use_scope = true to generate a scoped list of services
use actix_web::{App, HttpServer};
use crate::handlers::event_handler::register_service as register_event_handlers;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.configure(register_event_handlers) // Register event handlers
})
.bind("127.0.0.1:8080")?
.run()
.await
}
Use the generate_list_routes macro to display all automatically registered routes in a tabular format.
use register_routes::generate_list_routes;
generate_list_routes!();
Call the list_routes function at application startup:
fn main() {
list_routes();
}
This will print:
List of the automatically registered routes:
+--------------------+----------------+----------------+-------+
| Scope | Path | Handler | Verb |
+--------------------+----------------+----------------+-------+
| /events | /search | search | GET |
| /events | /create | create | POST |
+--------------------+----------------+----------------+-------+
The macros provide clear error messages for common mistakes:
#[get("/path")]).auto_register attribute requires a valid scope prefix (e.g., #[auto_register("/events")]).You can wrap entire scopes with middleware while still using register_service:
use actix_web::middleware::Logger;
HttpServer::new(|| {
App::new()
.wrap(Logger::default())
.configure(register_event_handlers)
})
You can use multiple register_service functions to organize routes by feature:
use crate::handlers::{
event_handler::register_service as register_event_handlers,
booking_handler::register_service as register_booking_handlers,
};
HttpServer::new(|| {
App::new()
.configure(register_event_handlers)
.configure(register_booking_handlers)
})
Actix-Specific:
Requires tabled:
tabled crate for pretty output.Contributions, issues, and feature requests are welcome! To contribute:
This project is licensed under the MIT License.
Enjoy clean and scalable route management in your Actix Web projects! 🚀 """