fix: decrease ipfs-http compilation times 50%
by boxing the filters on debug builds.
This commit is contained in:
parent
7f97292639
commit
0ee98a4c08
117
http/src/v0.rs
117
http/src/v0.rs
@ -1,5 +1,6 @@
|
|||||||
use ipfs::{Ipfs, IpfsTypes};
|
use ipfs::{Ipfs, IpfsTypes};
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
|
use warp::{query, Filter};
|
||||||
|
|
||||||
pub mod bitswap;
|
pub mod bitswap;
|
||||||
pub mod block;
|
pub mod block;
|
||||||
@ -14,64 +15,86 @@ pub mod support;
|
|||||||
pub use support::recover_as_message_response;
|
pub use support::recover_as_message_response;
|
||||||
pub(crate) use support::{with_ipfs, InvalidPeerId, NotImplemented, StringError};
|
pub(crate) use support::{with_ipfs, InvalidPeerId, NotImplemented, StringError};
|
||||||
|
|
||||||
pub fn routes<T>(
|
/// Helper to combine the multiple filters together with Filter::or, possibly boxing the types in
|
||||||
|
/// the process. This greatly helps the build times for `ipfs-http`.
|
||||||
|
macro_rules! combine {
|
||||||
|
($x:expr, $($y:expr),+) => {
|
||||||
|
{
|
||||||
|
let filter = boxed_on_debug!($x);
|
||||||
|
$(
|
||||||
|
let filter = boxed_on_debug!(filter.or($y));
|
||||||
|
)+
|
||||||
|
filter
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
macro_rules! boxed_on_debug {
|
||||||
|
($x:expr) => {
|
||||||
|
$x.boxed()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
macro_rules! boxed_on_debug {
|
||||||
|
($x:expr) => {
|
||||||
|
$x
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn routes<T: IpfsTypes>(
|
||||||
ipfs: &Ipfs<T>,
|
ipfs: &Ipfs<T>,
|
||||||
shutdown_tx: tokio::sync::mpsc::Sender<()>,
|
shutdown_tx: tokio::sync::mpsc::Sender<()>,
|
||||||
) -> impl warp::Filter<Extract = impl warp::Reply, Error = Infallible> + Clone
|
) -> impl warp::Filter<Extract = impl warp::Reply, Error = Infallible> + Clone {
|
||||||
where
|
let mount = warp::path("api").and(warp::path("v0"));
|
||||||
T: IpfsTypes,
|
|
||||||
{
|
|
||||||
use warp::{query, Filter};
|
|
||||||
// /api/v0/shutdown
|
|
||||||
let shutdown = warp::post()
|
let shutdown = warp::post()
|
||||||
.and(warp::path!("shutdown"))
|
.and(warp::path!("shutdown"))
|
||||||
.and(warp::any().map(move || shutdown_tx.clone()))
|
.and(warp::any().map(move || shutdown_tx.clone()))
|
||||||
.and_then(handle_shutdown);
|
.and_then(handle_shutdown);
|
||||||
|
|
||||||
let mount = warp::path("api").and(warp::path("v0"));
|
let api = mount.and(combine!(
|
||||||
|
shutdown,
|
||||||
let api = mount.and(
|
id::identity(ipfs),
|
||||||
shutdown
|
warp::path!("add").and_then(not_implemented),
|
||||||
.or(id::identity(ipfs))
|
bitswap::wantlist(ipfs),
|
||||||
// Placeholder paths
|
bitswap::stat(ipfs),
|
||||||
// https://docs.rs/warp/0.2.2/warp/macro.path.html#path-prefixes
|
block::get(ipfs),
|
||||||
.or(warp::path!("add").and_then(not_implemented))
|
block::put(ipfs),
|
||||||
.or(bitswap::wantlist(ipfs))
|
block::rm(ipfs),
|
||||||
.or(bitswap::stat(ipfs))
|
block::stat(ipfs),
|
||||||
.or(block::get(ipfs))
|
warp::path!("bootstrap" / ..).and_then(not_implemented),
|
||||||
.or(block::put(ipfs))
|
warp::path!("config" / ..).and_then(not_implemented),
|
||||||
.or(block::rm(ipfs))
|
dag::put(ipfs),
|
||||||
.or(block::stat(ipfs))
|
dag::resolve(ipfs),
|
||||||
.or(warp::path!("bootstrap" / ..).and_then(not_implemented))
|
warp::path!("dht" / ..).and_then(not_implemented),
|
||||||
.or(warp::path!("config" / ..).and_then(not_implemented))
|
warp::path!("get").and_then(not_implemented),
|
||||||
.or(dag::put(ipfs))
|
warp::path!("key" / ..).and_then(not_implemented),
|
||||||
.or(dag::resolve(ipfs))
|
warp::path!("name" / ..).and_then(not_implemented),
|
||||||
.or(warp::path!("dht" / ..).and_then(not_implemented))
|
warp::path!("object" / ..).and_then(not_implemented),
|
||||||
.or(warp::path!("get").and_then(not_implemented))
|
warp::path!("pin" / ..).and_then(not_implemented),
|
||||||
.or(warp::path!("key" / ..).and_then(not_implemented))
|
warp::path!("ping" / ..).and_then(not_implemented),
|
||||||
.or(warp::path!("name" / ..).and_then(not_implemented))
|
pubsub::routes(ipfs),
|
||||||
.or(warp::path!("object" / ..).and_then(not_implemented))
|
refs::local(ipfs),
|
||||||
.or(warp::path!("pin" / ..).and_then(not_implemented))
|
refs::refs(ipfs),
|
||||||
.or(warp::path!("ping" / ..).and_then(not_implemented))
|
warp::path!("repo" / ..).and_then(not_implemented),
|
||||||
.or(pubsub::routes(ipfs))
|
warp::path!("stats" / ..).and_then(not_implemented),
|
||||||
.or(refs::local(ipfs))
|
swarm::connect(ipfs),
|
||||||
.or(refs::refs(ipfs))
|
swarm::peers(ipfs),
|
||||||
.or(warp::path!("repo" / ..).and_then(not_implemented))
|
swarm::addrs(ipfs),
|
||||||
.or(warp::path!("stats" / ..).and_then(not_implemented))
|
swarm::addrs_local(ipfs),
|
||||||
.or(swarm::connect(ipfs))
|
swarm::disconnect(ipfs),
|
||||||
.or(swarm::peers(ipfs))
|
warp::path!("version")
|
||||||
.or(swarm::addrs(ipfs))
|
.and(query::<version::Query>())
|
||||||
.or(swarm::addrs_local(ipfs))
|
.and_then(version::version)
|
||||||
.or(swarm::disconnect(ipfs))
|
));
|
||||||
.or(warp::path!("version")
|
|
||||||
.and(query::<version::Query>())
|
|
||||||
.and_then(version::version)),
|
|
||||||
);
|
|
||||||
|
|
||||||
|
// have a common handler turn the rejections into 400 or 500 with json body
|
||||||
api.recover(recover_as_message_response)
|
api.recover(recover_as_message_response)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_shutdown(
|
pub(crate) async fn handle_shutdown(
|
||||||
mut tx: tokio::sync::mpsc::Sender<()>,
|
mut tx: tokio::sync::mpsc::Sender<()>,
|
||||||
) -> Result<impl warp::Reply, std::convert::Infallible> {
|
) -> Result<impl warp::Reply, std::convert::Infallible> {
|
||||||
Ok(match tx.send(()).await {
|
Ok(match tx.send(()).await {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user