Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Refactor HTTP API routes #84

Merged
merged 2 commits into from
Mar 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/cli/commands/version.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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)
})
}
})
2 changes: 1 addition & 1 deletion src/http-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/http-api/resources/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ exports.get = (request, reply) => {
Version: ipfsVersion,
Commit: '',
Repo: repoVersion
}).header('Transfer-Encoding', 'chunked')
.type('application/json')
})
})
})
}
14 changes: 8 additions & 6 deletions src/http-api/routes/block.js
Original file line number Diff line number Diff line change
@@ -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
})
}
77 changes: 40 additions & 37 deletions src/http-api/routes/bootstrap.js
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
}
})
})
}
63 changes: 33 additions & 30 deletions src/http-api/routes/config.js
Original file line number Diff line number Diff line change
@@ -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
}
})
}
15 changes: 9 additions & 6 deletions src/http-api/routes/id.js
Original file line number Diff line number Diff line change
@@ -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
})
}
16 changes: 9 additions & 7 deletions src/http-api/routes/index.js
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 8 additions & 6 deletions src/http-api/routes/object.js
Original file line number Diff line number Diff line change
@@ -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
})
}
14 changes: 8 additions & 6 deletions src/http-api/routes/repo.js
Original file line number Diff line number Diff line change
@@ -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
})
}
15 changes: 9 additions & 6 deletions src/http-api/routes/version.js
Original file line number Diff line number Diff line change
@@ -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
})
}
27 changes: 25 additions & 2 deletions tests/test-cli/test-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,44 @@

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()
})
})
})

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()
})
})
})
})
3 changes: 1 addition & 2 deletions tests/test-http-api/test-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down