Skip to content

Commit ca09468

Browse files
cache busting time
1 parent adc5b98 commit ca09468

9 files changed

+143
-36
lines changed

Cargo.lock

+79-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ tower-http = { version = "0.5", default-features = false, features = ["tracing",
1111
tokio = { version = "1", features = ["rt-multi-thread", "macros", "signal"] }
1212
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
1313
askama = { version = "0.12", features = ["with-axum"] }
14+
bustdir = { version = "0.1", features = ["askama"] }
1415
serde = { version = "1", features = ["derive"] }
1516
libmcping = { path = "./libmcping" }
1617
askama_escape = "0.10"

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
[![website](https://img.shields.io/website-up-down-green-red/https/mcping.me.svg)](https://mcping.me) [![CI](https://github.com/randomairborne/mcping/actions/workflows/build.yml/badge.svg)](https://github.com/randomairborne/mcping/actions/workflows/build.yml)
66

7-
A simple Minecraft server pinging service. Designed to work *everywhere*.
7+
A simple Minecraft server pinging service. Designed to work _everywhere_.
88

99
API Docs: [https://mcping.me/api/](https://mcping.me/api/)

src/filters.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::fmt::{Display, Formatter, Write};
22

33
use askama::Html;
44
use askama_escape::Escaper;
5+
pub use bustdir::askama::bust_dir;
56

67
const SECTION: char = '§';
78

src/main.rs

+39-21
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use axum::{
2020
Router,
2121
};
2222
use axum_extra::routing::RouterExt;
23+
use bustdir::BustDir;
2324
use parking_lot::RwLock;
2425
use reqwest::{header::HeaderMap, redirect::Policy, Client};
2526
use serde::{Deserialize, Serialize};
@@ -46,6 +47,7 @@ async fn main() {
4647
let root_url = valk_utils::get_var("ROOT_URL");
4748
let root_url = root_url.trim_end_matches('/').to_owned();
4849
let port: u16 = std::env::var("PORT").map_or(DEFAULT_PORT, |v| v.parse().unwrap());
50+
let bust_dir = BustDir::new(&asset_dir).expect("Failed to build cache busting directory");
4951

5052
let mut default_headers = HeaderMap::new();
5153
default_headers.insert("Accept", "application/json".parse().unwrap());
@@ -68,6 +70,7 @@ async fn main() {
6870
let state = AppState {
6971
svc_response: current_mcstatus,
7072
root_url: root_url.into(),
73+
bust_dir: bust_dir.into(),
7174
};
7275

7376
let serve_dir = ServeDir::new(&asset_dir)
@@ -85,18 +88,18 @@ async fn main() {
8588
.route("/api/java/", get(no_address))
8689
.route("/api/bedrock/", get(no_address))
8790
.route("/api/services", get(services::handle_mcstatus))
88-
.layer(axum::middleware::from_fn(noindex))
91+
.layer(
92+
ServiceBuilder::new()
93+
.layer(axum::middleware::from_fn(noindex))
94+
.layer(axum::middleware::from_fn(cache)),
95+
)
8996
.route("/", get(root))
9097
.route_with_tsr("/api/", get(api_info))
9198
.route_with_tsr("/ping/:edition/:hostname", get(ping_page))
9299
.route("/internal/ping-frame/:edition/:hostname", get(ping_frame))
93100
.route("/internal/ping-markup/:edition/:hostname", get(ping_markup))
94101
.fallback_service(serve_dir)
95-
.layer(
96-
ServiceBuilder::new()
97-
.layer(axum::middleware::from_fn(csp))
98-
.layer(axum::middleware::from_fn(cache)),
99-
)
102+
.layer(axum::middleware::from_fn(csp))
100103
.with_state(state);
101104

102105
let socket_address = SocketAddr::from(([0, 0, 0, 0], port));
@@ -111,11 +114,13 @@ async fn main() {
111114
pub struct AppState {
112115
svc_response: Arc<RwLock<ServicesResponse>>,
113116
root_url: Arc<str>,
117+
bust_dir: Arc<BustDir>,
114118
}
115119

116120
static ROBOTS_NAME: HeaderName = HeaderName::from_static("x-robots-tag");
117121
static ROBOTS_VALUE: HeaderValue = HeaderValue::from_static("noindex");
118-
static CACHE_CONTROL_AGE: HeaderValue = HeaderValue::from_static("s-maxage=30");
122+
static CACHE_CONTROL_IMMUTABLE: HeaderValue =
123+
HeaderValue::from_static("immutable, public, max-age=604800");
119124

120125
static CSP_VALUE: HeaderValue = HeaderValue::from_static(
121126
"default-src 'self'; \
@@ -145,21 +150,23 @@ async fn csp(req: Request, next: Next) -> Response {
145150
async fn cache(req: Request, next: Next) -> Response {
146151
let mut resp = next.run(req).await;
147152
resp.headers_mut()
148-
.insert(CACHE_CONTROL, CACHE_CONTROL_AGE.clone());
153+
.insert(CACHE_CONTROL, CACHE_CONTROL_IMMUTABLE.clone());
149154
resp
150155
}
151156

152157
#[derive(Template)]
153158
#[template(path = "index.html")]
154159
pub struct RootTemplate {
155160
svc_status: ServicesResponse,
156-
root_url: String,
161+
root_url: Arc<str>,
162+
bd: Arc<BustDir>,
157163
}
158164

159165
async fn root(State(state): State<AppState>) -> RootTemplate {
160166
RootTemplate {
161167
svc_status: *state.svc_response.read(),
162-
root_url: state.root_url.to_string(),
168+
root_url: state.root_url,
169+
bd: state.bust_dir,
163170
}
164171
}
165172

@@ -183,7 +190,8 @@ async fn ping_redirect(
183190
#[template(path = "ping-page.html")]
184191
pub struct PingPageTemplate {
185192
svc_status: ServicesResponse,
186-
root_url: String,
193+
root_url: Arc<str>,
194+
bd: Arc<BustDir>,
187195
hostname: String,
188196
edition: String,
189197
}
@@ -203,7 +211,8 @@ async fn ping_page(
203211
}
204212
Ok(PingPageTemplate {
205213
svc_status: *state.svc_response.read(),
206-
root_url: state.root_url.to_string(),
214+
root_url: state.root_url,
215+
bd: state.bust_dir,
207216
hostname,
208217
edition,
209218
})
@@ -230,7 +239,8 @@ async fn ping_generic(
230239
#[template(path = "ping-frame.html")]
231240
pub struct PingFrameTemplate {
232241
ping: MCPingResponse,
233-
root_url: String,
242+
bd: Arc<BustDir>,
243+
root_url: Arc<str>,
234244
}
235245

236246
async fn ping_frame(
@@ -240,15 +250,17 @@ async fn ping_frame(
240250
let ping = ping_generic(edition, hostname, &state).await?;
241251
Ok(PingFrameTemplate {
242252
ping,
243-
root_url: state.root_url.to_string(),
253+
root_url: state.root_url,
254+
bd: state.bust_dir,
244255
})
245256
}
246257

247258
#[derive(Template)]
248259
#[template(path = "ping-element.html")]
249260
pub struct PingElementTemplate {
250261
ping: MCPingResponse,
251-
root_url: String,
262+
bd: Arc<BustDir>,
263+
root_url: Arc<str>,
252264
}
253265

254266
async fn ping_markup(
@@ -258,19 +270,22 @@ async fn ping_markup(
258270
let ping = ping_generic(edition, hostname, &state).await?;
259271
Ok(PingElementTemplate {
260272
ping,
261-
root_url: state.root_url.to_string(),
273+
bd: state.bust_dir,
274+
root_url: state.root_url,
262275
})
263276
}
264277

265278
#[derive(Template)]
266279
#[template(path = "api.html")]
267280
pub struct ApiTemplate {
268-
root_url: String,
281+
bd: Arc<BustDir>,
282+
root_url: Arc<str>,
269283
}
270284

271285
async fn api_info(State(state): State<AppState>) -> ApiTemplate {
272286
ApiTemplate {
273-
root_url: state.root_url.to_string(),
287+
root_url: state.root_url,
288+
bd: state.bust_dir,
274289
}
275290
}
276291

@@ -290,7 +305,8 @@ async fn no_address() -> Failure {
290305
async fn handle_404(State(state): State<AppState>) -> ErrorTemplate {
291306
ErrorTemplate {
292307
error: "404 not found".to_owned(),
293-
root_url: state.root_url.to_string(),
308+
bd: state.bust_dir,
309+
root_url: state.root_url,
294310
}
295311
}
296312

@@ -335,13 +351,15 @@ pub struct ErrorSerialization {
335351
#[template(path = "error.html")]
336352
pub struct ErrorTemplate {
337353
error: String,
338-
root_url: String,
354+
bd: Arc<BustDir>,
355+
root_url: Arc<str>,
339356
}
340357

341358
impl ErrorTemplate {
342359
fn from_failure(failure: &Failure, state: &AppState) -> Self {
343360
Self {
344-
root_url: state.root_url.to_string(),
361+
root_url: state.root_url.clone(),
362+
bd: state.bust_dir.clone(),
345363
error: failure.to_string(),
346364
}
347365
}

0 commit comments

Comments
 (0)