2025-11-02 23:37:11 +01:00
|
|
|
use axum::{extract::Path, http::StatusCode, response::{IntoResponse, Response}, routing::get, Router};
|
|
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
use std::sync::Arc;
|
2025-11-15 18:37:30 +01:00
|
|
|
use tracing::{info, warn};
|
2025-11-02 23:37:11 +01:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
use crate::persist::Persist;
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct HttpState {
|
|
|
|
|
persist: Arc<Persist>,
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-15 18:37:30 +01:00
|
|
|
pub fn build_router(persist: Arc<Persist>) -> Router {
|
|
|
|
|
let state = HttpState { persist };
|
2025-11-02 23:37:11 +01:00
|
|
|
Router::new()
|
2025-11-18 11:48:09 +01:00
|
|
|
.route("/jobs/{request_id}/logs", get(get_logs_moved))
|
2025-11-02 23:37:11 +01:00
|
|
|
.with_state(state)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-18 11:48:09 +01:00
|
|
|
async fn get_logs_moved(
|
2025-11-02 23:37:11 +01:00
|
|
|
Path(request_id): Path<String>,
|
2025-11-18 11:48:09 +01:00
|
|
|
_state: axum::extract::State<HttpState>,
|
2025-11-02 23:37:11 +01:00
|
|
|
) -> Response {
|
2025-11-18 11:48:09 +01:00
|
|
|
let base = std::env::var("LOGS_BASE_URL").ok();
|
|
|
|
|
let msg = if let Some(b) = base.as_ref() {
|
|
|
|
|
format!("Logs have moved: {}/jobs/{}/logs", b.trim_end_matches('/'), request_id)
|
|
|
|
|
} else {
|
|
|
|
|
"Logs endpoint moved to logs-service; set LOGS_BASE_URL to enable 302 redirects".to_string()
|
2025-11-02 23:37:11 +01:00
|
|
|
};
|
2025-11-18 11:48:09 +01:00
|
|
|
if let Some(b) = base {
|
|
|
|
|
let loc = format!("{}/jobs/{}/logs", b.trim_end_matches('/'), request_id);
|
|
|
|
|
return (
|
|
|
|
|
StatusCode::MOVED_PERMANENTLY,
|
|
|
|
|
[(axum::http::header::LOCATION, loc.as_str())],
|
|
|
|
|
msg,
|
|
|
|
|
).into_response();
|
2025-11-02 23:37:11 +01:00
|
|
|
}
|
2025-11-18 11:48:09 +01:00
|
|
|
(StatusCode::GONE, msg).into_response()
|
2025-11-02 23:37:11 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-15 18:37:30 +01:00
|
|
|
pub async fn serve(addr: SocketAddr, persist: Arc<Persist>, shutdown: impl std::future::Future<Output = ()>) {
|
|
|
|
|
let app = build_router(persist);
|
2025-11-02 23:37:11 +01:00
|
|
|
info!(%addr, "http server starting");
|
|
|
|
|
let listener = tokio::net::TcpListener::bind(addr).await.expect("bind http");
|
|
|
|
|
let server = axum::serve(listener, app);
|
|
|
|
|
let _ = tokio::select! {
|
|
|
|
|
_ = server => {},
|
|
|
|
|
_ = shutdown => {},
|
|
|
|
|
};
|
|
|
|
|
}
|