From 16ba7070a6e040918f354b87f952cc47a7df6b5a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 22 Jul 2021 11:19:57 +0300 Subject: feat! allow/require setting address on which to listen This will break all existing config file, of which there should be none. Sponsored-by: author --- src/main.rs | 13 ++++++++++--- subplot/ewww.py | 10 ++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index e120534..38a6c44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use serde::Deserialize; +use std::net::{SocketAddr, ToSocketAddrs}; use std::path::{Path, PathBuf}; use structopt::StructOpt; use warp::Filter; @@ -8,7 +9,7 @@ extern crate log; #[derive(Debug, Deserialize)] struct Config { webroot: PathBuf, - port: u16, + listen: String, tls_key: PathBuf, tls_cert: PathBuf, } @@ -34,7 +35,7 @@ enum EwwwError { } #[tokio::main] -async fn main() { +async fn main() -> anyhow::Result<()> { pretty_env_logger::init(); info!("ewww starting up"); @@ -42,7 +43,11 @@ async fn main() { let config = read_config(&opt.config).unwrap(); let webroot = config.webroot.canonicalize().unwrap(); + let addr: SocketAddr = config.listen.to_socket_addrs().unwrap().next().unwrap(); + info!("webroot: {:?}", webroot); + info!("listen on {}", addr); + let log = warp::log("ewww"); let webroot = warp::any().and(warp::fs::dir(webroot)).with(log); @@ -50,8 +55,10 @@ async fn main() { .tls() .key_path(config.tls_key) .cert_path(config.tls_cert) - .run(([127, 0, 0, 1], config.port)) + .run(addr) .await; + + Ok(()) } fn read_config(filename: &Path) -> anyhow::Result { diff --git a/subplot/ewww.py b/subplot/ewww.py index e30c219..a6c4c4f 100644 --- a/subplot/ewww.py +++ b/subplot/ewww.py @@ -24,10 +24,10 @@ def _write(filename, content): # Construct a URL that points to server running on localhost by # replacing the actual scheme and host with ones that work for test. def _url(ctx, url): - port = ctx["config"]["port"] + listen = ctx["config"]["listen"] c = urllib.parse.urlparse(url) host = c[1] - c = (c[0], "localhost:{}".format(port)) + c[2:] + c = (c[0], "{}".format(listen)) + c[2:] return urllib.parse.urlunparse(c), host @@ -64,9 +64,11 @@ def start_server(ctx, filename=None): logging.debug(f"Starting ewww with config file {filename}") config = get_file(filename).decode("UTF-8") config = yaml.safe_load(config) - port = config["port"] = random.randint(2000, 30000) - logging.debug(f"Picked randomly port for ewww: {config['port']}") + port = random.randint(2000, 30000) + logging.debug(f"Picked randomly port for ewww: {port}") + config["listen"] = f"localhost:{port}" ctx["config"] = config + logging.debug(f"ewww config: {config!r}") config = yaml.safe_dump(config) _write(filename, config) -- cgit v1.2.1