Skip to content

Commit 5d1e7d4

Browse files
Rearrange caching
1 parent 72fd937 commit 5d1e7d4

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

src/main.rs

+27-12
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ async fn main() {
7979
bust_dir: bust_dir.into(),
8080
};
8181

82+
let cache_none = axum::middleware::from_fn(crate::cache_none);
83+
let cache_medium = axum::middleware::from_fn(crate::cache_medium);
84+
let cache_max = axum::middleware::from_fn(crate::cache_max);
85+
let noindex = axum::middleware::from_fn(crate::noindex);
86+
8287
let serve_dir_raw = ServeDir::new(&asset_dir)
8388
.append_index_html_on_directories(true)
8489
.precompressed_gzip()
@@ -87,29 +92,31 @@ async fn main() {
8792
.precompressed_zstd()
8893
.fallback(handle_404.with_state(state.clone()));
8994
let serve_dir = ServiceBuilder::new()
90-
.layer(axum::middleware::from_fn(noindex))
91-
.layer(axum::middleware::from_fn(cache))
95+
.layer(noindex.clone())
96+
.layer(cache_max.clone())
9297
.service(serve_dir_raw);
93-
let app = Router::new()
94-
.route("/ping/redirect", get(ping_redirect))
98+
let api = Router::new()
9599
.route("/api/:address", get(handle_java_ping))
96100
.route("/api/java/:address", get(handle_java_ping))
97101
.route("/api/bedrock/:address", get(handle_bedrock_ping))
98102
.route("/api/java/", get(no_address))
99103
.route("/api/bedrock/", get(no_address))
100104
.route("/api/services", get(services::handle_mcstatus))
101-
.layer(axum::middleware::from_fn(noindex))
105+
.layer(noindex)
106+
.layer(cache_none);
107+
let app = Router::new()
102108
.route("/", get(root))
103109
.route_with_tsr("/api/", get(api_info))
110+
.route("/ping/redirect", get(ping_redirect).layer(cache_max))
104111
.route_with_tsr("/ping/:edition/:hostname", get(ping_page))
105112
.route("/internal/ping-frame/:edition/:hostname", get(ping_frame))
106113
.route("/internal/ping-markup/:edition/:hostname", get(ping_markup))
107114
.route(
108115
"/internal/icon/:edition/:hostname/icon.:ext",
109-
get(ping_image),
116+
get(ping_image).layer(cache_medium),
110117
)
111-
.layer(axum::middleware::from_fn(cache_short))
112118
.fallback_service(serve_dir)
119+
.merge(api)
113120
.layer(axum::middleware::from_fn(csp))
114121
.with_state(state);
115122

@@ -133,8 +140,9 @@ static ROBOTS_NAME: HeaderName = HeaderName::from_static("x-robots-tag");
133140
static ROBOTS_VALUE: HeaderValue = HeaderValue::from_static("noindex");
134141
static CACHE_CONTROL_IMMUTABLE: HeaderValue =
135142
HeaderValue::from_static("immutable, public, max-age=31536000");
136-
static CACHE_CONTROL_SHORT: HeaderValue =
137-
HeaderValue::from_static("max-age=30, public, stale-while-revalidate");
143+
static CACHE_CONTROL_MEDIUM: HeaderValue =
144+
HeaderValue::from_static("max-age=7200, public, stale-while-revalidate");
145+
static CACHE_CONTROL_NONE: HeaderValue = HeaderValue::from_static("max-age=0, no-store");
138146

139147
static CSP_VALUE: HeaderValue = HeaderValue::from_static(
140148
"default-src 'self'; \
@@ -161,17 +169,24 @@ async fn csp(req: Request, next: Next) -> Response {
161169
resp
162170
}
163171

164-
async fn cache(req: Request, next: Next) -> Response {
172+
async fn cache_max(req: Request, next: Next) -> Response {
165173
let mut resp = next.run(req).await;
166174
resp.headers_mut()
167175
.insert(CACHE_CONTROL, CACHE_CONTROL_IMMUTABLE.clone());
168176
resp
169177
}
170178

171-
async fn cache_short(req: Request, next: Next) -> Response {
179+
async fn cache_medium(req: Request, next: Next) -> Response {
180+
let mut resp = next.run(req).await;
181+
resp.headers_mut()
182+
.insert(CACHE_CONTROL, CACHE_CONTROL_MEDIUM.clone());
183+
resp
184+
}
185+
186+
async fn cache_none(req: Request, next: Next) -> Response {
172187
let mut resp = next.run(req).await;
173188
resp.headers_mut()
174-
.insert(CACHE_CONTROL, CACHE_CONTROL_SHORT.clone());
189+
.insert(CACHE_CONTROL, CACHE_CONTROL_NONE.clone());
175190
resp
176191
}
177192

0 commit comments

Comments
 (0)