Skip to content

Commit d5e7c88

Browse files
authored
Customize Listen Address (#81)
Replace --port with --listen to allow specifying the interface as well Also fix the docker-compose file and adjust tests to this change
1 parent 84d9422 commit d5e7c88

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ system startup. See the docker-compose documentation for more information.
7070
The server is configured with command-line options. See
7171
`taskchampion-sync-server --help` for full details.
7272

73-
The `--data-dir` option specifies where the server should store its data, and
74-
`--port` gives the port on which the HTTP server runs.
73+
The `--listen` option specifies the interface and port the server listens on.
74+
It must contain an IP-Address or a DNS name and a port number. This option is
75+
mandatory, but can be repeated to specify multiple interfaces or ports.
76+
77+
The `--data-dir` option specifies where the server should store its data.
7578

7679
By default, the server allows all client IDs. To limit the accepted client IDs,
7780
such as when running a personal server, use `--allow-client-id <client-id>`.

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ services:
5656
volume:
5757
nocopy: true
5858
subpath: tss
59-
command: --data-dir /tss/taskchampion-sync-server --port 8080
59+
command: --data-dir /tss/taskchampion-sync-server --listen 0.0.0.0:8080
6060
environment:
6161
- RUST_LOG=info
6262
depends_on:

server/src/bin/taskchampion-sync-server.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ fn command() -> Command {
2121
.version(env!("CARGO_PKG_VERSION"))
2222
.about("Server for TaskChampion")
2323
.arg(
24-
arg!(-p --port <PORT> "Port on which to serve")
25-
.help("Port on which to serve")
26-
.value_parser(value_parser!(usize))
27-
.default_value("8080"),
24+
arg!(-l --listen <ADDRESS>)
25+
.help("Address and Port on which to listen on. Can be an IP Address or a DNS name followed by a colon and a port e.g. localhost:8080")
26+
.value_parser(ValueParser::string())
27+
.action(ArgAction::Append)
28+
.required(true),
2829
)
2930
.arg(
3031
arg!(-d --"data-dir" <DIR> "Directory in which to store data")
@@ -62,7 +63,6 @@ async fn main() -> anyhow::Result<()> {
6263
let matches = command().get_matches();
6364

6465
let data_dir: &OsString = matches.get_one("data-dir").unwrap();
65-
let port: usize = *matches.get_one("port").unwrap();
6666
let snapshot_versions: u32 = *matches.get_one("snapshot-versions").unwrap();
6767
let snapshot_days: i64 = *matches.get_one("snapshot-days").unwrap();
6868
let client_id_allowlist: Option<HashSet<Uuid>> = matches
@@ -75,16 +75,17 @@ async fn main() -> anyhow::Result<()> {
7575
};
7676
let server = WebServer::new(config, client_id_allowlist, SqliteStorage::new(data_dir)?);
7777

78-
log::info!("Serving on port {}", port);
79-
HttpServer::new(move || {
78+
let mut http_server = HttpServer::new(move || {
8079
App::new()
8180
.wrap(ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, print_error))
8281
.wrap(Logger::default())
8382
.configure(|cfg| server.config(cfg))
84-
})
85-
.bind(format!("0.0.0.0:{}", port))?
86-
.run()
87-
.await?;
83+
});
84+
for listen_address in matches.get_many::<&str>("listen").unwrap() {
85+
log::info!("Serving on {}", listen_address);
86+
http_server = http_server.bind(listen_address)?
87+
}
88+
http_server.run().await?;
8889
Ok(())
8990
}
9091

@@ -104,14 +105,19 @@ mod test {
104105

105106
#[test]
106107
fn command_allowed_client_ids_none() {
107-
let matches = command().get_matches_from(["tss"]);
108+
let matches = command().get_matches_from(["tss", "--listen", "localhost:8080"]);
108109
assert_eq!(allowed(&matches), None);
109110
}
110111

111112
#[test]
112113
fn command_allowed_client_ids_one() {
113-
let matches =
114-
command().get_matches_from(["tss", "-C", "711d5cf3-0cf0-4eb8-9eca-6f7f220638c0"]);
114+
let matches = command().get_matches_from([
115+
"tss",
116+
"--listen",
117+
"localhost:8080",
118+
"-C",
119+
"711d5cf3-0cf0-4eb8-9eca-6f7f220638c0",
120+
]);
115121
assert_eq!(
116122
allowed(&matches),
117123
Some(vec![Uuid::parse_str(
@@ -125,6 +131,8 @@ mod test {
125131
fn command_allowed_client_ids_two() {
126132
let matches = command().get_matches_from([
127133
"tss",
134+
"--listen",
135+
"localhost:8080",
128136
"-C",
129137
"711d5cf3-0cf0-4eb8-9eca-6f7f220638c0",
130138
"-C",
@@ -141,7 +149,13 @@ mod test {
141149

142150
#[test]
143151
fn command_data_dir() {
144-
let matches = command().get_matches_from(["tss", "--data-dir", "/foo/bar"]);
152+
let matches = command().get_matches_from([
153+
"tss",
154+
"--data-dir",
155+
"/foo/bar",
156+
"--listen",
157+
"localhost:8080",
158+
]);
145159
assert_eq!(matches.get_one::<OsString>("data-dir").unwrap(), "/foo/bar");
146160
}
147161

0 commit comments

Comments
 (0)