♻️ refactor: custom servers won't create a file server module.

main
_ 2021-05-09 19:54:09 +00:00
parent 6c826b0cc5
commit b018f16794
1 changed files with 26 additions and 26 deletions

View File

@ -73,7 +73,7 @@ pub mod load_toml;
use errors::ServerError; use errors::ServerError;
pub struct State { pub struct State {
file_server: file_server::FileServer, // file_server: file_server::FileServer,
config: Config, config: Config,
client: Client, client: Client,
} }
@ -179,13 +179,6 @@ SH: Send + FnMut () -> H
let (req_id, parts) = (wrapped_req.id, wrapped_req.req); let (req_id, parts) = (wrapped_req.id, wrapped_req.req);
debug! ("Handling request {}", req_id); debug! ("Handling request {}", req_id);
/*
let response = state.file_server.serve_all (
parts.method,
&parts.uri,
&parts.headers,
).await?;
*/
let f = handler (parts); let f = handler (parts);
let response = f.await?; let response = f.await?;
@ -246,6 +239,9 @@ impl ConfigFile {
#[derive (Default)] #[derive (Default)]
pub struct Config { pub struct Config {
/// The name of our `ptth_server` instance
pub name: String,
/// URL of the PTTH relay server that ptth_server should connect to /// URL of the PTTH relay server that ptth_server should connect to
pub relay_url: String, pub relay_url: String,
@ -294,6 +290,8 @@ impl Builder {
} }
} }
/// Runs a `ptth_server` instance with the built-in file server module
pub async fn run_server ( pub async fn run_server (
config_file: ConfigFile, config_file: ConfigFile,
shutdown_oneshot: oneshot::Receiver <()>, shutdown_oneshot: oneshot::Receiver <()>,
@ -302,19 +300,34 @@ pub async fn run_server (
) )
-> Result <(), ServerError> -> Result <(), ServerError>
{ {
let metrics_interval = Arc::new (arc_swap::ArcSwap::default ());
let interval_writer = Arc::clone (&metrics_interval);
tokio::spawn (async move {
file_server::metrics::Interval::monitor (interval_writer).await;
});
let file_server = file_server::FileServer::new (
config_file.file_server_root.clone (),
&asset_root.clone ().unwrap_or_else (PathBuf::new),
config_file.name.clone (),
metrics_interval,
hidden_path.clone (),
)?;
let file_server = Arc::new (file_server);
let state = Arc::new (State::new ( let state = Arc::new (State::new (
config_file, config_file,
hidden_path, hidden_path,
asset_root, asset_root,
)?); )?);
let state_2 = Arc::clone (&state); let file_server_2 = Arc::clone (&file_server);
let mut spawn_handler = || { let mut spawn_handler = || {
let state = Arc::clone (&state_2); let file_server = Arc::clone (&file_server);
|req: http_serde::RequestParts| async move { |req: http_serde::RequestParts| async move {
Ok (state.file_server.serve_all (req.method, &req.uri, &req.headers).await?) Ok (file_server.serve_all (req.method, &req.uri, &req.headers).await?)
} }
}; };
@ -350,22 +363,9 @@ impl State {
.connect_timeout (Duration::from_secs (30)) .connect_timeout (Duration::from_secs (30))
.build ().map_err (ServerError::CantBuildHttpClient)?; .build ().map_err (ServerError::CantBuildHttpClient)?;
let metrics_interval = Arc::new (ArcSwap::default ());
let interval_writer = Arc::clone (&metrics_interval);
tokio::spawn (async move {
file_server::metrics::Interval::monitor (interval_writer).await;
});
let state = State { let state = State {
file_server: file_server::FileServer::new (
config_file.file_server_root,
&asset_root,
config_file.name,
metrics_interval,
hidden_path,
)?,
config: Config { config: Config {
name: config_file.name,
relay_url: config_file.relay_url, relay_url: config_file.relay_url,
throttle_upload: config_file.throttle_upload, throttle_upload: config_file.throttle_upload,
}, },
@ -408,7 +408,7 @@ impl State {
debug! ("http_listen"); debug! ("http_listen");
let req_req = state.client.get (&format! ("{}/http_listen/{}", state.config.relay_url, state.file_server.metrics_startup.server_name)) let req_req = state.client.get (&format! ("{}/http_listen/{}", state.config.relay_url, state.config.name))
.timeout (Duration::from_secs (30)) .timeout (Duration::from_secs (30))
.send (); .send ();