3 releases
Uses new Rust 2024
| 0.1.4 | Jul 21, 2025 |
|---|---|
| 0.1.1 | May 7, 2025 |
| 0.1.0 | Oct 4, 2024 |
#1838 in Database interfaces
286 downloads per month
Used in 49 crates
(8 directly)
35KB
546 lines
MoosicBox Profiles
A simple profile name management system for the MoosicBox ecosystem, providing basic profile name storage and validation for request handling.
Features
- Profile Name Storage: Add, remove, and retrieve profile names
- Profile Validation: Verify profile names exist before processing requests
- Thread-Safe Operations: Safe concurrent access to profile data
- API Integration: Extract profile names from HTTP headers and query parameters
Installation
Add this to your Cargo.toml:
[dependencies]
moosicbox_profiles = "0.1.1"
Usage
Basic Profile Management
use moosicbox_profiles::PROFILES;
fn main() {
// Add a profile
PROFILES.add("user123".to_string());
// Check if profile exists
if let Some(profile) = PROFILES.get("user123") {
println!("Profile found: {}", profile);
}
// Get all profile names
let all_profiles = PROFILES.names();
println!("All profiles: {:?}", all_profiles);
// Remove a profile
PROFILES.remove("user123");
}
API Integration
use moosicbox_profiles::api::{ProfileName, ProfileNameUnverified};
use actix_web::{web, HttpResponse, Result};
// Extract verified profile name from request
async fn handler(profile: ProfileName) -> Result<HttpResponse> {
let profile_name: String = profile.into();
Ok(HttpResponse::Ok().json(format!("Hello, {}", profile_name)))
}
// Extract unverified profile name from request
async fn handler_unverified(profile: ProfileNameUnverified) -> Result<HttpResponse> {
let profile_name: String = profile.into();
// Profile name exists in request but may not be registered
Ok(HttpResponse::Ok().json(format!("Profile: {}", profile_name)))
}
API Features
The package provides request extractors for Actix Web:
- ProfileName: Extracts and validates profile names from requests
- ProfileNameUnverified: Extracts profile names without validation
- Header Support: Reads from
moosicbox-profileheader - Query Parameter Support: Reads from
moosicboxProfilequery parameter
Profile names are extracted in this order of precedence:
- Query parameter
moosicboxProfile - HTTP header
moosicbox-profile
Error Handling
- Missing profile information returns
400 Bad Request - Non-existent profiles return
400 Bad Request - Invalid header values return
400 Bad Request
Thread Safety
All operations are thread-safe using RwLock for concurrent access to the profile storage.
Dependencies
~2–15MB
~137K SLoC