diff --git a/current.json b/current.json new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/http-api/resources/swarm.js b/src/http-api/resources/swarm.js index f311526884..983498f35c 100644 --- a/src/http-api/resources/swarm.js +++ b/src/http-api/resources/swarm.js @@ -25,9 +25,9 @@ exports.parseAddrs = (request, reply) => { } exports.peers = { - // main route handler which is called after the above `parseArgs`, but only if the args were valid handler: (request, reply) => { - request.server.app.ipfs.swarm.peers((err, peers) => { + const ipfs = request.server.app.ipfs + ipfs.swarm.peers((err, peers) => { if (err) { log.error(err) return reply({ @@ -37,18 +37,35 @@ exports.peers = { } return reply({ - Strings: Object.keys(peers) - .map((key) => - `${peers[key].multiaddrs[0].toString()}/ipfs/${peers[key].id.toB58String()}`) + Strings: peers.map((addr) => addr.toString()) + }) + }) + } +} + +exports.addrs = { + handler: (request, reply) => { + const ipfs = request.server.app.ipfs + ipfs.libp2p.swarm.addrs((err, addrs) => { + if (err) { + log.error(err) + return reply({ + Message: err.toString(), + Code: 0 + }).code(500) + } + + return reply({ + Addrs: addrs.map((addr) => addr.toString()) }) }) } } exports.localAddrs = { - // main route handler which is called after the above `parseArgs`, but only if the args were valid handler: (request, reply) => { - request.server.app.ipfs.swarm.localAddrs((err, addrs) => { + const ipfs = request.server.app.ipfs + ipfs.swarm.localAddrs((err, addrs) => { if (err) { log.error(err) return reply({ @@ -71,8 +88,9 @@ exports.connect = { // main route handler which is called after the above `parseArgs`, but only if the args were valid handler: (request, reply) => { const addr = request.pre.args.addr + const ipfs = request.server.app.ipfs - request.server.app.ipfs.swarm.connect(addr, (err) => { + ipfs.swarm.connect(addr, (err) => { if (err) { log.error(err) return reply({ @@ -87,3 +105,28 @@ exports.connect = { }) } } + +exports.disconnect = { + // uses common parseAddr method that returns a `addr` + parseArgs: exports.parseAddrs, + + // main route handler which is called after the above `parseArgs`, but only if the args were valid + handler: (request, reply) => { + const addr = request.pre.args.addr + const ipfs = request.server.app.ipfs + + ipfs.libp2p.swarm.disconnect(addr, (err) => { + if (err) { + log.error(err) + return reply({ + Message: err.toString(), + Code: 0 + }).code(500) + } + + return reply({ + Strings: [`disconnect ${addr} success`] + }) + }) + } +} diff --git a/src/http-api/routes/swarm.js b/src/http-api/routes/swarm.js index 3b4844cd5a..0667acded4 100644 --- a/src/http-api/routes/swarm.js +++ b/src/http-api/routes/swarm.js @@ -13,13 +13,13 @@ module.exports = (server) => { } }) - // api.route({ - // method: '*', - // path: '/api/v0/swarm/addrs', - // config: { - // handler: resources.swarm.addrs.handler - // } - // }) + api.route({ + method: '*', + path: '/api/v0/swarm/addrs', + config: { + handler: resources.swarm.addrs.handler + } + }) api.route({ method: '*', @@ -40,13 +40,16 @@ module.exports = (server) => { } }) - // api.route({ - // method: '*', - // path: '/api/v0/swarm/disconnect', - // config: { - // handler: resources.swarm.disconnect - // } - // }) + api.route({ + method: '*', + path: '/api/v0/swarm/disconnect', + config: { + pre: [ + { method: resources.swarm.disconnect.parseArgs, assign: 'args' } + ], + handler: resources.swarm.disconnect.handler + } + }) // TODO // api.route({ diff --git a/test/http-api/interface-ipfs-core-over-ipfs-api/test-swarm.js b/test/http-api/interface-ipfs-core-over-ipfs-api/test-swarm.js index c8e332e12d..d0527da0f3 100644 --- a/test/http-api/interface-ipfs-core-over-ipfs-api/test-swarm.js +++ b/test/http-api/interface-ipfs-core-over-ipfs-api/test-swarm.js @@ -2,7 +2,6 @@ 'use strict' -/* const test = require('interface-ipfs-core') const FactoryClient = require('./../../utils/factory-http') @@ -17,7 +16,5 @@ const common = { fc.dismantle(callback) } } -*/ -// TODO -// Needs: https://github.com/ipfs/js-libp2p-ipfs/pull/16 -// test.swarm(common) + +test.swarm(common)