axum_rh
Axum router helper helps you build an API simply by using macros to define the router and all endpoints.
Install
To add axum-rh to your project, run:
Middlewares
All middlewares are optional and can be overridden by your own implementations.
Logging
A simple logger middleware that can also send logs to Betterstack (with the async feature). It formats request logs.
use init_logger;
use middlewares;
async
Sessions
A default implementation of session management using Redis storage. This middleware will be updated to be more user-friendly in the future.
Auth
A default JWT authentication middleware. The JWT token requires a user_id field. This middleware will also be updated to be more user-friendly in the future.
How to:
Declare Router
To declare a router, use the RouterHelper derive macro and the router_config attribute to define the state type and router definitions.
;
Use States
To use states in your router, define a state struct and include it in the router_config attribute.
;
Declare Endpoints
To declare an endpoint, use two macros. The first macro is applied to the implementation of the module definition.
;
The base_path argument is optional and defaults to / if not set. The second macro is applied to each function definition under the struct implementation.
;
HTTP Methods
Define endpoints using the following macros:
-
GET: Define a GET endpoint.
-
POST: Define a POST endpoint.
-
PUT: Define a PUT endpoint.
-
DELETE: Define a DELETE endpoint.
Return Values
The ApiResponse struct defines a default response type for your endpoints. You can use it or define your own response type as long as it implements IntoResponse.
Example: