synthwave/src/route/mod.rs
2024-08-12 18:48:33 +02:00

48 lines
916 B
Rust

use rocket::{
get,
response::{status::BadRequest, Redirect},
uri,
};
use serde_json::json;
pub mod album;
pub mod artist;
pub mod playlist;
pub mod track;
pub mod user;
// todo : rework api
type ApiError = BadRequest<serde_json::Value>;
type FallibleApiResponse = Result<serde_json::Value, ApiError>;
pub fn api_error(msg: &str) -> ApiError {
BadRequest(json!({
"error": msg
}))
}
pub trait ToAPI: Sized {
/// Generate public API JSON
fn api(&self) -> impl std::future::Future<Output = serde_json::Value>;
}
pub async fn to_api(albums: &[impl ToAPI]) -> Vec<serde_json::Value> {
let mut ret = Vec::new();
for e in albums {
ret.push(e.api().await);
}
ret
}
#[get("/")]
pub fn index_redir() -> Redirect {
Redirect::to(uri!("/web"))
}
#[get("/manifest.json")]
pub fn manifest_redir() -> Redirect {
Redirect::to(uri!("/web/manifest.json"))
}