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

Commit

Permalink
fix(http): use multiaddr instead of socketaddr in config
Browse files Browse the repository at this point in the history
  • Loading branch information
niklaslong committed Oct 26, 2020
1 parent 2039bc0 commit 9fb93fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
13 changes: 6 additions & 7 deletions http/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! go-ipfs compatible configuration file handling and setup.
use parity_multiaddr::Multiaddr;
use parity_multiaddr::{multiaddr, Multiaddr};
use serde::{Deserialize, Serialize};
use std::fs::{self, File};
use std::net::SocketAddr;
use std::num::NonZeroU16;
use std::path::Path;
use std::str::FromStr;
Expand Down Expand Up @@ -87,8 +86,8 @@ fn create(
use std::io::BufWriter;

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

let bits = bits.get();
Expand Down Expand Up @@ -143,7 +142,7 @@ fn create(
},
addresses: Addresses {
swarm: vec!["/ip4/127.0.0.1/tcp/0".parse().unwrap()],
api: api_addr.parse().unwrap(),
api: api_addr,
},
};

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

let CompatibleConfigFile {
Expand Down Expand Up @@ -272,7 +271,7 @@ struct CompatibleConfigFile {
struct Addresses {
swarm: Vec<Multiaddr>,
#[serde(rename = "API")]
api: SocketAddr,
api: Multiaddr,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
16 changes: 14 additions & 2 deletions http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use structopt::StructOpt;

use ipfs::{Ipfs, IpfsOptions, IpfsTypes, UninitializedIpfs};
use ipfs_http::{config, v0};
use parity_multiaddr::{Multiaddr, Protocol};

#[macro_use]
extern crate tracing;
Expand Down Expand Up @@ -189,18 +190,29 @@ fn main() {

fn serve<Types: IpfsTypes>(
ipfs: &Ipfs<Types>,
listening_addr: std::net::SocketAddr,
listening_addr: Multiaddr,
) -> (std::net::SocketAddr, impl std::future::Future<Output = ()>) {
use tokio::stream::StreamExt;
use warp::Filter;

let (shutdown_tx, mut shutdown_rx) = tokio::sync::mpsc::channel::<()>(1);

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

let ipfs = ipfs.clone();

warp::serve(routes).bind_with_graceful_shutdown(listening_addr, async move {
let components = listening_addr.iter().collect::<Vec<_>>();

use std::net::SocketAddr;
let socket_addr =
if let (Protocol::Ip4(ip), Protocol::Tcp(port)) = (&components[0], &components[1]) {
SocketAddr::new(ip.clone().into(), *port)
} else {
panic!("Couldn't convert MultiAddr into SocketAddr")
};

warp::serve(routes).bind_with_graceful_shutdown(socket_addr, async move {
shutdown_rx.next().await;
info!("Shutdown trigger received; starting shutdown");
ipfs.exit_daemon().await;
Expand Down

0 comments on commit 9fb93fc

Please sign in to comment.