ci / rust (pull_request) Successful in 3m0s
The CLI, TUI and end-to-end harness each carried their own client: three reqwest builders with three timeout policies, three copies of the success-or-body check, and route strings at every call site. Two of them discarded the daemon's structured error and reported only a status code. Deletes `maestro-cli/src/http.rs`, both `ws_base` copies in the TUI, the CLI's partial `DaemonInfo` (it dropped `version` and `presets`), and the e2e harness's `check_ok`. `maestro-web` gains a protocol dependency and stops building the error envelope with a format string and reading it back as untyped JSON; the daemon and the proxy now register routes from the same constants. `maestro_web::router` returns `Result` — an unusable daemon URL fails at startup instead of at first connect. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
57 lines
2.2 KiB
Rust
57 lines
2.2 KiB
Rust
//! HTTP server wiring — composes the static bundle, the HTTP proxy, and the
|
|
//! WS proxy into a single axum router.
|
|
|
|
use axum::extract::{Request, State, WebSocketUpgrade};
|
|
use axum::response::{IntoResponse, Response};
|
|
use axum::routing::{any, get};
|
|
use axum::Router;
|
|
use maestro_protocol::routes::{self, UrlError};
|
|
|
|
use crate::config::WebConfig;
|
|
use crate::proxy::{pump_ws, upstream_ws_url, ProxyState, UpstreamConfig};
|
|
use crate::static_bundle;
|
|
|
|
/// Build the public router. The returned router is ready to hand to
|
|
/// `axum::serve`.
|
|
pub fn router(config: &WebConfig) -> Result<Router, UrlError> {
|
|
let upstream = UpstreamConfig::from_daemon_url(&config.daemon_url)?;
|
|
let proxy_state = ProxyState::new(upstream);
|
|
|
|
// Mount each route at its real path (not under `nest`, which would strip
|
|
// the `/api` prefix before the proxy could forward it). WS routes go
|
|
// first so the HTTP fallback doesn't shadow them.
|
|
Ok(Router::new()
|
|
.route(routes::patterns::ATTACH, any(ws_proxy))
|
|
.route(routes::EVENTS, any(ws_proxy))
|
|
.route("/", get(serve_index))
|
|
.fallback(catch_all)
|
|
.with_state(proxy_state))
|
|
}
|
|
|
|
/// Default handler: `/api/*` goes to the daemon, anything else falls through
|
|
/// to the embedded SPA bundle (with SPA-style fallback to `index.html`).
|
|
async fn catch_all(State(state): State<ProxyState>, req: Request) -> Response {
|
|
if is_api_path(req.uri().path()) {
|
|
return crate::proxy::proxy_http(State(state), req).await;
|
|
}
|
|
static_bundle::serve_path(req.uri().path())
|
|
}
|
|
|
|
/// Split point between the proxied daemon surface and the SPA bundle. A daemon
|
|
/// route added outside [`routes::API_PREFIX`] would silently be answered with
|
|
/// `index.html` instead of being forwarded.
|
|
fn is_api_path(path: &str) -> bool {
|
|
path.starts_with(&format!("{}/", routes::API_PREFIX))
|
|
}
|
|
|
|
async fn ws_proxy(State(state): State<ProxyState>, ws: WebSocketUpgrade, req: Request) -> Response {
|
|
let upstream_url = upstream_ws_url(&state, req.uri());
|
|
ws.on_upgrade(move |socket| async move {
|
|
pump_ws(socket, upstream_url).await;
|
|
})
|
|
}
|
|
|
|
async fn serve_index() -> Response {
|
|
static_bundle::serve_index().into_response()
|
|
}
|