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

Commit

Permalink
Merge pull request #69 from ipfs/feature/http-api
Browse files Browse the repository at this point in the history
WIP: Make http-api endpoints as specified by the http-api-spec, for the features present in core
  • Loading branch information
daviddias committed Feb 23, 2016
2 parents 50d95a7 + d169279 commit 180a5e4
Show file tree
Hide file tree
Showing 51 changed files with 824 additions and 436 deletions.
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
"main": "src/index.js",
"scripts": {
"lint": "standard",
"coverage": "istanbul cover --print both -- _mocha tests/test-*/index.js",
"coverage": "istanbul cover --print both -- _mocha tests/test-core/index.js",
"test": "npm run test:node && npm run test:browser",
"test:node": "mocha tests/test-*/index.js",
"test:node": "npm run test:node:core && npm run test:node:http-api && npm run test:node:cli",
"test:node:cli": "mocha tests/test-cli/index.js",
"test:node:core": "mocha tests/test-core/index.js",
"test:node:http-api": "mocha tests/test-http-api/index.js",
"test:browser": "karma start karma.conf.js",
"test:core": "mocha tests/test-core/index.js",
"test:cli": "mocha tests/test-cli/index.js",
"test:cli-offline": "mocha tests/test-cli-offline/index.js"
"test:cli": "mocha tests/test-cli/index.js"
},
"pre-commit": [
"lint",
Expand Down Expand Up @@ -67,9 +69,11 @@
"bs58": "^3.0.0",
"debug": "^2.2.0",
"hapi": "^12.0.0",
"ipfs-api": "^2.13.1",
"ipfs-blocks": "^0.1.0",
"ipfs-merkle-dag": "^0.2.1",
"ipfs-repo": "^0.5.0",
"joi": "^8.0.2",
"lodash.get": "^4.0.0",
"lodash.set": "^4.0.0",
"peer-id": "^0.5.0",
Expand Down
19 changes: 16 additions & 3 deletions src/cli/commands/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@ const Command = require('ronin').Command
const httpAPI = require('../../http-api')
const debug = require('debug')
const log = debug('cli:daemon')
log.error = debug('cli:damon:error')
log.error = debug('cli:daemon:error')

module.exports = Command.extend({
desc: 'Start a long-running daemon process',

run: name => {
console.log('Initializing daemon...')
httpAPI.start((err) => {
if (err) { return log.error(err) }
log('daemon started')
if (err) {
return log.error(err)
}
console.log('Daemon is ready')
})

process.on('SIGINT', () => {
console.log('Received interrupt signal, shutting down..')
httpAPI.stop((err) => {
if (err) {
return log.error(err)
}
process.exit(0)
})
})
}
})
29 changes: 21 additions & 8 deletions src/cli/commands/id.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const Command = require('ronin').Command
const IPFS = require('../../ipfs-core')
const debug = require('debug')
const log = debug('cli:id')
log.error = debug('cli:id:error')
const utils = require('../utils')
const log = debug('cli')
log.error = debug('cli:error')

module.exports = Command.extend({
desc: 'Shows IPFS Node ID info',
Expand All @@ -14,11 +15,23 @@ module.exports = Command.extend({
}
},

run: name => {
const node = new IPFS()
node.id((err, id) => {
if (err) { return log.error(err) }
console.log(id)
})
run: (name) => {
if (utils.isDaemonOn()) {
const ctl = utils.getAPICtl()
ctl.id((err, result) => {
if (err) {
return log.error(err)
}
console.log(result)
})
} else {
const node = new IPFS()
node.id((err, id) => {
if (err) {
return log.error(err)
}
console.log(id)
})
}
}
})
32 changes: 32 additions & 0 deletions src/cli/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fs = require('fs')
const os = require('os')
const APIctl = require('ipfs-api')
const multiaddr = require('multiaddr')
const debug = require('debug')
const log = debug('cli')
log.error = debug('cli:error')

exports = module.exports

const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'

exports.isDaemonOn = isDaemonOn
function isDaemonOn () {
try {
fs.readFileSync(repoPath + '/api')
log('daemon is on')
return true
} catch (err) {
log('daemon is off')
return false
}
}

exports.getAPICtl = () => {
if (!isDaemonOn) {
throw new Error('daemon is not on')
}

const apiAddr = multiaddr(fs.readFileSync(repoPath + '/api').toString())
return APIctl(apiAddr.toString())
}
14 changes: 0 additions & 14 deletions src/help-menu.js

This file was deleted.

64 changes: 46 additions & 18 deletions src/http-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,66 @@
const Hapi = require('hapi')
const IPFS = require('../ipfs-core')
const debug = require('debug')
const fs = require('fs')
const os = require('os')
const log = debug('api')
log.error = debug('api:error')

exports = module.exports

exports.start = callback => {
// start IPFS and exports.ipfs = new IPFS()
exports.start = (callback) => {
const ipfs = exports.ipfs = new IPFS()

exports.ipfs = new IPFS()
const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'
try {
fs.statSync(repoPath + '/api')
console.log('This repo is currently being used by another daemon')
process.exit(1)
} catch (err) {
fs.writeFileSync(repoPath + '/api', 'api is on by js-ipfs', {flag: 'w+'})
}

var server = exports.server = new Hapi.Server({
connections: {
routes: {
cors: true
}
ipfs.config.show((err, config) => {
if (err) {
return callback(err)
}
})

server.connection({
port: 9001
})
// TODO: set up cors correctly, following config
var server = exports.server = new Hapi.Server({
connections: {
routes: {
cors: true
}
}
})
const api = config.Addresses.API.split('/')
const gateway = config.Addresses.Gateway.split('/')

// load routes
require('./routes')
// for the CLI to know the where abouts of the API
fs.writeFileSync(repoPath + '/api', config.Addresses.API)

server.start(err => {
if (err) { return callback(err) }
log('server started: ' + server.info.uri)
callback()
// select which connection with server.select(<label>) to add routes
server.connection({ host: api[2], port: api[4], labels: 'API' })
server.connection({ host: gateway[2], port: gateway[4], labels: 'Gateway' })

// load routes
require('./routes')

server.start((err) => {
if (err) {
return callback(err)
}
const api = server.select('API')
const gateway = server.select('Gateway')
console.log('API is listening on: ' + api.info.uri)
console.log('Gateway (readonly) is listening on: ' + gateway.info.uri)
callback()
})
})
}

exports.stop = callback => {
const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'
fs.unlinkSync(repoPath + '/api')
exports.server.stop(callback)
}
Empty file added src/http-api/resources/block.js
Empty file.
27 changes: 27 additions & 0 deletions src/http-api/resources/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const ipfs = require('./../index.js').ipfs
const boom = require('boom')

exports = module.exports

exports.list = (request, reply) => {
ipfs.bootstrap.list((err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
return reply(list)
})
}

exports.add = (request, reply) => {
// ipfs.id((err, id) => {
// if (err) { return reply(boom.badRequest(err)) }
// return reply(id)
// })
}

exports.rm = (request, reply) => {
// ipfs.id((err, id) => {
// if (err) { return reply(boom.badRequest(err)) }
// return reply(id)
// })
}
Empty file.
2 changes: 0 additions & 2 deletions src/http-api/resources/id.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

const ipfs = require('./../index.js').ipfs
const boom = require('boom')

Expand Down
5 changes: 5 additions & 0 deletions src/http-api/resources/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
exports.version = require('./version')
exports.id = require('./id')
exports.bootstrap = require('./bootstrap')
exports.repo = require('./repo')
exports.object = require('./object')
exports.config = require('./config')
exports.block = require('./block')
Empty file.
Empty file added src/http-api/resources/repo.js
Empty file.
25 changes: 20 additions & 5 deletions src/http-api/resources/version.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
'use strict'

const ipfs = require('./../index.js').ipfs
const boom = require('boom')

exports = module.exports

exports.get = (request, reply) => {
ipfs.version((err, version) => {
if (err) { return reply(boom.badRequest(err)) }
return reply(version)
ipfs.version((err, ipfsVersion) => {
if (err) {
return reply(boom.badRequest(err))
}

ipfs.repo.version((err, repoVersion) => {
if (err) {
return reply(boom.badRequest(err))
}

console.log('--------->')

reply({
Version: ipfsVersion,
Commit: '',
Repo: repoVersion
}).header('Transfer-Encoding', 'chunked')
// .header('Trailer', 'X-Stream-Error')
.type('application/json')
})
})
}
10 changes: 10 additions & 0 deletions src/http-api/routes/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const api = require('./../index.js').server.select('API')
const resources = require('./../resources')

// TODO

api.route({
method: 'GET',
path: '/api/v0/block',
handler: resources.block
})
51 changes: 38 additions & 13 deletions src/http-api/routes/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
'use strict'

const server = require('./../index.js').server
const api = require('./../index.js').server.select('API')
const resources = require('./../resources')
const Joi = require('joi')

server.route({
// https://github.com/ipfs/http-api-spec/blob/master/apiary.apib#L818
api.route({
method: 'GET',
path: '/api/v0/bootstrap',
handler: resources.version.list
handler: resources.bootstrap.list
})

server.route({
method: 'POST',
path: '/api/v0/bootstrap',
handler: resources.version.add
// 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()
}
}
}
})

server.route({
method: 'DELETE',
path: '/api/v0/bootstrap',
handler: resources.version.add
// 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()
}
}
}
})
10 changes: 10 additions & 0 deletions src/http-api/routes/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const api = require('./../index.js').server.select('API')
const resources = require('./../resources')

// TODO

api.route({
method: 'GET',
path: '/api/v0/config',
handler: resources.config
})
Loading

0 comments on commit 180a5e4

Please sign in to comment.