synthwave/src/route/mod.rs

49 lines
916 B
Rust
Raw Normal View History

2024-08-01 14:42:58 +00:00
use rocket::{
get,
response::{status::BadRequest, Redirect},
uri,
};
2024-07-24 09:07:24 +00:00
use serde_json::json;
pub mod album;
2024-07-26 12:14:08 +00:00
pub mod artist;
2024-08-11 19:56:55 +00:00
pub mod playlist;
2024-07-24 09:07:24 +00:00
pub mod track;
pub mod user;
2024-08-01 14:36:54 +00:00
// todo : rework api
2024-07-24 09:07:24 +00:00
type ApiError = BadRequest<serde_json::Value>;
type FallibleApiResponse = Result<serde_json::Value, ApiError>;
pub fn api_error(msg: &str) -> ApiError {
BadRequest(json!({
"error": msg
}))
}
2024-08-01 14:36:54 +00:00
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
}
2024-08-01 14:42:58 +00:00
#[get("/")]
2024-08-12 16:48:33 +00:00
pub fn index_redir() -> Redirect {
2024-08-01 14:42:58 +00:00
Redirect::to(uri!("/web"))
}
2024-08-07 19:56:09 +00:00
#[get("/manifest.json")]
2024-08-12 16:48:33 +00:00
pub fn manifest_redir() -> Redirect {
2024-08-07 19:56:09 +00:00
Redirect::to(uri!("/web/manifest.json"))
}