| Crates.io | toolcraft-axum-kit |
| lib.rs | toolcraft-axum-kit |
| version | 0.2.3 |
| created_at | 2025-08-06 04:01:17.690344+00 |
| updated_at | 2025-08-06 04:01:17.690344+00 |
| description | Toolcraft axum kit module |
| homepage | |
| repository | https://github.com/code-serenade/toolcraft.git |
| max_upload_size | |
| id | 1783366 |
| size | 43,294 |
A comprehensive toolkit for building Axum web services with built-in middleware, error handling, and response utilities.
Add this to your Cargo.toml:
[dependencies]
toolcraft-axum-kit = "*"
Or with specific features:
[dependencies]
toolcraft-axum-kit = { version = "*", features = ["jwt"] }
Check the crates.io page for the latest version.
use axum::{routing::get, Router};
use toolcraft_axum_kit::{start, CommonOk};
#[tokio::main]
async fn main() {
// Create your router
let app = Router::new()
.route("/", get(handler))
.route("/health", get(health));
// Start the server
start("0.0.0.0:3000", app).await;
}
async fn handler() -> CommonOk<String> {
CommonOk("Hello, World!".to_string())
}
async fn health() -> CommonOk<&'static str> {
CommonOk("OK")
}
The toolkit provides a standardized response format for consistency across your API:
use toolcraft_axum_kit::{CommonOk, CommonError, CommonResponse};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct User {
id: u64,
name: String,
}
// Success response
async fn get_user() -> CommonOk<User> {
let user = User {
id: 1,
name: "Alice".to_string(),
};
CommonOk(user)
}
// Error response
async fn not_found() -> CommonError {
CommonError::not_found("User not found")
}
// Using Result type
async fn get_user_by_id(id: u64) -> Result<CommonOk<User>, CommonError> {
if id == 0 {
return Err(CommonError::bad_request("Invalid user ID"));
}
Ok(CommonOk(User {
id,
name: "Bob".to_string(),
}))
}
use axum::Router;
use toolcraft_axum_kit::middleware::cors::cors_layer;
let app = Router::new()
.route("/api/users", get(list_users))
.layer(cors_layer());
When the jwt feature is enabled:
use axum::{Router, routing::get};
use toolcraft_axum_kit::middleware::auth_mw::{auth_layer, Claims};
use axum::Extension;
// Configure your JWT settings
let jwt_config = toolcraft_jwt::JwtCfg {
access_secret: "your-secret".to_string(),
refresh_secret: "your-refresh-secret".to_string(),
audience: "your-app".to_string(),
access_token_duration: 3600,
refresh_token_duration: 86400,
access_key_validate_exp: true,
refresh_key_validate_exp: true,
};
let jwt = toolcraft_jwt::Jwt::new(jwt_config);
// Protected routes
let protected_routes = Router::new()
.route("/profile", get(get_profile))
.layer(auth_layer(jwt.clone()));
// Handler with authentication
async fn get_profile(Extension(claims): Extension<Claims>) -> CommonOk<String> {
CommonOk(format!("Hello, user {}", claims.sub))
}
let app = Router::new()
.nest("/api", protected_routes)
.route("/login", post(login));
The toolkit provides comprehensive error handling:
use toolcraft_axum_kit::{CommonError, error::ErrorCode};
// Using predefined error methods
async fn handler() -> Result<CommonOk<String>, CommonError> {
// Bad request (400)
return Err(CommonError::bad_request("Invalid input"));
// Unauthorized (401)
return Err(CommonError::unauthorized("Please login"));
// Forbidden (403)
return Err(CommonError::forbidden("Access denied"));
// Not found (404)
return Err(CommonError::not_found("Resource not found"));
// Internal server error (500)
return Err(CommonError::internal_server_error("Something went wrong"));
}
// Custom error codes
async fn custom_error() -> CommonError {
CommonError::new(ErrorCode::Custom(422), "Validation failed")
}
use toolcraft_axum_kit::{CommonResponse, IntoCommonResponse, Empty};
// Different response types
async fn success_with_data() -> CommonResponse {
CommonOk("Success").into_common_response()
}
async fn success_empty() -> CommonResponse {
CommonOk(Empty).into_common_response()
}
async fn error_response() -> CommonResponse {
CommonError::bad_request("Invalid request").into_common_response()
}
// Using Result with ResponseResult
use toolcraft_axum_kit::{Result, ResponseResult};
async fn flexible_handler(id: u64) -> ResponseResult<User> {
if id == 0 {
return Err(CommonError::bad_request("Invalid ID"));
}
Ok(CommonOk(User {
id,
name: "Alice".to_string(),
}))
}
start(addr: &str, app: Router) - Start the HTTP serverCommonOk<T> - Success response wrapperCommonError - Error response wrapperCommonResponse - Generic response typeResponseResult<T> - Result type alias for handlersEmpty - Empty response bodyErrorCode::BadRequest - 400ErrorCode::Unauthorized - 401ErrorCode::Forbidden - 403ErrorCode::NotFound - 404ErrorCode::InternalServerError - 500ErrorCode::Custom(u16) - Custom status codecors_layer() - CORS middleware layerauth_layer(jwt: Jwt) - JWT authentication middleware (requires jwt feature)jwt - Enable JWT authentication middleware (enabled by default)This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.