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

View File

@ -14,6 +14,7 @@ serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
tokio = { version = "1.32.0", features = ["net", "rt", "macros", "rt-multi-thread", "process", "io-util"] }
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-subscriber = "0.3.17"

View File

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

View File

@ -1,11 +1,14 @@
#![feature(try_blocks)]
#![feature(absolute_path)]
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use axum::http::header::CONTENT_SECURITY_POLICY;
use axum::http::header::X_FRAME_OPTIONS;
use axum::http::HeaderMap;
use axum::http::Uri;
use axum::response::Html;
use axum::response::Redirect;
use axum::routing::get;
use clap::Parser;
use serde::Deserialize;
@ -20,7 +23,9 @@ use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader;
use tokio::process::Command;
use tokio::task::JoinHandle;
use tower_http::trace::DefaultOnResponse;
use tower_http::trace::TraceLayer;
use tracing::Level;
use axum::Router;
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<()> {
let app = Router::new()
.route("/", get(get_index))
.nest_service("/local", ServeDir::new(build_docs_path))
.nest_service("/std", ServeDir::new(std_docs_path))
.layer(TraceLayer::new_for_http());
.nest_service(
"/global/",
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();
info!("Listening on address {}", addr);
info!("Listening on address http://{}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.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]>) {
let mut headers = HeaderMap::new();
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> {
// let options = DocOptions {
// open_result: false,
// compile_opts: CompileOptions {
// }
// }
let mut child = Command::new("cargo")
.args(["doc", "--keep-going", "--message-format=json"])
.stdout(Stdio::piped())
@ -140,6 +154,8 @@ async fn build_docs() -> Result<PathBuf> {
.context("Failed to join reader handle")???;
// info!("with output path: {:?}", output_path);
info!("Last successful artifact: {output_path:?}");
let output_path = output_path
.parent()
.map(|p| p.parent())