diff --git a/src/routes/licenses.rs b/src/routes/licenses.rs new file mode 100644 index 00000000..19a48a6d --- /dev/null +++ b/src/routes/licenses.rs @@ -0,0 +1,62 @@ +use axum::response::IntoResponse; +use axum::routing::get; +use axum::{Json, Router}; +use itertools::Itertools; +use serde_json::{json, Value}; + +use crate::server::appstate::AppState; + +async fn packages() -> Json { + Json(json!([])) +} + +async fn hardcoded() -> Json { + Json(json!([{ + "Attributions": [], + "Package": "bifrost", + "SPDX-License-Identifiers": [ + "GPL-3.0" + ], + "SourceLinks": [ + "https://github.com/chrivers/bifrost", + ], + "Version": "0.9", + "Website": "https://github.com/chrivers/bifrost", + "licenses": { + "GPL-3.0": "gpl-3.0.txt", + } + }])) +} + +async fn license() -> impl IntoResponse { + const LICENSE: &str = include_str!("../../LICENSE"); + + let split = LICENSE + .find("Preamble") + .expect("License file must have preamble"); + + /* a bit of string trickery to make license render nicely in hue app */ + format!( + "{}{}", + &LICENSE[..split] + .split("\n\n ") + .map(|s| s.replace("\n ", " ")) + .join("\n\n"), + &LICENSE[split..] + .split("\n\n ") + .map(|s| s.replace("\n ", "\n").replace('\n', " ")) + .join("\n\n") + ) +} + +async fn rust_packages() -> Json { + Json(json!([])) +} + +pub fn router() -> Router { + Router::new() + .route("/packages.json", get(packages)) + .route("/hardcoded.json", get(hardcoded)) + .route("/rust-packages.json", get(rust_packages)) + .route("/gpl-3.0.txt", get(license)) +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 682e7f29..431f3d84 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -10,6 +10,7 @@ use crate::server::appstate::AppState; pub mod api; pub mod clip; pub mod eventstream; +pub mod licenses; impl IntoResponse for ApiError { fn into_response(self) -> Response { @@ -36,6 +37,7 @@ impl IntoResponse for ApiError { pub fn router(appstate: AppState) -> Router<()> { Router::new() .nest("/api", api::router()) + .nest("/licenses", licenses::router()) .nest("/clip/v2/resource", clip::router()) .nest("/eventstream", eventstream::router()) .with_state(appstate)