Move `relay_url` into the server config TOML too

main
_ 2020-11-02 14:41:22 +00:00
parent 1de6c0aca7
commit 26eb395f18
3 changed files with 29 additions and 14 deletions

View File

@ -7,9 +7,6 @@ use structopt::StructOpt;
#[derive (Debug, StructOpt)]
struct Opt {
#[structopt (name = "RELAY_URL")]
relay_url: String,
#[structopt (long)]
file_server_root: Option <PathBuf>,
}
@ -33,8 +30,7 @@ async fn main () -> Result <(), Box <dyn Error>> {
let opt = Opt::from_args ();
let opt = ptth::server::Opt {
relay_url: opt.relay_url,
file_server_root: opt.file_server_root.unwrap_or_else (|| "/home/user".into ()),
};
ptth::server::main (config_file, opt).await

View File

@ -23,6 +23,8 @@ pub fn prefix_match <'a> (hay: &'a str, needle: &str) -> Option <&'a str>
}
}
// Thanks to https://github.com/robsheldon/bad-passwords-index
const BAD_PASSWORDS: &[u8] = include_bytes! ("bad_passwords.txt");
pub fn password_is_bad (mut password: String) -> bool {
@ -103,16 +105,16 @@ mod tests {
assert! (relay_state.list_servers ().await.is_empty ());
let relay_url = "http://127.0.0.1:4000";
let relay_url_2 = relay_url.into ();
let config_file = server::ConfigFile {
name: server_name.into (),
api_key: api_key.into (),
relay_url: relay_url.into (),
file_server_root: None,
};
spawn (async move {
let opt = server::Opt {
relay_url: relay_url_2,
file_server_root: "./".into (),
};
server::main (config_file, opt).await.unwrap ();

View File

@ -23,6 +23,7 @@ use crate::{
pub mod file_server;
struct ServerState {
config: Config,
opt: Opt,
handlebars: Handlebars <'static>,
client: Client,
@ -61,9 +62,14 @@ async fn handle_req_resp <'a> (
let (req_id, parts) = (wrapped_req.id, wrapped_req.req);
let response = if let Some (uri) = prefix_match (&parts.uri, "/files") {
let default_root = PathBuf::from ("./");
let file_server_root: &std::path::Path = state.config.file_server_root
.as_ref ()
.unwrap_or (&default_root);
file_server::serve_all (
&state.handlebars,
&state.opt.file_server_root,
file_server_root,
parts.method,
uri,
&parts.headers
@ -74,7 +80,7 @@ async fn handle_req_resp <'a> (
};
let mut resp_req = state.client
.post (&format! ("{}/7ZSFUKGV_http_response/{}", state.opt.relay_url, req_id))
.post (&format! ("{}/7ZSFUKGV_http_response/{}", state.config.relay_url, req_id))
.header (crate::PTTH_MAGIC_HEADER, base64::encode (rmp_serde::to_vec (&response.parts).unwrap ()));
if let Some (body) = response.body {
@ -93,12 +99,19 @@ async fn handle_req_resp <'a> (
pub struct ConfigFile {
pub name: String,
pub api_key: String,
pub relay_url: String,
pub file_server_root: Option <PathBuf>,
}
#[derive (Default)]
pub struct Config {
pub relay_url: String,
pub file_server_root: Option <PathBuf>,
}
#[derive (Clone)]
pub struct Opt {
pub relay_url: String,
pub file_server_root: PathBuf,
}
pub async fn main (config_file: ConfigFile, opt: Opt)
@ -123,6 +136,10 @@ pub async fn main (config_file: ConfigFile, opt: Opt)
let handlebars = file_server::load_templates ()?;
let state = Arc::new (ServerState {
config: Config {
relay_url: config_file.relay_url,
file_server_root: config_file.file_server_root,
},
opt,
handlebars,
client,
@ -135,7 +152,7 @@ pub async fn main (config_file: ConfigFile, opt: Opt)
delay_for (Duration::from_millis (backoff_delay)).await;
}
let req_req = state.client.get (&format! ("{}/7ZSFUKGV_http_listen/{}", state.opt.relay_url, config_file.name));
let req_req = state.client.get (&format! ("{}/7ZSFUKGV_http_listen/{}", state.config.relay_url, config_file.name));
let err_backoff_delay = std::cmp::min (30_000, backoff_delay * 2 + 500);