1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
use crate::Registry;
use anyhow::Result;
use hurrycurry_protocol::registry::Entry;
use rocket::{
get,
http::MediaType,
request::{self, FromRequest, Outcome},
response::content::{RawHtml, RawJson},
Either, Request, State,
};
use std::sync::Arc;
use tokio::sync::RwLock;
#[get("/v1/list")]
pub(super) async fn r_list(
registry: &State<Arc<RwLock<Registry>>>,
json: AcceptJson,
) -> Either<RawJson<Arc<str>>, RawHtml<Arc<str>>> {
if json.0 {
Either::Left(RawJson(registry.read().await.json_response.clone()))
} else {
Either::Right(RawHtml(registry.read().await.html_response.clone()))
}
}
pub(super) fn generate_json_list(entries: &[Entry]) -> Result<Arc<str>> {
Ok(serde_json::to_string(&entries)?.into())
}
pub(super) fn generate_html_list(entries: &[Entry]) -> Result<Arc<str>> {
Ok(ListPage { entries }.to_string().into())
}
markup::define!(
ListPage<'a>(entries: &'a [Entry]) {
@markup::doctype()
html {
head {
title {
"Hurry Curry! Server Registry"
}
}
body {
table {
tr { th {"Server"} th { "Players" } th { "Protocol" } }
@for e in *entries { tr {
td { details {
summary { @e.name }
ul { @for a in &e.address { li { @a } }}
} }
td { @e.players_online }
td { @e.version.0 "." @e.version.1 }
}}
}
}
}
}
);
pub struct AcceptJson(bool);
impl<'r> FromRequest<'r> for AcceptJson {
type Error = ();
fn from_request<'life0, 'async_trait>(
request: &'r Request<'life0>,
) -> ::core::pin::Pin<
Box<
dyn ::core::future::Future<Output = request::Outcome<Self, Self::Error>>
+ ::core::marker::Send
+ 'async_trait,
>,
>
where
'r: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
{
Box::pin(async move {
Outcome::Success(AcceptJson(
request
.accept()
.map(|a| a.preferred().exact_eq(&MediaType::JSON))
.unwrap_or(false),
))
})
}
}
|