diff --git a/package.json b/package.json index adec5ef9b3..7806dd778b 100644 --- a/package.json +++ b/package.json @@ -94,8 +94,8 @@ "async": "^2.1.5", "bl": "^1.2.0", "boom": "^4.2.0", - "debug": "^2.6.3", "cids": "^0.4.2", + "debug": "^2.6.3", "fs-pull-blob-store": "~0.4.1", "glob": "^7.1.1", "hapi": "^16.1.0", @@ -126,6 +126,7 @@ "mkdirp": "^0.5.1", "multiaddr": "^2.2.2", "multihashes": "~0.4.4", + "once": "^1.4.0", "path-exists": "^3.0.0", "peer-book": "~0.3.1", "peer-id": "~0.8.4", diff --git a/src/http-api/index.js b/src/http-api/index.js index f941c7e062..f465b8ae72 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -1,6 +1,6 @@ 'use strict' -const parallel = require('async/parallel') +const series = require('async/series') const Hapi = require('hapi') const debug = require('debug') const fs = require('fs') @@ -9,6 +9,7 @@ const IPFSRepo = require('ipfs-repo') const multiaddr = require('multiaddr') const Store = require('fs-pull-blob-store') const setHeader = require('hapi-set-header') +const once = require('once') const log = debug('api') log.error = debug('api:error') @@ -21,113 +22,116 @@ function uriToMultiaddr (uri) { return `/ip4/${ipPort[0]}/tcp/${ipPort[1]}` } -exports = module.exports = function HttpApi (repo) { - this.ipfs = null - this.server = null +function HttpApi (repo) { + this.node = undefined + this.server = undefined this.start = (callback) => { if (typeof repo === 'string') { repo = new IPFSRepo(repo, {stores: Store}) } - this.ipfs = new IPFS({ - repo: repo, - init: false, - start: false, - EXPERIMENTAL: { - pubsub: true - } - }) - - const repoPath = this.ipfs.repo.path() - - try { - fs.statSync(repoPath) - } catch (err) { - return callback(err) - } - - console.log('Starting at %s', this.ipfs.repo.path()) - - const apiPath = path.join(repoPath, 'api') - - try { - fs.statSync(apiPath) - console.log('This repo is currently being used by another daemon') - process.exit(1) - } catch (err) {} - - this.ipfs._repo.config.get((err, config) => { - if (err) { - return callback(err) - } - - // CORS is enabled by default - this.server = new Hapi.Server({ connections: { routes: { cors: true } } }) - - this.server.app.ipfs = this.ipfs - const api = config.Addresses.API.split('/') - const gateway = config.Addresses.Gateway.split('/') - - // select which connection with server.select(