diff --git a/src/cli/commands/version.js b/src/cli/commands/version.js index 337c2f55fa..09375cf4b0 100644 --- a/src/cli/commands/version.js +++ b/src/cli/commands/version.js @@ -1,7 +1,7 @@ 'use strict' const Command = require('ronin').Command -const IPFS = require('../../ipfs-core') +const utils = require('../utils') const debug = require('debug') const log = debug('cli:version') log.error = debug('cli:version:error') @@ -26,11 +26,16 @@ module.exports = Command.extend({ }, run: (name) => { - var node = new IPFS() - node.version((err, version) => { + var ipfs = utils.getIPFS() + ipfs.version((err, version) => { if (err) { return log.error(err) } - console.log(version) + if (typeof version === 'object') { // js-ipfs-api output + console.log('ipfs version', version.Version) + return + } + + console.log('ipfs version', version) }) } }) diff --git a/src/http-api/index.js b/src/http-api/index.js index ffdf6d6e8e..fce4526485 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -46,7 +46,7 @@ exports.start = (callback) => { server.connection({ host: gateway[2], port: gateway[4], labels: 'Gateway' }) // load routes - require('./routes') + require('./routes')(server) server.start((err) => { if (err) { diff --git a/src/http-api/resources/version.js b/src/http-api/resources/version.js index 9f14c80160..f863cb117c 100644 --- a/src/http-api/resources/version.js +++ b/src/http-api/resources/version.js @@ -18,8 +18,7 @@ exports.get = (request, reply) => { Version: ipfsVersion, Commit: '', Repo: repoVersion - }).header('Transfer-Encoding', 'chunked') - .type('application/json') + }) }) }) } diff --git a/src/http-api/routes/block.js b/src/http-api/routes/block.js index 570365ea0d..bd30e569b2 100644 --- a/src/http-api/routes/block.js +++ b/src/http-api/routes/block.js @@ -1,10 +1,12 @@ -const api = require('./../index.js').server.select('API') const resources = require('./../resources') // TODO +module.exports = (server) => { + const api = server.select('API') -api.route({ - method: 'GET', - path: '/api/v0/block', - handler: resources.block -}) + api.route({ + method: 'GET', + path: '/api/v0/block', + handler: resources.block + }) +} diff --git a/src/http-api/routes/bootstrap.js b/src/http-api/routes/bootstrap.js index 18e1f32934..7ec21c2e22 100644 --- a/src/http-api/routes/bootstrap.js +++ b/src/http-api/routes/bootstrap.js @@ -1,47 +1,50 @@ -const api = require('./../index.js').server.select('API') const resources = require('./../resources') const Joi = require('joi') -// https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L818 -api.route({ - method: 'GET', - path: '/api/v0/bootstrap', - handler: resources.bootstrap.list -}) +module.exports = (server) => { + const api = server.select('API') -// https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L866 -api.route({ - method: 'GET', - path: '/api/v0/bootstrap/add', - handler: resources.bootstrap.add, - config: { - validate: { - query: { - arg: Joi.string().required(), // multiaddr to add - default: Joi.boolean() + // https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L818 + api.route({ + method: 'GET', + path: '/api/v0/bootstrap', + handler: resources.bootstrap.list + }) + + // https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L866 + api.route({ + method: 'GET', + path: '/api/v0/bootstrap/add', + handler: resources.bootstrap.add, + config: { + validate: { + query: { + arg: Joi.string().required(), // multiaddr to add + default: Joi.boolean() + } } } - } -}) + }) -// https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L1081 -api.route({ - method: 'GET', - path: '/api/v0/bootstrap/list', - handler: resources.bootstrap.list -}) + // https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L1081 + api.route({ + method: 'GET', + path: '/api/v0/bootstrap/list', + handler: resources.bootstrap.list + }) -// https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L1131 -api.route({ - method: 'GET', - path: '/api/v0/bootstrap/rm', - handler: resources.bootstrap.rm, - config: { - validate: { - query: { - arg: Joi.string().required(), // multiaddr to rm - all: Joi.boolean() + // https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L1131 + api.route({ + method: 'GET', + path: '/api/v0/bootstrap/rm', + handler: resources.bootstrap.rm, + config: { + validate: { + query: { + arg: Joi.string().required(), // multiaddr to rm + all: Joi.boolean() + } } } - } -}) + }) +} diff --git a/src/http-api/routes/config.js b/src/http-api/routes/config.js index 8ea51c2251..7c3ec378b8 100644 --- a/src/http-api/routes/config.js +++ b/src/http-api/routes/config.js @@ -1,34 +1,37 @@ -const api = require('./../index.js').server.select('API') const resources = require('./../resources') -api.route({ - method: '*', - path: '/api/v0/config/{key?}', - config: { - pre: [ - { method: resources.config.getOrSet.parseArgs, assign: 'args' } - ], - handler: resources.config.getOrSet.handler - } -}) +module.exports = (server) => { + const api = server.select('API') -api.route({ - method: '*', - path: '/api/v0/config/show', - handler: resources.config.show -}) + api.route({ + method: '*', + path: '/api/v0/config/{key?}', + config: { + pre: [ + { method: resources.config.getOrSet.parseArgs, assign: 'args' } + ], + handler: resources.config.getOrSet.handler + } + }) -api.route({ - method: '*', - path: '/api/v0/config/replace', - config: { - payload: { - parse: false, - output: 'stream' - }, - pre: [ - { method: resources.config.replace.parseArgs, assign: 'args' } - ], - handler: resources.config.replace.handler - } -}) + api.route({ + method: '*', + path: '/api/v0/config/show', + handler: resources.config.show + }) + + api.route({ + method: '*', + path: '/api/v0/config/replace', + config: { + payload: { + parse: false, + output: 'stream' + }, + pre: [ + { method: resources.config.replace.parseArgs, assign: 'args' } + ], + handler: resources.config.replace.handler + } + }) +} diff --git a/src/http-api/routes/id.js b/src/http-api/routes/id.js index 65c0d842ab..6ea05d2559 100644 --- a/src/http-api/routes/id.js +++ b/src/http-api/routes/id.js @@ -1,8 +1,11 @@ -const api = require('./../index.js').server.select('API') const resources = require('./../resources') -api.route({ - method: 'GET', - path: '/api/v0/id', - handler: resources.id.get -}) +module.exports = (server) => { + const api = server.select('API') + + api.route({ + method: 'GET', + path: '/api/v0/id', + handler: resources.id.get + }) +} diff --git a/src/http-api/routes/index.js b/src/http-api/routes/index.js index c715b087c5..2608f4fcbf 100644 --- a/src/http-api/routes/index.js +++ b/src/http-api/routes/index.js @@ -1,7 +1,9 @@ -require('./version') -require('./id') -require('./bootstrap') -// require('./block') -// require('./object') -// require('./repo') -require('./config') +module.exports = (server) => { + require('./version')(server) + require('./id')(server) + require('./bootstrap')(server) + // require('./block')(server) + // require('./object')(server) + // require('./repo')(server) + require('./config')(server) +} diff --git a/src/http-api/routes/object.js b/src/http-api/routes/object.js index 0bf200f95d..fd57859261 100644 --- a/src/http-api/routes/object.js +++ b/src/http-api/routes/object.js @@ -1,10 +1,12 @@ -const api = require('./../index.js').server.select('API') const resources = require('./../resources') // TODO +module.exports = (server) => { + const api = server.select('API') -api.route({ - method: 'GET', - path: '/api/v0/object', - handler: resources.object -}) + api.route({ + method: 'GET', + path: '/api/v0/object', + handler: resources.object + }) +} diff --git a/src/http-api/routes/repo.js b/src/http-api/routes/repo.js index d230c40f30..716777bace 100644 --- a/src/http-api/routes/repo.js +++ b/src/http-api/routes/repo.js @@ -1,10 +1,12 @@ -const api = require('./../index.js').server.select('API') const resources = require('./../resources') // TODO +module.exports = (server) => { + const api = server.select('API') -api.route({ - method: 'GET', - path: '/api/v0/repo', - handler: resources.repo -}) + api.route({ + method: 'GET', + path: '/api/v0/repo', + handler: resources.repo + }) +} diff --git a/src/http-api/routes/version.js b/src/http-api/routes/version.js index e2a8f2a702..ce15e89a5d 100644 --- a/src/http-api/routes/version.js +++ b/src/http-api/routes/version.js @@ -1,8 +1,11 @@ -const api = require('./../index.js').server.select('API') const resources = require('./../resources') -api.route({ - method: 'GET', - path: '/api/v0/version', - handler: resources.version.get -}) +module.exports = (server) => { + const api = server.select('API') + + api.route({ + method: 'GET', + path: '/api/v0/version', + handler: resources.version.get + }) +} diff --git a/tests/test-cli/test-version.js b/tests/test-cli/test-version.js index 5e5083c884..821ee19678 100644 --- a/tests/test-cli/test-version.js +++ b/tests/test-cli/test-version.js @@ -2,14 +2,15 @@ const expect = require('chai').expect const nexpect = require('nexpect') +const httpAPI = require('../../src/http-api') describe('version', () => { describe('api offline', () => { it('get the version', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'version']) - .expect('0.4.0-dev') .run((err, stdout, exitcode) => { expect(err).to.not.exist + expect(stdout[0]).to.equal('ipfs version 0.4.0-dev') expect(exitcode).to.equal(0) done() }) @@ -17,6 +18,28 @@ describe('version', () => { }) describe('api running', () => { - // TODO + before((done) => { + httpAPI.start((err) => { + expect(err).to.not.exist + done() + }) + }) + + after((done) => { + httpAPI.stop((err) => { + expect(err).to.not.exist + done() + }) + }) + + it('get the version', (done) => { + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'version']) + .run((err, stdout, exitcode) => { + expect(err).to.not.exist + expect(exitcode).to.equal(0) + expect(stdout[0]).to.equal('ipfs version 0.4.0-dev') + done() + }) + }) }) }) diff --git a/tests/test-http-api/test-version.js b/tests/test-http-api/test-version.js index 57fed030ba..effe5ff9db 100644 --- a/tests/test-http-api/test-version.js +++ b/tests/test-http-api/test-version.js @@ -35,8 +35,7 @@ describe('version', () => { done() }) - // TODO fix: only fails on travis - it.skip('get the version', (done) => { + it('get the version', (done) => { ctl.version((err, result) => { expect(err).to.not.exist expect(result).to.have.a.property('Version')