Skip to content

Commit 090d962

Browse files
Start responsive, degrade gracefully
1 parent d57e247 commit 090d962

File tree

5 files changed

+93
-45
lines changed

5 files changed

+93
-45
lines changed

assets/ping.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const frame = document.getElementById("response-fake-frame");
2+
3+
async function load() {
4+
const req = await fetch(frame.dataset.src);
5+
frame.innerHTML = await req.text();
6+
}
7+
8+
load().then(() => {});

src/main.rs

+40-11
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ async fn main() {
8080
.route_with_tsr("/ping/:edition/:hostname", get(ping_page))
8181
.route("/ping/redirect", get(ping_redirect))
8282
.route("/internal/ping-frame/:edition/:hostname", get(ping_frame))
83+
.route("/internal/ping-markup/:edition/:hostname", get(ping_markup))
8384
.route("/api/:address", get(handle_java_ping))
8485
.route("/api/java/:address", get(handle_java_ping))
8586
.route("/api/bedrock/:address", get(handle_bedrock_ping))
@@ -110,6 +111,7 @@ static CACHE_CONTROL_AGE: HeaderValue = HeaderValue::from_static("s-maxage=30");
110111

111112
static CSP_VALUE: HeaderValue = HeaderValue::from_static(
112113
"default-src 'self'; \
114+
frame-src 'self'; \
113115
img-src 'self' data:; \
114116
connect-src 'self' https://v4.giveip.io; \
115117
script-src 'self' https://static.cloudflareinsights.com; \
@@ -194,17 +196,11 @@ async fn ping_page(
194196
})
195197
}
196198

