Skip to content

Commit c138902

Browse files
committed
Improve bind address logic
Now allows for multiple addresses & ipv6
1 parent 1548dff commit c138902

4 files changed

Lines changed: 50 additions & 20 deletions

File tree

.zed/debug.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Project-local debug tasks
2+
//
3+
// For more documentation on how to configure debug tasks,
4+
// see: https://zed.dev/docs/debugger
5+
[
6+
{
7+
"label": "Build & Debug backend",
8+
"build": {
9+
"command": "cargo",
10+
"args": ["build"]
11+
},
12+
"program": "$ZED_WORKTREE_ROOT/target/debug/backend",
13+
"args": [
14+
"--public-maven-url", "https://repo.polyfrost.org"
15+
],
16+
// sourceLanguages is required for CodeLLDB (not GDB) when using Rust
17+
"sourceLanguages": ["rust"],
18+
"request": "launch",
19+
"adapter": "CodeLLDB"
20+
}
21+
]

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ The main command that starts the backend HTTP server. The server can be configur
1010
Usage: backend [OPTIONS] --public-maven-url <PUBLIC_MAVEN_URL>
1111
1212
Options:
13-
--port <PORT>
14-
The port for the HTTP server to listen on [env: BACKEND_LISTEN_PORT=] [default: 8080]
15-
--host <HOST>
16-
The host address for the HTTP server to listen on [env: BACKEND_LISTEN_HOST=] [default: 0.0.0.0]
13+
--bind <BIND>
14+
The addresses (IP and port) for the HTTP server to bind to [env: BACKEND_LISTEN_PORT=] [default: 0.0.0.0:8080,[::]:8080]
1715
--http1
1816
If passed, the server will be downgraded to HTTP/1.1 rather than HTTP/2 [env: BACKEND_USE_HTTP1=]
1917
--public-maven-url <PUBLIC_MAVEN_URL>
@@ -24,4 +22,4 @@ Options:
2422
Print help
2523
-V, --version
2624
Print version
27-
```
25+
```

flake.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
# Add all build-time dependencies to the environment
4747
packages = cranePackage.buildInputs ++ cranePackage.nativeBuildInputs ++ (with pkgs; [
4848
cargo-deny
49+
evcxr
50+
lldb
4951
]);
5052
};
5153
});

src/main.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod api;
44
mod maven;
55
mod types;
66

7-
use std::{net::Ipv4Addr, time::Duration};
7+
use std::{net::SocketAddr, time::Duration};
88

99
use actix_web::{App, HttpServer, web};
1010
use api::v1::{ApiData, CacheKey, CacheValue, ETagType};
@@ -18,12 +18,17 @@ use url::Url;
1818
#[derive(Parser, Clone)]
1919
#[clap(version, about, long_about = None)]
2020
struct AppCommand {
21-
/// The port for the HTTP server to listen on
22-
#[clap(long, env = "BACKEND_LISTEN_PORT", default_value_t = 8080)]
23-
pub port: u16,
24-
/// The host address for the HTTP server to listen on
25-
#[clap(long, env = "BACKEND_LISTEN_HOST", default_value_t = Ipv4Addr::new(0, 0, 0, 0))]
26-
pub host: Ipv4Addr,
21+
/// The addresses (IP and port) for the HTTP server to bind to.
22+
///
23+
/// Multiple bind addresses can be either split by comma, or passed as
24+
/// multiple flags.
25+
#[clap(
26+
long,
27+
env = "BACKEND_LISTEN_PORT",
28+
value_delimiter = ',',
29+
default_value = "[::]:8080"
30+
)]
31+
pub bind: Vec<SocketAddr>,
2732
/// If passed, the server will be downgraded to HTTP/1.1 rather than HTTP/2
2833
#[clap(long, env = "BACKEND_USE_HTTP1", default_value_t = false)]
2934
pub http1: bool,
@@ -46,7 +51,6 @@ async fn main() {
4651
env_logger::init();
4752

4853
let args = AppCommand::parse();
49-
let listen_args = (args.host, args.port);
5054
let data = web::Data::new(ApiData {
5155
internal_maven_url: args.internal_maven_url.map(|url| url.to_string()),
5256
public_maven_url: args.public_maven_url.to_string(),
@@ -76,14 +80,19 @@ async fn main() {
7680
.build()
7781
});
7882

79-
HttpServer::new(move || {
83+
let mut server = HttpServer::new(move || {
8084
App::new()
8185
.app_data(data.clone())
8286
.configure(api::v1::configure())
83-
})
84-
.bind_auto_h2c(listen_args)
85-
.expect("Unable to bind on specified IP and port")
86-
.run()
87-
.await
88-
.expect("Unable to start HTTP server");
87+
});
88+
89+
// Call .bind for each address, as using multiple in the same call can silently
90+
// fail
91+
for bind_addr in args.bind {
92+
server = server
93+
.bind_auto_h2c(bind_addr)
94+
.expect("Unable to bind on specified address");
95+
}
96+
97+
server.run().await.expect("Unable to start HTTP server");
8998
}

0 commit comments

Comments
 (0)