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

Commit

Permalink
feat: implement /dns
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed Sep 1, 2020
1 parent 3cb2db4 commit 1bccb91
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion conformance/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const factory = createFactory(options)
// Phase 1.0-ish
//
tests.miscellaneous(factory, { skip: [
'dns',
// recursive resolving is not implemented yet
'should recursively resolve ipfs.io',
// the cidBase param is not implemented yet
'should resolve an IPFS hash and return a base64url encoded CID in path',
// different Cid, the /path/to/testfile.txt suffix shouldn't be there
Expand Down
1 change: 1 addition & 0 deletions http/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub fn routes<T: IpfsTypes>(
and_boxed!(warp::path!("id"), id::identity(ipfs)),
and_boxed!(warp::path!("add"), root_files::add(ipfs)),
and_boxed!(warp::path!("cat"), root_files::cat(ipfs)),
and_boxed!(warp::path!("dns"), ipns::dns(ipfs)),
and_boxed!(warp::path!("get"), root_files::get(ipfs)),
and_boxed!(warp::path!("refs" / "local"), refs::local(ipfs)),
and_boxed!(warp::path!("refs"), refs::refs(ipfs)),
Expand Down
31 changes: 31 additions & 0 deletions http/src/v0/ipns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,34 @@ async fn resolve_query<T: IpfsTypes>(
struct ResolveResponse {
path: String,
}

#[derive(Debug, Deserialize)]
pub struct DnsQuery {
// the name to resolve
arg: StringSerialized<IpfsPath>,
}

pub fn dns<T: IpfsTypes>(
ipfs: &Ipfs<T>,
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
with_ipfs(ipfs).and(query::<DnsQuery>()).and_then(dns_query)
}

async fn dns_query<T: IpfsTypes>(ipfs: Ipfs<T>, query: DnsQuery) -> Result<impl Reply, Rejection> {
let DnsQuery { arg, .. } = query;
let path = ipfs
.resolve(&arg.into_inner())
.await
.map_err(StringError::from)?
.to_string();

let response = DnsResponse { path };

Ok(warp::reply::json(&response))
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
struct DnsResponse {
path: String,
}
9 changes: 7 additions & 2 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ impl FromStr for IpfsPath {
let empty = subpath.next().expect("there's always the first split");

let root = if !empty.is_empty() {
// by default if there is no prefix it's an ipfs or ipld path
PathRoot::Ipld(Cid::try_from(empty)?)
// by default if there is no prefix it's an ipfs or ipld path...
if let Ok(cid) = Cid::try_from(empty) {
PathRoot::Ipld(cid)
} else {
// ...but if that isn't the case, it might be a domain name too
PathRoot::Dns(empty.to_string())
}
} else {
let root_type = subpath.next();
let key = subpath.next();
Expand Down

0 comments on commit 1bccb91

Please sign in to comment.