| Crates.io | moosicbox_middleware |
| lib.rs | moosicbox_middleware |
| version | 0.1.4 |
| created_at | 2024-10-04 17:42:13.288891+00 |
| updated_at | 2025-07-21 19:24:18.946873+00 |
| description | MoosicBox middleware package |
| homepage | |
| repository | https://github.com/MoosicBox/MoosicBox |
| max_upload_size | |
| id | 1396865 |
| size | 53,827 |
Basic HTTP middleware collection for the MoosicBox web server ecosystem, providing request logging and service information utilities for Actix Web applications.
Add this to your Cargo.toml:
[dependencies]
moosicbox_middleware = "0.1.1"
# Enable tunnel middleware
moosicbox_middleware = { version = "0.1.1", features = ["tunnel"] }
use moosicbox_middleware::api_logger::ApiLogger;
use actix_web::{web, App, HttpServer, HttpResponse};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(ApiLogger::new())
.route("/hello", web::get().to(hello_handler))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
async fn hello_handler() -> HttpResponse {
HttpResponse::Ok().json("Hello, World!")
}
The API logger middleware provides:
use moosicbox_middleware::service_info::ServiceInfo;
use actix_web::{web, App, HttpServer};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(ServiceInfo::new("My Music Service", "1.0.0"))
.route("/api/status", web::get().to(status_handler))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
async fn status_handler() -> HttpResponse {
HttpResponse::Ok().json("Service is running")
}
When the tunnel feature is enabled:
use moosicbox_middleware::tunnel_info::TunnelInfo;
use actix_web::{web, App, HttpServer};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(TunnelInfo::new())
.route("/tunnel/api", web::get().to(tunnel_handler))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
async fn tunnel_handler() -> HttpResponse {
HttpResponse::Ok().json("Tunnel endpoint")
}
The ApiLogger middleware logs:
Log output example:
TRACE GET /api/tracks?limit=10 headers=[("range", "bytes=0-1023")] STARTED
TRACE GET /api/tracks?limit=10 headers=[("range", "bytes=0-1023")] resp_headers=[("content-length", "2048"), ("accept-ranges", "bytes")] FINISHED SUCCESS "200 OK" (25 ms)
The ServiceInfo middleware adds service metadata to responses for identification and monitoring purposes.
The TunnelInfo middleware handles tunnel-specific request processing when tunnel features are enabled.
// API Logger
pub struct ApiLogger;
pub struct ApiLoggerMiddleware<S>;
// Service Info
pub struct ServiceInfo;
pub struct ServiceInfoMiddleware<S>;
// Tunnel Info (feature-gated)
#[cfg(feature = "tunnel")]
pub struct TunnelInfo;
#[cfg(feature = "tunnel")]
pub struct TunnelInfoMiddleware<S>;
actix_web: Web framework for middleware integrationfutures-util: For async middleware implementationlog: For logging functionalitytracing: For structured logging supportThis middleware collection provides essential request logging and service identification capabilities for MoosicBox web services.