Fix routing

This commit is contained in:
Austen Adler 2023-10-06 20:37:43 -04:00
parent 4b5ab8f0ac
commit ccc1a9556c
4 changed files with 39 additions and 23 deletions

9
Cargo.lock generated
View File

@ -1643,9 +1643,9 @@ dependencies = [
[[package]] [[package]]
name = "http-range-header" name = "http-range-header"
version = "0.3.1" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" checksum = "3ce4ef31cda248bbdb6e6820603b82dfcd9e833db65a43e997a0ccec777d11fe"
[[package]] [[package]]
name = "httparse" name = "httparse"
@ -2960,9 +2960,8 @@ dependencies = [
[[package]] [[package]]
name = "tower-http" name = "tower-http"
version = "0.4.4" version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/austenadler/tower-http?branch=serve-dir-mount-point#0b6ba8e6b70514753961ed93600c5372530b729c"
checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140"
dependencies = [ dependencies = [
"bitflags 2.4.0", "bitflags 2.4.0",
"bytes", "bytes",

View File

@ -14,6 +14,7 @@ serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107" serde_json = "1.0.107"
tokio = { version = "1.32.0", features = ["net", "rt", "macros", "rt-multi-thread", "process", "io-util"] } tokio = { version = "1.32.0", features = ["net", "rt", "macros", "rt-multi-thread", "process", "io-util"] }
tower = "0.4.13" tower = "0.4.13"
tower-http = { version = "0.4.4", features = ["fs", "tracing", "trace"] } # Use this until https://github.com/tokio-rs/axum/issues/1731 works
tower-http = { git = "https://github.com/austenadler/tower-http", branch = "serve-dir-mount-point", features = ["fs", "tracing", "trace"] }
tracing = "0.1.37" tracing = "0.1.37"
tracing-subscriber = "0.3.17" tracing-subscriber = "0.3.17"

View File

@ -36,11 +36,11 @@
</head> </head>
<body> <body>
<div id="links"> <div id="links">
<a href="std/std/" id="std-docs" target="main" onclick="focusFrame()">STD</a> <a href="global/std/" id="std-docs" target="main" onclick="focusFrame()">STD</a>
<a href="local/tracing/" id="local-docs" target="main" onclick="focusFrame()">Cargo</a> <a href="local/" id="local-docs" target="main" onclick="focusFrame()">Cargo</a>
</div> </div>
<iframe name="main" id="main" src="local/tracing/"></iframe> <iframe name="main" id="main" src="local/"></iframe>
</body> </body>
<script type="text/javascript"> <script type="text/javascript">
@ -63,9 +63,9 @@
function focusFrame() { function focusFrame() {
frame.contentWindow.focus(); frame.contentWindow.focus();
frame.addEventListener("load", function() { frame.addEventListener("load", function() {
if (this.contentWindow.window.searchState) { // if (this.contentWindow.window.searchState) {
this.contentWindow.window.searchState.focus(); // this.contentWindow.window.searchState.focus();
} // }
frame.contentWindow.removeEventListener("keydown", cl); frame.contentWindow.removeEventListener("keydown", cl);
frame.contentWindow.addEventListener("keydown", cl); frame.contentWindow.addEventListener("keydown", cl);
}); });

View File

@ -1,11 +1,14 @@
#![feature(try_blocks)] #![feature(try_blocks)]
#![feature(absolute_path)]
use anyhow::bail; use anyhow::bail;
use anyhow::Context; use anyhow::Context;
use anyhow::Result; use anyhow::Result;
use axum::http::header::CONTENT_SECURITY_POLICY; use axum::http::header::CONTENT_SECURITY_POLICY;
use axum::http::header::X_FRAME_OPTIONS; use axum::http::header::X_FRAME_OPTIONS;
use axum::http::HeaderMap; use axum::http::HeaderMap;
use axum::http::Uri;
use axum::response::Html; use axum::response::Html;
use axum::response::Redirect;
use axum::routing::get; use axum::routing::get;
use clap::Parser; use clap::Parser;
use serde::Deserialize; use serde::Deserialize;
@ -20,7 +23,9 @@ use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader; use tokio::io::BufReader;
use tokio::process::Command; use tokio::process::Command;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tower_http::trace::DefaultOnResponse;
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use tracing::Level;
use axum::Router; use axum::Router;
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
@ -52,18 +57,34 @@ async fn main() -> Result<()> {
async fn start_http(std_docs_path: PathBuf, build_docs_path: PathBuf) -> Result<()> { async fn start_http(std_docs_path: PathBuf, build_docs_path: PathBuf) -> Result<()> {
let app = Router::new() let app = Router::new()
.route("/", get(get_index)) .route("/", get(get_index))
.nest_service("/local", ServeDir::new(build_docs_path)) .nest_service(
.nest_service("/std", ServeDir::new(std_docs_path)) "/global/",
.layer(TraceLayer::new_for_http()); ServeDir::new(std_docs_path).with_mount_point("/global"),
)
.nest_service(
"/local/",
ServeDir::new(build_docs_path).with_mount_point("/local"),
)
.route("/global", get(add_slash))
.route("/local", get(add_slash))
.layer(TraceLayer::new_for_http().on_response(DefaultOnResponse::new().level(Level::INFO)));
let addr = SocketAddr::from_str("127.0.0.1:8888").unwrap(); let addr = SocketAddr::from_str("127.0.0.1:8888").unwrap();
info!("Listening on address {}", addr); info!("Listening on address http://{}", addr);
axum::Server::bind(&addr) axum::Server::bind(&addr)
.serve(app.into_make_service()) .serve(app.into_make_service())
.await .await
.map_err(Into::into) .map_err(Into::into)
} }
async fn add_slash(uri: Uri) -> Result<Redirect, ()> {
if uri.path().ends_with('/') {
Err(())
} else {
Ok(Redirect::permanent(&(uri.path().to_string() + "/")))
}
}
async fn get_index() -> (HeaderMap, Html<&'static [u8]>) { async fn get_index() -> (HeaderMap, Html<&'static [u8]>) {
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
headers.insert(X_FRAME_OPTIONS, "SAMEORIGIN".parse().unwrap()); headers.insert(X_FRAME_OPTIONS, "SAMEORIGIN".parse().unwrap());
@ -72,13 +93,6 @@ async fn get_index() -> (HeaderMap, Html<&'static [u8]>) {
} }
async fn build_docs() -> Result<PathBuf> { async fn build_docs() -> Result<PathBuf> {
// let options = DocOptions {
// open_result: false,
// compile_opts: CompileOptions {
// }
// }
let mut child = Command::new("cargo") let mut child = Command::new("cargo")
.args(["doc", "--keep-going", "--message-format=json"]) .args(["doc", "--keep-going", "--message-format=json"])
.stdout(Stdio::piped()) .stdout(Stdio::piped())
@ -140,6 +154,8 @@ async fn build_docs() -> Result<PathBuf> {
.context("Failed to join reader handle")???; .context("Failed to join reader handle")???;
// info!("with output path: {:?}", output_path); // info!("with output path: {:?}", output_path);
info!("Last successful artifact: {output_path:?}");
let output_path = output_path let output_path = output_path
.parent() .parent()
.map(|p| p.parent()) .map(|p| p.parent())