Skip to content
This repository was archived by the owner on Oct 23, 2022. It is now read-only.

Commit 1c7c5ee

Browse files
committed
fix(http): use multiaddr instead of socketaddr in config
1 parent 2039bc0 commit 1c7c5ee

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

http/src/config.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! go-ipfs compatible configuration file handling and setup.
22
3-
use parity_multiaddr::Multiaddr;
3+
use parity_multiaddr::{multiaddr, Multiaddr};
44
use serde::{Deserialize, Serialize};
55
use std::fs::{self, File};
66
use std::net::SocketAddr;
@@ -87,8 +87,8 @@ fn create(
8787
use std::io::BufWriter;
8888

8989
let api_addr = match profiles[0] {
90-
Profile::Test => "127.0.0.1:0",
91-
Profile::Default => "127.0.0.1:4004",
90+
Profile::Test => multiaddr!(Ip4([127, 0, 0, 1]), Tcp(0u16)),
91+
Profile::Default => multiaddr!(Ip4([127, 0, 0, 1]), Tcp(4004u16)),
9292
};
9393

9494
let bits = bits.get();
@@ -143,7 +143,7 @@ fn create(
143143
},
144144
addresses: Addresses {
145145
swarm: vec!["/ip4/127.0.0.1/tcp/0".parse().unwrap()],
146-
api: api_addr.parse().unwrap(),
146+
api: api_addr,
147147
},
148148
};
149149

@@ -173,7 +173,7 @@ pub enum LoadingError {
173173
/// Returns only the keypair and listening addresses or [`LoadingError`] but this should be
174174
/// extended to contain the bootstrap nodes at least later when we need to support those for
175175
/// testing purposes.
176-
pub fn load(config: File) -> Result<(ipfs::Keypair, Vec<Multiaddr>, SocketAddr), LoadingError> {
176+
pub fn load(config: File) -> Result<(ipfs::Keypair, Vec<Multiaddr>, Multiaddr), LoadingError> {
177177
use std::io::BufReader;
178178

179179
let CompatibleConfigFile {
@@ -272,7 +272,7 @@ struct CompatibleConfigFile {
272272
struct Addresses {
273273
swarm: Vec<Multiaddr>,
274274
#[serde(rename = "API")]
275-
api: SocketAddr,
275+
api: Multiaddr,
276276
}
277277

278278
#[derive(Debug, Serialize, Deserialize)]

http/src/main.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use structopt::StructOpt;
44

55
use ipfs::{Ipfs, IpfsOptions, IpfsTypes, UninitializedIpfs};
66
use ipfs_http::{config, v0};
7+
use parity_multiaddr::{Multiaddr, Protocol};
78

89
#[macro_use]
910
extern crate tracing;
@@ -189,18 +190,29 @@ fn main() {
189190

190191
fn serve<Types: IpfsTypes>(
191192
ipfs: &Ipfs<Types>,
192-
listening_addr: std::net::SocketAddr,
193+
listening_addr: Multiaddr,
193194
) -> (std::net::SocketAddr, impl std::future::Future<Output = ()>) {
194195
use tokio::stream::StreamExt;
195196
use warp::Filter;
197+
196198
let (shutdown_tx, mut shutdown_rx) = tokio::sync::mpsc::channel::<()>(1);
197199

198200
let routes = v0::routes(ipfs, shutdown_tx);
199201
let routes = routes.with(warp::log(env!("CARGO_PKG_NAME")));
200202

201203
let ipfs = ipfs.clone();
202204

203-
warp::serve(routes).bind_with_graceful_shutdown(listening_addr, async move {
205+
let components = listening_addr.iter().collect::<Vec<_>>();
206+
207+
use std::net::SocketAddr;
208+
let socket_addr =
209+
if let (Protocol::Ip4(ip), Protocol::Tcp(port)) = (&components[0], &components[1]) {
210+
SocketAddr::new(ip.clone().into(), *port)
211+
} else {
212+
panic!("Couldn't convert MultiAddr into SocketAddr")
213+
};
214+
215+
warp::serve(routes).bind_with_graceful_shutdown(socket_addr, async move {
204216
shutdown_rx.next().await;
205217
info!("Shutdown trigger received; starting shutdown");
206218
ipfs.exit_daemon().await;

0 commit comments

Comments
 (0)