♻️ refactor: rename `RelayState` to `Relay` and privatize `load_templates`
parent
c6f651387e
commit
146c91f2be
|
@ -71,7 +71,7 @@ mod server_endpoint;
|
||||||
|
|
||||||
pub use config::Config;
|
pub use config::Config;
|
||||||
pub use errors::*;
|
pub use errors::*;
|
||||||
pub use relay_state::RelayState;
|
pub use relay_state::Relay;
|
||||||
|
|
||||||
use relay_state::{
|
use relay_state::{
|
||||||
RejectedServer,
|
RejectedServer,
|
||||||
|
@ -99,7 +99,7 @@ fn error_reply (status: StatusCode, b: &str)
|
||||||
async fn handle_http_request (
|
async fn handle_http_request (
|
||||||
req: http::request::Parts,
|
req: http::request::Parts,
|
||||||
uri: String,
|
uri: String,
|
||||||
state: Arc <RelayState>,
|
state: Arc <Relay>,
|
||||||
server_name: &str
|
server_name: &str
|
||||||
)
|
)
|
||||||
-> Result <Response <Body>, http::Error>
|
-> Result <Response <Body>, http::Error>
|
||||||
|
@ -289,7 +289,7 @@ struct AuditLogPage {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async fn handle_server_list_internal (state: &Arc <RelayState>)
|
async fn handle_server_list_internal (state: &Arc <Relay>)
|
||||||
-> ServerListPage <'static>
|
-> ServerListPage <'static>
|
||||||
{
|
{
|
||||||
use LastSeen::*;
|
use LastSeen::*;
|
||||||
|
@ -330,7 +330,7 @@ async fn handle_server_list_internal (state: &Arc <RelayState>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_unregistered_servers_internal (state: &Arc <RelayState>)
|
async fn handle_unregistered_servers_internal (state: &Arc <Relay>)
|
||||||
-> UnregisteredServerListPage
|
-> UnregisteredServerListPage
|
||||||
{
|
{
|
||||||
use LastSeen::*;
|
use LastSeen::*;
|
||||||
|
@ -359,7 +359,7 @@ async fn handle_unregistered_servers_internal (state: &Arc <RelayState>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_audit_log_internal (state: &Arc <RelayState>)
|
async fn handle_audit_log_internal (state: &Arc <Relay>)
|
||||||
-> AuditLogPage
|
-> AuditLogPage
|
||||||
{
|
{
|
||||||
let audit_log = state.audit_log.to_vec ().await
|
let audit_log = state.audit_log.to_vec ().await
|
||||||
|
@ -371,7 +371,7 @@ async fn handle_audit_log_internal (state: &Arc <RelayState>)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_server_list (
|
async fn handle_server_list (
|
||||||
state: Arc <RelayState>,
|
state: Arc <Relay>,
|
||||||
handlebars: Arc <Handlebars <'static>>
|
handlebars: Arc <Handlebars <'static>>
|
||||||
) -> Result <Response <Body>, RequestError>
|
) -> Result <Response <Body>, RequestError>
|
||||||
{
|
{
|
||||||
|
@ -382,7 +382,7 @@ async fn handle_server_list (
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_unregistered_servers (
|
async fn handle_unregistered_servers (
|
||||||
state: Arc <RelayState>,
|
state: Arc <Relay>,
|
||||||
handlebars: Arc <Handlebars <'static>>
|
handlebars: Arc <Handlebars <'static>>
|
||||||
) -> Result <Response <Body>, RequestError>
|
) -> Result <Response <Body>, RequestError>
|
||||||
{
|
{
|
||||||
|
@ -393,7 +393,7 @@ async fn handle_unregistered_servers (
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_audit_log (
|
async fn handle_audit_log (
|
||||||
state: Arc <RelayState>,
|
state: Arc <Relay>,
|
||||||
handlebars: Arc <Handlebars <'static>>
|
handlebars: Arc <Handlebars <'static>>
|
||||||
) -> Result <Response <Body>, RequestError>
|
) -> Result <Response <Body>, RequestError>
|
||||||
{
|
{
|
||||||
|
@ -473,7 +473,7 @@ async fn handle_endless_source (gib: usize, throttle: Option <usize>)
|
||||||
.body (Body::wrap_stream (ReceiverStream::new (rx)))
|
.body (Body::wrap_stream (ReceiverStream::new (rx)))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_gen_scraper_key (_state: Arc <RelayState>)
|
async fn handle_gen_scraper_key (_state: Arc <Relay>)
|
||||||
-> Result <Response <Body>, http::Error>
|
-> Result <Response <Body>, http::Error>
|
||||||
{
|
{
|
||||||
let key = ptth_core::gen_key ();
|
let key = ptth_core::gen_key ();
|
||||||
|
@ -489,7 +489,7 @@ async fn handle_gen_scraper_key (_state: Arc <RelayState>)
|
||||||
#[instrument (level = "trace", skip (req, state, handlebars))]
|
#[instrument (level = "trace", skip (req, state, handlebars))]
|
||||||
async fn handle_all (
|
async fn handle_all (
|
||||||
req: Request <Body>,
|
req: Request <Body>,
|
||||||
state: Arc <RelayState>,
|
state: Arc <Relay>,
|
||||||
handlebars: Arc <Handlebars <'static>>
|
handlebars: Arc <Handlebars <'static>>
|
||||||
)
|
)
|
||||||
-> Result <Response <Body>, RequestError>
|
-> Result <Response <Body>, RequestError>
|
||||||
|
@ -580,7 +580,7 @@ fn load_templates (asset_root: &Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn reload_config (
|
async fn reload_config (
|
||||||
state: &Arc <RelayState>,
|
state: &Arc <Relay>,
|
||||||
config_reload_path: &Path
|
config_reload_path: &Path
|
||||||
) -> Result <(), ConfigError> {
|
) -> Result <(), ConfigError> {
|
||||||
let new_config = Config::from_file (config_reload_path).await?;
|
let new_config = Config::from_file (config_reload_path).await?;
|
||||||
|
@ -604,7 +604,7 @@ async fn reload_config (
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run_relay (
|
pub async fn run_relay (
|
||||||
state: Arc <RelayState>,
|
state: Arc <Relay>,
|
||||||
asset_root: &Path,
|
asset_root: &Path,
|
||||||
shutdown_oneshot: oneshot::Receiver <()>,
|
shutdown_oneshot: oneshot::Receiver <()>,
|
||||||
config_reload_path: Option <PathBuf>
|
config_reload_path: Option <PathBuf>
|
||||||
|
|
|
@ -16,7 +16,7 @@ use tracing_subscriber::{
|
||||||
|
|
||||||
use ptth_relay::{
|
use ptth_relay::{
|
||||||
Config,
|
Config,
|
||||||
RelayState,
|
Relay,
|
||||||
run_relay,
|
run_relay,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ async fn main () -> Result <(), Box <dyn Error>> {
|
||||||
|
|
||||||
forced_shutdown.wrap_server (
|
forced_shutdown.wrap_server (
|
||||||
run_relay (
|
run_relay (
|
||||||
Arc::new (RelayState::try_from (config)?),
|
Arc::new (Relay::try_from (config)?),
|
||||||
&PathBuf::new (),
|
&PathBuf::new (),
|
||||||
shutdown_rx,
|
shutdown_rx,
|
||||||
Some (config_path)
|
Some (config_path)
|
||||||
|
|
|
@ -71,7 +71,7 @@ impl Default for ServerStatus {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RelayState {
|
pub struct Relay {
|
||||||
pub config: RwLock <Config>,
|
pub config: RwLock <Config>,
|
||||||
|
|
||||||
// Key: Server ID
|
// Key: Server ID
|
||||||
|
@ -157,7 +157,7 @@ impl <T: Clone> BoundedVec <T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom <Config> for RelayState {
|
impl TryFrom <Config> for Relay {
|
||||||
type Error = RelayError;
|
type Error = RelayError;
|
||||||
|
|
||||||
fn try_from (config: Config) -> Result <Self, Self::Error> {
|
fn try_from (config: Config) -> Result <Self, Self::Error> {
|
||||||
|
@ -176,7 +176,7 @@ impl TryFrom <Config> for RelayState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RelayState {
|
impl Relay {
|
||||||
pub async fn list_servers (&self) -> Vec <String> {
|
pub async fn list_servers (&self) -> Vec <String> {
|
||||||
self.request_rendezvous.lock ().await.iter ()
|
self.request_rendezvous.lock ().await.iter ()
|
||||||
.map (|(k, _)| (*k).clone ())
|
.map (|(k, _)| (*k).clone ())
|
||||||
|
|
|
@ -26,7 +26,7 @@ use crate::{
|
||||||
BlakeHashWrapper,
|
BlakeHashWrapper,
|
||||||
KeyValidity,
|
KeyValidity,
|
||||||
},
|
},
|
||||||
relay_state::RelayState,
|
relay_state::Relay,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Not sure if this is the best way to do a hard-coded string table, but
|
// Not sure if this is the best way to do a hard-coded string table, but
|
||||||
|
@ -65,7 +65,7 @@ pub struct ServerList {
|
||||||
pub servers: Vec <Server>,
|
pub servers: Vec <Server>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn v1_server_list (state: &Arc <RelayState>)
|
pub async fn v1_server_list (state: &Arc <Relay>)
|
||||||
-> ServerList
|
-> ServerList
|
||||||
{
|
{
|
||||||
// name --> display_name
|
// name --> display_name
|
||||||
|
@ -112,7 +112,7 @@ pub async fn v1_server_list (state: &Arc <RelayState>)
|
||||||
#[instrument (level = "trace", skip (req, state))]
|
#[instrument (level = "trace", skip (req, state))]
|
||||||
async fn api_v1 (
|
async fn api_v1 (
|
||||||
req: Request <Body>,
|
req: Request <Body>,
|
||||||
state: Arc <RelayState>,
|
state: Arc <Relay>,
|
||||||
path_rest: &str
|
path_rest: &str
|
||||||
)
|
)
|
||||||
-> Result <Response <Body>, RequestError>
|
-> Result <Response <Body>, RequestError>
|
||||||
|
@ -184,7 +184,7 @@ async fn api_v1 (
|
||||||
#[instrument (level = "trace", skip (req, state))]
|
#[instrument (level = "trace", skip (req, state))]
|
||||||
pub async fn handle (
|
pub async fn handle (
|
||||||
req: Request <Body>,
|
req: Request <Body>,
|
||||||
state: Arc <RelayState>,
|
state: Arc <Relay>,
|
||||||
path_rest: &str
|
path_rest: &str
|
||||||
)
|
)
|
||||||
-> Result <Response <Body>, RequestError>
|
-> Result <Response <Body>, RequestError>
|
||||||
|
@ -299,7 +299,7 @@ mod tests {
|
||||||
|
|
||||||
let config = config::Config::try_from (config_file).expect ("Can't load config");
|
let config = config::Config::try_from (config_file).expect ("Can't load config");
|
||||||
|
|
||||||
let relay_state = Arc::new (RelayState::try_from (config).expect ("Can't create relay state"));
|
let relay_state = Arc::new (Relay::try_from (config).expect ("Can't create relay state"));
|
||||||
|
|
||||||
let actual = super::handle (input, relay_state, self.path_rest).await;
|
let actual = super::handle (input, relay_state, self.path_rest).await;
|
||||||
let actual = actual.expect ("Relay didn't respond");
|
let actual = actual.expect ("Relay didn't respond");
|
||||||
|
|
|
@ -36,7 +36,7 @@ use super::{
|
||||||
},
|
},
|
||||||
HandleHttpResponseError,
|
HandleHttpResponseError,
|
||||||
ok_reply,
|
ok_reply,
|
||||||
RelayState,
|
Relay,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Servers will come here and either handle queued requests from parked clients,
|
// Servers will come here and either handle queued requests from parked clients,
|
||||||
|
@ -44,7 +44,7 @@ use super::{
|
||||||
// Step 1
|
// Step 1
|
||||||
|
|
||||||
pub async fn handle_listen (
|
pub async fn handle_listen (
|
||||||
state: Arc <RelayState>,
|
state: Arc <Relay>,
|
||||||
watcher_code: String,
|
watcher_code: String,
|
||||||
api_key: &[u8],
|
api_key: &[u8],
|
||||||
)
|
)
|
||||||
|
@ -140,7 +140,7 @@ pub async fn handle_listen (
|
||||||
|
|
||||||
pub async fn handle_response (
|
pub async fn handle_response (
|
||||||
req: Request <Body>,
|
req: Request <Body>,
|
||||||
state: Arc <RelayState>,
|
state: Arc <Relay>,
|
||||||
req_id: String,
|
req_id: String,
|
||||||
)
|
)
|
||||||
-> Result <Response <Body>, HandleHttpResponseError>
|
-> Result <Response <Body>, HandleHttpResponseError>
|
||||||
|
|
|
@ -46,7 +46,7 @@ async fn main () -> anyhow::Result <()> {
|
||||||
|
|
||||||
let config = ptth_relay::config::Config::try_from (config_file).expect ("Can't load config");
|
let config = ptth_relay::config::Config::try_from (config_file).expect ("Can't load config");
|
||||||
|
|
||||||
let relay_state = Arc::new (ptth_relay::RelayState::try_from (config).expect ("Can't create relay state"));
|
let relay_state = Arc::new (ptth_relay::Relay::try_from (config).expect ("Can't create relay state"));
|
||||||
|
|
||||||
let (stop_relay_tx, stop_relay_rx) = oneshot::channel ();
|
let (stop_relay_tx, stop_relay_rx) = oneshot::channel ();
|
||||||
let task_relay = spawn ({
|
let task_relay = spawn ({
|
||||||
|
@ -54,7 +54,7 @@ async fn main () -> anyhow::Result <()> {
|
||||||
async move {
|
async move {
|
||||||
ptth_relay::run_relay (
|
ptth_relay::run_relay (
|
||||||
relay_state,
|
relay_state,
|
||||||
Arc::new (ptth_relay::load_templates (&PathBuf::new ())?),
|
&PathBuf::new (),
|
||||||
stop_relay_rx,
|
stop_relay_rx,
|
||||||
None
|
None
|
||||||
).await
|
).await
|
||||||
|
|
12
src/tests.rs
12
src/tests.rs
|
@ -20,7 +20,7 @@ use tracing::{debug, info};
|
||||||
// If this takes more than 5 seconds-ish, it's bad, the test should
|
// If this takes more than 5 seconds-ish, it's bad, the test should
|
||||||
// fail
|
// fail
|
||||||
|
|
||||||
async fn wait_for_any_server (relay_state: &ptth_relay::RelayState) {
|
async fn wait_for_any_server (relay_state: &ptth_relay::Relay) {
|
||||||
for _ in 0..50 {
|
for _ in 0..50 {
|
||||||
tokio::time::sleep (Duration::from_millis (100)).await;
|
tokio::time::sleep (Duration::from_millis (100)).await;
|
||||||
if ! relay_state.list_servers ().await.is_empty () {
|
if ! relay_state.list_servers ().await.is_empty () {
|
||||||
|
@ -83,7 +83,7 @@ impl TestingConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TestingRelay {
|
struct TestingRelay {
|
||||||
state: Arc <ptth_relay::RelayState>,
|
state: Arc <ptth_relay::Relay>,
|
||||||
task: tokio::task::JoinHandle <Result <(), ptth_relay::RelayError>>,
|
task: tokio::task::JoinHandle <Result <(), ptth_relay::RelayError>>,
|
||||||
stop_tx: oneshot::Sender <()>,
|
stop_tx: oneshot::Sender <()>,
|
||||||
}
|
}
|
||||||
|
@ -114,7 +114,7 @@ impl TestingRelay {
|
||||||
|
|
||||||
let cfg = config::Config::try_from (config_file).expect ("Can't load config");
|
let cfg = config::Config::try_from (config_file).expect ("Can't load config");
|
||||||
|
|
||||||
let state = Arc::new (RelayState::try_from (cfg).expect ("Can't create relay state"));
|
let state = Arc::new (Relay::try_from (cfg).expect ("Can't create relay state"));
|
||||||
|
|
||||||
let (stop_tx, stop_rx) = oneshot::channel ();
|
let (stop_tx, stop_rx) = oneshot::channel ();
|
||||||
let task = spawn ({
|
let task = spawn ({
|
||||||
|
@ -122,7 +122,7 @@ impl TestingRelay {
|
||||||
async move {
|
async move {
|
||||||
run_relay (
|
run_relay (
|
||||||
state,
|
state,
|
||||||
Arc::new (load_templates (&PathBuf::new ())?),
|
&PathBuf::new (),
|
||||||
stop_rx,
|
stop_rx,
|
||||||
None
|
None
|
||||||
).await
|
).await
|
||||||
|
@ -286,13 +286,13 @@ async fn scraper_endpoints () {
|
||||||
|
|
||||||
let config = config::Config::try_from (config_file).expect ("Can't load config");
|
let config = config::Config::try_from (config_file).expect ("Can't load config");
|
||||||
|
|
||||||
let relay_state = Arc::new (RelayState::try_from (config).expect ("Can't create relay state"));
|
let relay_state = Arc::new (Relay::try_from (config).expect ("Can't create relay state"));
|
||||||
let relay_state_2 = relay_state.clone ();
|
let relay_state_2 = relay_state.clone ();
|
||||||
let (stop_relay_tx, stop_relay_rx) = oneshot::channel ();
|
let (stop_relay_tx, stop_relay_rx) = oneshot::channel ();
|
||||||
let task_relay = spawn (async move {
|
let task_relay = spawn (async move {
|
||||||
run_relay (
|
run_relay (
|
||||||
relay_state_2,
|
relay_state_2,
|
||||||
Arc::new (load_templates (&PathBuf::new ())?),
|
&PathBuf::new (),
|
||||||
stop_relay_rx,
|
stop_relay_rx,
|
||||||
None
|
None
|
||||||
).await
|
).await
|
||||||
|
|
Loading…
Reference in New Issue