use std::{net::SocketAddr, path::PathBuf};
use axum::{
Json, Router,
extract::State,
http::{HeaderValue, StatusCode, header},
response::{Html, IntoResponse},
routing::{get, post},
};
use tower_http::{services::ServeDir, set_header::SetResponseHeaderLayer};
use crate::{
api_types::{SeekReq, SeekTsReq, StepReq, StepUntilChangeReq, StepUntilReq},
tape::Viz,
};
impl Viz {
pub async fn bind(port: u16) -> tokio::net::TcpListener {
let addr = SocketAddr::from(([127, 0, 0, 1], port));
tokio::net::TcpListener::bind(addr).await.unwrap_or_else(|e| panic!("bind {addr}: {e}"))
}
pub async fn serve_on(self, listener: tokio::net::TcpListener) {
let no_store = SetResponseHeaderLayer::overriding(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
let web = web_dir().expect(UNSET);
let app = Router::new()
.route("/api/topology", get(topology))
.route("/api/day", get(day))
.route("/api/status", get(status))
.route("/api/step", post(step))
.route("/api/seek", post(seek))
.route("/api/seek_ts", post(seek_ts))
.route("/api/step_until", post(step_until))
.route("/api/step_until_change", post(step_until_change))
.route("/lwc_draw.js", get(lwc_draw))
.nest_service("/wasm", ServeDir::new(web.join("wasm")))
.nest_service("/assets", ServeDir::new(web.join("assets")))
.fallback(index)
.layer(no_store)
.with_state(self);
axum::serve(listener, app).await.expect("axum serve");
}
}
const UNSET: &str = "EXEC_VIZ_WEB_DIR: point it at a `dx build -p exec_viz_web` bundle";
pub fn web_dir() -> Option<PathBuf> {
std::env::var_os("EXEC_VIZ_WEB_DIR").map(PathBuf::from)
}
async fn topology(State(v): State<Viz>) -> impl IntoResponse {
Json(v.lock().topology())
}
async fn day(State(v): State<Viz>) -> impl IntoResponse {
Json(v.lock().day())
}
async fn status(State(v): State<Viz>) -> impl IntoResponse {
Json(v.lock().frame())
}
async fn step(State(v): State<Viz>, Json(req): Json<StepReq>) -> impl IntoResponse {
Json(v.lock().step(req.n))
}
async fn seek(State(v): State<Viz>, Json(req): Json<SeekReq>) -> impl IntoResponse {
Json(v.lock().seek(req.tick))
}
async fn seek_ts(State(v): State<Viz>, Json(req): Json<SeekTsReq>) -> impl IntoResponse {
Json(v.lock().seek_ts(req.ts_ns))
}
async fn step_until(State(v): State<Viz>, Json(req): Json<StepUntilReq>) -> impl IntoResponse {
Json(v.lock().step_until(&req.node))
}
async fn step_until_change(State(v): State<Viz>, Json(req): Json<StepUntilChangeReq>) -> impl IntoResponse {
Json(v.lock().step_until_change(&req.nodes))
}
async fn index(method: axum::http::Method) -> impl IntoResponse {
if method != axum::http::Method::GET {
return (StatusCode::NOT_FOUND, format!("no such route for {method}")).into_response();
}
let path = web_dir().expect(UNSET).join("index.html");
match tokio::fs::read_to_string(&path).await {
Ok(s) => Html(s).into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("read {}: {e}", path.display())).into_response(),
}
}
async fn lwc_draw() -> impl IntoResponse {
([(header::CONTENT_TYPE, HeaderValue::from_static("text/javascript"))], include_str!("../assets/lwc_draw.js"))
}