197-
#[derive(Template)]
198-
#[template(path = "ping-frame.html")]
199-
pub struct PingFrameTemplate {
200-
ping: MCPingResponse,
201-
root_url: String,
202-
}
203-
204-
async fn ping_frame(
205-
State(state): State<AppState>,
206-
Path((edition, hostname)): Path<(String, String)>,
207-
) -> Result<PingFrameTemplate, ErrorTemplate> {
199+
async fn ping_generic(
200+
edition: String,
201+
hostname: String,
202+
state: &AppState,
203+
) -> Result<MCPingResponse, ErrorTemplate> {
208204
let ping = match edition.as_str() {
209205
"java" => ping_java(hostname).await,
210206
"bedrock" => ping_bedrock(hostname).await,
@@ -219,12 +215,45 @@ async fn ping_frame(
219215
Ok(v) => v,
220216
Err(e) => return Err(ErrorTemplate::from_failure(&e, &state)),
221217
};
218+
Ok(ping)
219+
}
220+
221+
#[derive(Template)]
222+
#[template(path = "ping-frame.html")]
223+
pub struct PingFrameTemplate {
224+
ping: MCPingResponse,
225+
root_url: String,
226+
}
227+
228+
async fn ping_frame(
229+
State(state): State<AppState>,
230+
Path((edition, hostname)): Path<(String, String)>,
231+
) -> Result<PingFrameTemplate, ErrorTemplate> {
232+
let ping = ping_generic(edition, hostname, &state).await?;
222233
Ok(PingFrameTemplate {
223234
ping,
224235
root_url: state.root_url.to_string(),
225236
})
226237
}
227238

239+
#[derive(Template)]
240+
#[template(path = "ping-element.html")]
241+
pub struct PingElementTemplate {
242+
ping: MCPingResponse,
243+
root_url: String,
244+
}
245+
246+
async fn ping_markup(
247+
State(state): State<AppState>,
248+
Path((edition, hostname)): Path<(String, String)>,
249+
) -> Result<PingElementTemplate, ErrorTemplate> {
250+
let ping = ping_generic(edition, hostname, &state).await?;
251+
Ok(PingElementTemplate {
252+
ping,
253+
root_url: state.root_url.to_string(),
254+
})
255+
}
256+
228257
#[derive(Template)]
229258
#[template(path = "api.html")]
230259
pub struct ApiTemplate {

templates/ping-element.html

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{% let icon %}
2+
{% match ping.icon %}
3+
{% when Some with (raw_icon) %}
4+
{% let icon = "{}"|format(raw_icon) %}
5+
{% when None %}
6+
{% let icon = "{}/icon.png"|format(root_url) %}
7+
{% endmatch %}
8+
<div class="server-response">
9+
<div class="server-response-stack">
10+
<img
11+
id="server-favicon"
12+
height="256"
13+
width="256"
14+
alt="The pixel-art icon for this server."
15+
src="{{ icon }}"
16+
/>
17+
<div class="small-vspacer"></div>
18+
<span class="server-online">Server online!</span>
19+
<div id="server-latency">Ping: {{ ping.latency }}ms</div>
20+
<div id="server-players">
21+
Players: {{ ping.players.online }} / {{ ping.players.maximum }}
22+
</div>
23+
<div id="server-version">Version: {{ ping.version.broadcast }}</div>
24+
<div id="server-motd">{{ ping.motd|mojang_colorize|safe }}</div>
25+
</div>
26+
<div class="small-vspacer"></div>
27+
</div>

templates/ping-frame.html

+1-26
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,5 @@
33
mcping - internal iframe
44
{% endblock description %}
55
{% block body %}
6-
{% let icon %}
7-
{% match ping.icon %}
8-
{% when Some with (raw_icon) %}
9-
{% let icon = "{}"|format(raw_icon) %}
10-
{% when None %}
11-
{% let icon = "{}/icon.png"|format(root_url) %}
12-
{% endmatch %}
13-
<div class="server-response" id="server-response">
14-
<div class="server-response-stack">
15-
<img
16-
id="server-favicon"
17-
height="256"
18-
width="256"
19-
alt="The pixel-art icon for this server."
20-
src="{{ icon }}"
21-
/>
22-
<div class="small-vspacer"></div>
23-
<span class="server-online">Server online!</span>
24-
<div id="server-latency">Ping: {{ ping.latency }}ms</div>
25-
<div id="server-players">
26-
Players: {{ ping.players.online }} / {{ ping.players.maximum }}
27-
</div>
28-
<div id="server-version">Version: {{ ping.version.broadcast }}</div>
29-
<div id="server-motd">{{ ping.motd|mojang_colorize|safe }}</div>
30-
</div>
31-
</div>
6+
{% include "ping-element.html" %}
327
{% endblock %}

templates/ping-page.html

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
{% extends "base.html" %}
22
{% block description %}
3-
mcping - ping {{ hostname }}
3+
mcping - ping {{ hostname }}
44
{% endblock description %}
55
{% block body %}
6-
<div class="server-response" id="server-response">
7-
<div class="server-response-stack">
8-
<h1>Pinging <code>{{ hostname }}</code></h1>
6+
<div class="server-response">
7+
<div class="server-response-stack">
8+
<h1>Pinging <code>{{ hostname }}</code></h1>
9+
<noscript>
910
<iframe
1011
class="ping-frame"
1112
src="{{ root_url }}/internal/ping-frame/{{ edition }}/{{ hostname }}"
1213
></iframe>
13-
{% include "ping-form.html" %}
14-
</div>
14+
</noscript>
15+
<div
16+
id="response-fake-frame"
17+
class="server-response-stack"
18+
data-src="{{ root_url }}/internal/ping-markup/{{ edition }}/{{ hostname }}"
19+
></div>
20+
<h2>Ping another?</h2>
21+
{% include "ping-form.html" %}
1522
</div>
16-
<div class="vspacer"></div>
17-
{% include "api-status.html" %}
23+
</div>
24+
<script src="{{ root_url }}/ping.js"></script>
25+
<div class="vspacer"></div>
26+
{% include "api-status.html" %}
1827
{% endblock %}

0 commit comments

Comments
 (0)