summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-07-22 11:19:57 +0300
committerLars Wirzenius <liw@liw.fi>2021-07-22 13:05:58 +0300
commit16ba7070a6e040918f354b87f952cc47a7df6b5a (patch)
tree67c4fae0e8ebae013a962aba0614744ee7f16e37
parentdd6bf8292fbe53eac2c528ff613cd4420ba46655 (diff)
downloadewww-16ba7070a6e040918f354b87f952cc47a7df6b5a.tar.gz
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
-rw-r--r--src/main.rs13
-rw-r--r--subplot/ewww.py10
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<Config> {
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)