♻️ Planning changes to relay config

main
_ 2020-11-26 21:50:55 +00:00
parent 8369dc8675
commit bf4e5c7a5b
2 changed files with 40 additions and 22 deletions

View File

@ -1,4 +1,5 @@
use std::{
borrow::Cow,
error::Error,
collections::*,
convert::Infallible,
@ -94,10 +95,16 @@ type ResponseRendezvous = oneshot::Sender <Result <(http_serde::ResponseParts,
// Stuff we need to load from the config file and use to
// set up the HTTP server
#[derive (Default, Deserialize)]
pub struct ConfigServers {
pub tripcodes: HashMap <String, String>,
pub display_names: HashMap <String, String>,
}
#[derive (Default, Deserialize)]
pub struct ConfigFile {
pub port: Option <u16>,
pub server_tripcodes: HashMap <String, String>,
pub servers: ConfigServers,
}
// Stuff we actually need at runtime
@ -108,7 +115,7 @@ struct Config {
impl From <&ConfigFile> for Config {
fn from (f: &ConfigFile) -> Self {
let server_tripcodes = HashMap::from_iter (f.server_tripcodes.iter ()
let server_tripcodes = HashMap::from_iter (f.servers.tripcodes.iter ()
.map (|(k, v)| {
use std::convert::TryInto;
let bytes: Vec <u8> = base64::decode (v).unwrap ();
@ -529,13 +536,6 @@ fn pretty_print_last_seen (
Description (last_seen.to_rfc3339_opts (SecondsFormat::Secs, true))
}
async fn handle_server_list (
state: Arc <RelayState>
) -> Response <Body>
{
use std::borrow::Cow;
#[derive (Serialize)]
struct ServerEntry <'a> {
path: String,
@ -548,14 +548,22 @@ async fn handle_server_list (
servers: Vec <ServerEntry <'a>>,
}
let servers = {
async fn handle_server_list_internal (state: &Arc <RelayState>)
-> ServerListPage <'static>
{
let all_servers: Vec <_> = {
let guard = state.config.read ().await;
(*guard).server_tripcodes.keys ().cloned ().collect ()
};
let server_status = {
let guard = state.server_status.lock ().await;
(*guard).clone ()
};
let now = Utc::now ();
let mut servers: Vec <_> = servers.into_iter ()
let mut servers: Vec <_> = server_status.into_iter ()
.map (|(name, server)| {
let display_name = percent_encoding::percent_decode_str (&name).decode_utf8 ().unwrap_or_else (|_| "Server name isn't UTF-8".into ()).to_string ();
@ -577,9 +585,16 @@ async fn handle_server_list (
servers.sort_by (|a, b| a.name.cmp (&b.name));
let page = ServerListPage {
ServerListPage {
servers,
};
}
}
async fn handle_server_list (
state: Arc <RelayState>
) -> Response <Body>
{
let page = handle_server_list_internal (&state).await;
let s = state.handlebars.render ("relay_server_list", &page).unwrap ();
ok_reply (s)

View File

@ -1,3 +1,6 @@
- Server list should include offline or never-launched servers
- Allow relay to rename servers
- Estimate bandwidth per server?
- "Preview as" feature for Markdown (It's not threaded through the relay yet)
- Remote `tail -f` (_Complicated_) (Maybe use chunked encoding or something?)
- Make a debug client to replicate the issue Firefox is having with turtling