Skip to content

Commit b37a7af

Browse files
committed
Template 404 page to account for public_address
If the service is not hosted at the root of a domain, the links to the graphql and graphiql endpoints cannot be absolute.
1 parent f9e23b6 commit b37a7af

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

Cargo.lock

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ clap = { version = "4.5.48", features = ["derive"] }
1919
uuid = { version = "1.18.1", features = ["serde"] }
2020
tracing = "0.1.41"
2121
tracing-subscriber = "0.3.20"
22+
askama = "0.14.0"
2223

2324
[dev-dependencies]
2425
http-body-util = "0.1.3"

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ COPY ./Cargo.lock ./Cargo.lock
1717
RUN cargo build --release --target x86_64-unknown-linux-musl
1818

1919
COPY ./static ./static
20+
COPY ./templates ./templates
2021
COPY ./src ./src
2122

2223
RUN touch src/main.rs && cargo build --release --target x86_64-unknown-linux-musl

src/main.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use askama::Template;
12
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
23
use axum::http::StatusCode;
34
use axum::response::{Html, IntoResponse};
@@ -56,7 +57,7 @@ async fn serve(config: GlazedConfig) -> Result<(), Box<dyn std::error::Error>> {
5657
.clone()
5758
.unwrap_or_else(|| Url::parse(&format!("http://{}", config.bind_address)).unwrap());
5859
let schema = Schema::build(TiledQuery, EmptyMutation, EmptySubscription)
59-
.data(RootAddress(public_address))
60+
.data(RootAddress(public_address.clone()))
6061
.data(client.clone())
6162
.finish();
6263

@@ -75,7 +76,11 @@ async fn serve(config: GlazedConfig) -> Result<(), Box<dyn std::error::Error>> {
7576
.with_state(client)
7677
.fallback((
7778
StatusCode::NOT_FOUND,
78-
Html(include_str!("../static/404.html")),
79+
Html(
80+
NotFound::from_public_address(public_address.clone())
81+
.render()
82+
.expect("Rendering to a string shouldn't fail"),
83+
),
7984
))
8085
.layer(Extension(schema));
8186

@@ -87,6 +92,22 @@ async fn serve(config: GlazedConfig) -> Result<(), Box<dyn std::error::Error>> {
8792
.await?)
8893
}
8994

95+
#[derive(Template)]
96+
#[template(path = "404.html")]
97+
struct NotFound {
98+
graphql_address: Url,
99+
graphiql_address: Url,
100+
}
101+
102+
impl NotFound {
103+
fn from_public_address(add: Url) -> Self {
104+
Self {
105+
graphql_address: add.join("graphql").unwrap(),
106+
graphiql_address: add.join("graphiql").unwrap(),
107+
}
108+
}
109+
}
110+
90111
async fn graphql_get_warning() -> impl IntoResponse {
91112
(
92113
StatusCode::METHOD_NOT_ALLOWED,

static/404.html renamed to templates/404.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<h1>GraphQL interface to Tiled</h1>
88
<p>
99
Service is available at
10-
<a href="/graphql">/graphql</a>.
10+
<a href="{{ graphql_address }}">/graphql</a>.
1111
Playground is available for testing at
12-
<a href="/graphiql">/graphiql</a>
12+
<a href="{{ graphiql_address }}">/graphiql</a>
1313
</p>
1414
</body>
1515
</html>

0 commit comments

Comments
 (0)