Skip to content

Commit

Permalink
make await work with plane
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed Feb 10, 2025
1 parent 0a4d15d commit d0d5ad1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,22 @@ impl<T> FromRequestParts<T> for WriteUser {
ready(WriteUser::from_parts_sync(parts))
}
}

pub struct VerifiedPath(pub String);

impl<T> FromRequestParts<T> for VerifiedPath {
type Rejection = (StatusCode, String);

fn from_request_parts(
parts: &mut Parts,
_body: &T,
) -> impl Future<Output = Result<Self, Self::Rejection>> {
let header = if let Some(header) = parts.headers.get("x-verified-path") {
header.to_str().unwrap_or_else(|_| "/").to_string()
} else {
"/".to_string()
};

std::future::ready(Ok(Self(header)))
}
}
10 changes: 8 additions & 2 deletions src/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ServerState;
use crate::{auth::VerifiedPath, ServerState};
use axum::{
body::Body,
extract::{Request, State},
Expand Down Expand Up @@ -31,13 +31,19 @@ fn translate_request(mut req: Request<Body>, port: u16) -> http::Request<SimpleB

pub async fn proxy_request(
State(server_state): State<Arc<ServerState>>,
VerifiedPath(path): VerifiedPath,
req: Request<Body>,
) -> Response<SimpleBody> {
if !server_state.wrapped_server.running() {
let stdout = server_state.wrapped_server.stdout();
let handlebars = Handlebars::new();
let await_url = format!("{path}_frag/await");

let rendered = handlebars
.render_template(ERROR_TEMPLATE, &serde_json::json!({ "stdout": stdout }))
.render_template(
ERROR_TEMPLATE,
&serde_json::json!({ "stdout": stdout, "await_url": await_url }),
)
.unwrap();

let body = to_simple_body(rendered);
Expand Down
2 changes: 1 addition & 1 deletion src/template/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>Artifact ServerError</title>
<script>
async function waitForReload() {
const response = await fetch('/_frag/await');
const response = await fetch('{await_url}');
if (response.ok) {
location.reload();
}
Expand Down

0 comments on commit d0d5ad1

Please sign in to comment.