Skip to content

Commit 7dda925

Browse files
committed
Add fallback html responses for unhandled routes
Makes it look less like something has gone wrong when visiting the site from a browser.
1 parent 54db669 commit 7dda925

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ COPY ./.env ./.env
1414
COPY ./src ./src
1515
COPY ./.sqlx ./.sqlx
1616
COPY ./migrations ./migrations
17+
COPY ./static ./static
1718

1819
RUN cargo build --release --target x86_64-unknown-linux-musl
1920

src/graphql.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use async_graphql::{
3030
};
3131
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
3232
use auth::{AuthError, PolicyCheck};
33+
use axum::http::StatusCode;
3334
use axum::response::{Html, IntoResponse};
3435
use axum::routing::{get, post};
3536
use axum::{Extension, Json, Router};
@@ -75,7 +76,22 @@ pub async fn serve_graphql(db: &Path, opts: ServeOptions) {
7576
// making graphql queries
7677
.route("/status", get(server_status))
7778
.route("/graphql", post(graphql_handler))
79+
// make it obvious that /graphql isn't expected to work when visiting from a browser
80+
.route(
81+
"/graphql",
82+
get((
83+
StatusCode::METHOD_NOT_ALLOWED,
84+
[("Allow", "POST")],
85+
Html(include_str!("../static/get_graphql_warning.html")),
86+
)),
87+
)
88+
// Interactive graphiql playground
7889
.route("/graphiql", get(graphiql))
90+
// Make it look less like something is broken when going to any other page
91+
.fallback((
92+
StatusCode::NOT_FOUND,
93+
Html(include_str!("../static/404.html")),
94+
))
7995
.layer(Extension(schema));
8096
let listener = TcpListener::bind(addr)
8197
.await

static/404.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<head>
3+
<title>Numtracker</title>
4+
</head>
5+
<body>
6+
<h1>Data Acquisition File Naming Service</h1>
7+
<p>
8+
Service is available at
9+
<a href="/graphql">/graphql</a>.
10+
Playground is available for testing at
11+
<a href="/graphiql">/graphiql</a>
12+
</p>
13+
</body>
14+
</html>

static/get_graphql_warning.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Numtracker</title>
4+
</head>
5+
<body>
6+
<p>Requests should be made as graphql queries</p>
7+
</body>
8+
</html>

0 commit comments

Comments
 (0)