make live reloading not work in the sandbox
This commit is contained in:
parent
38e8ed32c2
commit
aff885cf17
4 changed files with 147 additions and 76 deletions
|
@ -8,22 +8,21 @@ edition = "2021"
|
|||
treehouse-format = { workspace = true }
|
||||
|
||||
anyhow = "1.0.75"
|
||||
axum = "0.6.20"
|
||||
axum = "0.7.4"
|
||||
clap = { version = "4.3.22", features = ["derive"] }
|
||||
codespan-reporting = "0.11.1"
|
||||
copy_dir = "0.1.3"
|
||||
env_logger = "0.10.0"
|
||||
log = { workspace = true }
|
||||
handlebars = "4.3.7"
|
||||
http-body = "1.0.0"
|
||||
pulldown-cmark = { version = "0.9.3", default-features = false }
|
||||
rand = "0.8.5"
|
||||
serde = { version = "1.0.183", features = ["derive"] }
|
||||
serde_json = "1.0.105"
|
||||
tokio = { version = "1.32.0", features = ["full"] }
|
||||
toml_edit = { version = "0.19.14", features = ["serde"] }
|
||||
tower-http = { version = "0.4.3", features = ["fs"] }
|
||||
tower-livereload = "0.8.0"
|
||||
tower-http = { version = "0.5.1", features = ["fs"] }
|
||||
tower-livereload = "0.9.2"
|
||||
walkdir = "2.3.3"
|
||||
ulid = "1.0.0"
|
||||
rand = "0.8.5"
|
||||
serde_json = "1.0.105"
|
||||
|
||||
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
use std::{path::PathBuf, sync::Arc};
|
||||
#[cfg(debug_assertions)]
|
||||
mod live_reload;
|
||||
|
||||
use std::{net::Ipv4Addr, path::PathBuf, sync::Arc};
|
||||
|
||||
use anyhow::Context;
|
||||
use axum::{
|
||||
extract::{RawQuery, State},
|
||||
response::Html,
|
||||
response::{Html, IntoResponse, Response},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use log::{error, info};
|
||||
use pulldown_cmark::escape::escape_html;
|
||||
use tokio::net::TcpListener;
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
use crate::state::{Source, Treehouse};
|
||||
|
@ -18,6 +22,7 @@ use super::Paths;
|
|||
struct SystemPages {
|
||||
four_oh_four: String,
|
||||
b_docs: String,
|
||||
sandbox: String,
|
||||
}
|
||||
|
||||
struct Server {
|
||||
|
@ -30,6 +35,7 @@ pub async fn serve(treehouse: Treehouse, paths: &Paths<'_>, port: u16) -> anyhow
|
|||
let app = Router::new()
|
||||
.nest_service("/", ServeDir::new(paths.target_dir))
|
||||
.route("/b", get(branch))
|
||||
.route("/sandbox", get(sandbox))
|
||||
.with_state(Arc::new(Server {
|
||||
treehouse,
|
||||
target_dir: paths.target_dir.to_owned(),
|
||||
|
@ -38,16 +44,30 @@ pub async fn serve(treehouse: Treehouse, paths: &Paths<'_>, port: u16) -> anyhow
|
|||
.context("cannot read 404 page")?,
|
||||
b_docs: std::fs::read_to_string(paths.target_dir.join("_treehouse/b.html"))
|
||||
.context("cannot read /b documentation page")?,
|
||||
sandbox: std::fs::read_to_string(paths.target_dir.join("static/html/sandbox.html"))
|
||||
.context("cannot read sandbox page")?,
|
||||
},
|
||||
}));
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
let app = app.layer(tower_livereload::LiveReloadLayer::new());
|
||||
let app = live_reload::live_reload(app);
|
||||
|
||||
info!("serving on port {port}");
|
||||
Ok(axum::Server::bind(&([0, 0, 0, 0], port).into())
|
||||
.serve(app.into_make_service())
|
||||
.await?)
|
||||
let listener = TcpListener::bind((Ipv4Addr::from([0u8, 0, 0, 0]), port)).await?;
|
||||
Ok(axum::serve(listener, app).await?)
|
||||
}
|
||||
|
||||
async fn sandbox(State(state): State<Arc<Server>>) -> Response {
|
||||
// Small hack to prevent the LiveReloadLayer from injecting itself into the sandbox.
|
||||
// The sandbox is always nested under a different page, so there's no need to do that.
|
||||
let mut response = Html(state.system_pages.sandbox.clone()).into_response();
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
response
|
||||
.extensions_mut()
|
||||
.insert(live_reload::DisableLiveReload);
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
async fn branch(RawQuery(named_id): RawQuery, State(state): State<Arc<Server>>) -> Html<String> {
|
||||
|
|
17
crates/treehouse/src/cli/serve/live_reload.rs
Normal file
17
crates/treehouse/src/cli/serve/live_reload.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use axum::{http::Response, Router};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct DisableLiveReload;
|
||||
|
||||
pub fn live_reload(router: Router) -> Router {
|
||||
router.layer(tower_livereload::LiveReloadLayer::new().response_predicate(
|
||||
|response: &Response<_>| {
|
||||
let is_html = response
|
||||
.headers()
|
||||
.get("Content-Type")
|
||||
.is_some_and(|v| v == "text/html");
|
||||
let is_disabled = response.extensions().get::<DisableLiveReload>().is_some();
|
||||
is_html && !is_disabled
|
||||
},
|
||||
))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue