use std::sync::Arc;
use axum::Json;
use axum::Router;
use axum::extract::{Path, State};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use forge_jobs::Storage;
use crate::Error;
use crate::dto::{QueueOverviewDto, SetBackoffRequest, StorageInfoDto};
use crate::handlers;
pub fn build(storage: Arc<Storage>) -> Router {
Router::new()
.route("/health", get(health))
.route("/metrics", get(metrics_route))
.route("/storage/info", get(storage_info_route))
.route("/queue/overview", get(queue_overview_route))
.route("/queue/{name}/backoff", post(queue_set_backoff_route))
.with_state(storage)
}
async fn metrics_route(State(storage): State<Arc<Storage>>) -> Result<Response, Error> {
let body = crate::metrics::render(&storage).await?;
Ok((
[(
axum::http::header::CONTENT_TYPE,
"text/plain; version=0.0.4; charset=utf-8",
)],
body,
)
.into_response())
}
async fn health() -> &'static str {
"ok"
}
async fn storage_info_route(
State(storage): State<Arc<Storage>>,
) -> Result<Json<StorageInfoDto>, Error> {
handlers::storage_info(&storage).await.map(Json)
}
async fn queue_overview_route(
State(storage): State<Arc<Storage>>,
) -> Result<Json<Vec<QueueOverviewDto>>, Error> {
handlers::queue_overview(&storage).await.map(Json)
}
async fn queue_set_backoff_route(
State(storage): State<Arc<Storage>>,
Path(name): Path<String>,
Json(body): Json<SetBackoffRequest>,
) -> Result<(), Error> {
handlers::queue_set_backoff(
&storage,
&name,
body.enabled,
body.base_seconds,
body.max_seconds,
)
.await
}