summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-12-23 19:13:25 +0200
committerLars Wirzenius <liw@liw.fi>2020-12-23 19:13:25 +0200
commitc7bc29635624984aee3e620680a264fd32b27e50 (patch)
tree2741c71aab30827e833126e38f3fc31984abed68
parent0967052ec4d6dfbd428a1c0027990f9c2610c040 (diff)
downloadobnam2-c7bc29635624984aee3e620680a264fd32b27e50.tar.gz
feat! let server address be configured
Instead of just specifying port, let the address (or name) be configured.
-rw-r--r--server.yaml2
-rw-r--r--src/bin/obnam-server.rs21
-rw-r--r--subplot/server.py15
3 files changed, 24 insertions, 14 deletions
diff --git a/server.yaml b/server.yaml
index 4e1e57b..ef40fe6 100644
--- a/server.yaml
+++ b/server.yaml
@@ -1,4 +1,4 @@
-port: 8888
+address: localhost:8888
chunks: /home/liw/tmp/chunks
tls_key: test.key
tls_cert: test.pem
diff --git a/src/bin/obnam-server.rs b/src/bin/obnam-server.rs
index 682b5a7..e695870 100644
--- a/src/bin/obnam-server.rs
+++ b/src/bin/obnam-server.rs
@@ -7,6 +7,7 @@ use obnam::indexedstore::IndexedStore;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::default::Default;
+use std::net::{SocketAddr, ToSocketAddrs};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use structopt::StructOpt;
@@ -28,6 +29,13 @@ async fn main() -> anyhow::Result<()> {
let opt = Opt::from_args();
let config = Config::read_config(&opt.config).unwrap();
+ let addresses: Vec<SocketAddr> = config.address.to_socket_addrs()?.collect();
+ if addresses.is_empty() {
+ error!("specified address is empty set: {:?}", addresses);
+ eprintln!("ERROR: server address is empty: {:?}", addresses);
+ return Err(ConfigError::BadServerAddress.into());
+ }
+
let store = IndexedStore::new(&config.chunks);
let store = Arc::new(Mutex::new(store));
let store = warp::any().map(move || Arc::clone(&store));
@@ -69,7 +77,7 @@ async fn main() -> anyhow::Result<()> {
.tls()
.key_path(config.tls_key)
.cert_path(config.tls_cert)
- .run(([127, 0, 0, 1], config.port))
+ .run(addresses[0])
.await;
Ok(())
}
@@ -77,16 +85,13 @@ async fn main() -> anyhow::Result<()> {
#[derive(Debug, Deserialize, Clone)]
pub struct Config {
pub chunks: PathBuf,
- pub port: u16,
+ pub address: String,
pub tls_key: PathBuf,
pub tls_cert: PathBuf,
}
#[derive(Debug, thiserror::Error)]
enum ConfigError {
- #[error("Port number {0} too small, would require running as root")]
- PortTooSmall(u16),
-
#[error("Directory for chunks {0} does not exist")]
ChunksDirNotFound(PathBuf),
@@ -95,6 +100,9 @@ enum ConfigError {
#[error("TLS key {0} does not exist")]
TlsKeyNotFound(PathBuf),
+
+ #[error("server address can't be resolved")]
+ BadServerAddress,
}
impl Config {
@@ -106,9 +114,6 @@ impl Config {
}
pub fn check(&self) -> anyhow::Result<()> {
- if self.port < 1024 {
- return Err(ConfigError::PortTooSmall(self.port).into());
- }
if !self.chunks.exists() {
return Err(ConfigError::ChunksDirNotFound(self.chunks.clone()).into());
}
diff --git a/subplot/server.py b/subplot/server.py
index c4c01e1..7abe498 100644
--- a/subplot/server.py
+++ b/subplot/server.py
@@ -26,14 +26,19 @@ def start_chunk_server(ctx):
chunks = "chunks"
os.mkdir(chunks)
- config = {"chunks": chunks, "tls_key": "test.key", "tls_cert": "test.pem"}
- port = config["port"] = random.randint(2000, 30000)
+ port = random.randint(2000, 30000)
+ ctx["config"] = config = {
+ "chunks": chunks,
+ "tls_key": "test.key",
+ "tls_cert": "test.pem",
+ "address": f"localhost:{port}",
+ }
+
filename = "config.yaml"
yaml.safe_dump(config, stream=open(filename, "w"))
- logging.debug(f"Picked randomly port for obnam-server: {config['port']}")
- ctx["config"] = config
+ logging.debug(f"Picked randomly port for obnam-server: {config['address']}")
- ctx["server_url"] = f"https://localhost:{port}/chunks"
+ ctx["server_url"] = f"https://{config['address']}/chunks"
start_daemon(
ctx,