2
2
mod services;
3
3
mod structures;
4
4
5
- use std:: str:: FromStr ;
6
- use std:: { borrow:: Cow , net:: SocketAddr , sync:: Arc } ;
5
+ use std:: { borrow:: Cow , net:: SocketAddr , str:: FromStr , sync:: Arc } ;
7
6
8
- use axum:: extract:: Request ;
9
- use axum:: http:: { HeaderName , HeaderValue } ;
10
- use axum:: middleware:: Next ;
11
- use axum:: response:: Response ;
12
- use axum:: { extract:: Path , http:: StatusCode , response:: IntoResponse , routing:: get} ;
7
+ use axum:: {
8
+ extract:: { Path , Request } ,
9
+ http:: { HeaderName , HeaderValue , StatusCode } ,
10
+ middleware:: Next ,
11
+ response:: { IntoResponse , Response } ,
12
+ routing:: get,
13
+ } ;
13
14
use libmcping:: { Bedrock , Java } ;
14
15
use reqwest:: header:: HeaderMap ;
15
16
use tokio:: { net:: TcpListener , sync:: RwLock } ;
16
17
use tower_http:: services:: ServeDir ;
17
- use tracing_subscriber:: layer:: SubscriberExt ;
18
- use tracing_subscriber:: util:: SubscriberInitExt ;
18
+ use tracing_subscriber:: { layer:: SubscriberExt , util:: SubscriberInitExt } ;
19
19
20
20
use crate :: {
21
21
services:: { get_mcstatus, refresh_mcstatus} ,
@@ -56,7 +56,7 @@ async fn main() {
56
56
. route ( "/api/java/:address" , get ( handle_java_ping) )
57
57
. route ( "/api/bedrock/:address" , get ( handle_bedrock_ping) )
58
58
. route ( "/api/services" , get ( services:: handle_mcstatus) )
59
- . layer ( axum:: middleware:: from_fn ( noindex ) )
59
+ . layer ( axum:: middleware:: from_fn ( noindex_cache ) )
60
60
. fallback_service ( serve_dir)
61
61
. with_state ( current_mcstatus) ;
62
62
let socket_address = SocketAddr :: from ( (
@@ -72,12 +72,18 @@ async fn main() {
72
72
. await
73
73
. unwrap ( ) ;
74
74
}
75
+ static ROBOTS_NAME : HeaderName = HeaderName :: from_static ( "X-Robots-Tag" ) ;
76
+ static ROBOTS_VALUE : HeaderValue = HeaderValue :: from_static ( "noindex" ) ;
77
+ static CACHE_CONTROL_NOSTORE : HeaderValue = HeaderValue :: from_static ( "s-maxage=10" ) ;
75
78
76
- async fn noindex ( req : Request , next : Next ) -> Response {
79
+ async fn noindex_cache ( req : Request , next : Next ) -> Response {
77
80
let mut resp = next. run ( req) . await ;
78
- let name = HeaderName :: from_str ( "X-Robots-Tag" ) . unwrap ( ) ;
79
- let value = HeaderValue :: from_str ( "noindex" ) . unwrap ( ) ;
80
- resp. headers_mut ( ) . insert ( name, value) ;
81
+ resp. headers_mut ( )
82
+ . insert ( ROBOTS_NAME . clone ( ) , ROBOTS_VALUE . clone ( ) ) ;
83
+ resp. headers_mut ( ) . insert (
84
+ axum:: http:: header:: CACHE_CONTROL ,
85
+ CACHE_CONTROL_NOSTORE . clone ( ) ,
86
+ ) ;
81
87
resp
82
88
}
83
89
0 commit comments