♻️ Planning changes to relay config
parent
8369dc8675
commit
bf4e5c7a5b
|
@ -1,4 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
error::Error,
|
error::Error,
|
||||||
collections::*,
|
collections::*,
|
||||||
convert::Infallible,
|
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
|
// Stuff we need to load from the config file and use to
|
||||||
// set up the HTTP server
|
// 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)]
|
#[derive (Default, Deserialize)]
|
||||||
pub struct ConfigFile {
|
pub struct ConfigFile {
|
||||||
pub port: Option <u16>,
|
pub port: Option <u16>,
|
||||||
pub server_tripcodes: HashMap <String, String>,
|
pub servers: ConfigServers,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stuff we actually need at runtime
|
// Stuff we actually need at runtime
|
||||||
|
@ -108,7 +115,7 @@ struct Config {
|
||||||
|
|
||||||
impl From <&ConfigFile> for Config {
|
impl From <&ConfigFile> for Config {
|
||||||
fn from (f: &ConfigFile) -> Self {
|
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)| {
|
.map (|(k, v)| {
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
let bytes: Vec <u8> = base64::decode (v).unwrap ();
|
let bytes: Vec <u8> = base64::decode (v).unwrap ();
|
||||||
|
@ -529,33 +536,34 @@ fn pretty_print_last_seen (
|
||||||
Description (last_seen.to_rfc3339_opts (SecondsFormat::Secs, true))
|
Description (last_seen.to_rfc3339_opts (SecondsFormat::Secs, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive (Serialize)]
|
||||||
async fn handle_server_list (
|
struct ServerEntry <'a> {
|
||||||
state: Arc <RelayState>
|
|
||||||
) -> Response <Body>
|
|
||||||
{
|
|
||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
#[derive (Serialize)]
|
|
||||||
struct ServerEntry <'a> {
|
|
||||||
path: String,
|
path: String,
|
||||||
name: String,
|
name: String,
|
||||||
last_seen: Cow <'a, str>,
|
last_seen: Cow <'a, str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Serialize)]
|
#[derive (Serialize)]
|
||||||
struct ServerListPage <'a> {
|
struct ServerListPage <'a> {
|
||||||
servers: Vec <ServerEntry <'a>>,
|
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;
|
let guard = state.server_status.lock ().await;
|
||||||
(*guard).clone ()
|
(*guard).clone ()
|
||||||
};
|
};
|
||||||
|
|
||||||
let now = Utc::now ();
|
let now = Utc::now ();
|
||||||
|
|
||||||
let mut servers: Vec <_> = servers.into_iter ()
|
let mut servers: Vec <_> = server_status.into_iter ()
|
||||||
.map (|(name, server)| {
|
.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 ();
|
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));
|
servers.sort_by (|a, b| a.name.cmp (&b.name));
|
||||||
|
|
||||||
let page = ServerListPage {
|
ServerListPage {
|
||||||
servers,
|
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 ();
|
let s = state.handlebars.render ("relay_server_list", &page).unwrap ();
|
||||||
ok_reply (s)
|
ok_reply (s)
|
||||||
|
|
3
todo.md
3
todo.md
|
@ -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)
|
- "Preview as" feature for Markdown (It's not threaded through the relay yet)
|
||||||
- Remote `tail -f` (_Complicated_) (Maybe use chunked encoding or something?)
|
- Remote `tail -f` (_Complicated_) (Maybe use chunked encoding or something?)
|
||||||
- Make a debug client to replicate the issue Firefox is having with turtling
|
- Make a debug client to replicate the issue Firefox is having with turtling
|
||||||
|
|
Loading…
Reference in New Issue