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

Commit 7eb9abb

Browse files
refactor: http-api has now clean state
1 parent 9cd595a commit 7eb9abb

28 files changed

+1574
-1544
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"lint": "aegir-lint",
1212
"coverage": "aegir-coverage",
1313
"test": "aegir-test",
14-
"test:node": "aegir-test node && npm run test:node:cli",
15-
"test:node:cli": "mocha test/cli-tests/index.js",
14+
"test:node": "aegir-test node",
1615
"test:browser": "aegir-test browser",
1716
"build": "aegir-build",
1817
"release": "aegir-release",

src/cli/commands/daemon.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
'use strict'
22

33
const Command = require('ronin').Command
4-
const httpAPI = require('../../http-api')
4+
const HttpAPI = require('../../http-api')
55
const debug = require('debug')
66
const log = debug('cli:daemon')
77
log.error = debug('cli:daemon:error')
88

9+
let httpAPI
10+
911
module.exports = Command.extend({
1012
desc: 'Start a long-running daemon process',
1113

1214
run: (name) => {
1315
console.log('Initializing daemon...')
16+
httpAPI = new HttpAPI()
1417
httpAPI.start((err) => {
1518
if (err) {
1619
return log.error(err)

src/http-api/index.js

Lines changed: 71 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,83 +10,92 @@ log.error = debug('api:error')
1010
const IPFSRepo = require('ipfs-repo')
1111
const fsbs = require('fs-blob-store')
1212

13-
exports = module.exports
13+
exports = module.exports = function HttpApi (repo) {
14+
this.ipfs = null
15+
this.server = null
1416

15-
exports.start = (repo, callback) => {
16-
if (typeof repo === 'function') {
17-
callback = repo
18-
repo = undefined
19-
}
17+
this.start = (callback) => {
18+
if (typeof repo === 'string') {
19+
repo = new IPFSRepo(repo, {stores: fsbs})
20+
}
2021

21-
if (typeof repo === 'string') {
22-
repo = new IPFSRepo(repo, {stores: fsbs})
23-
}
22+
this.ipfs = new IPFS(repo)
2423

25-
const ipfs = exports.ipfs = new IPFS(repo)
26-
console.log('Starting at %s', ipfs.repo.path())
27-
ipfs.load(() => {
28-
const repoPath = ipfs.repo.path()
29-
const apiPath = path.join(repoPath, 'api')
30-
console.log('Finished loading')
31-
try {
32-
fs.statSync(apiPath)
33-
console.log('This repo is currently being used by another daemon')
34-
process.exit(1)
35-
} catch (err) {
36-
fs.writeFileSync(apiPath, 'api is on by js-ipfs', {flag: 'w+'})
37-
}
24+
console.log('Starting at %s', this.ipfs.repo.path())
3825

39-
ipfs.config.show((err, config) => {
40-
if (err) {
41-
return callback(err)
26+
this.ipfs.load(() => {
27+
const repoPath = this.ipfs.repo.path()
28+
const apiPath = path.join(repoPath, 'api')
29+
console.log('Finished loading')
30+
try {
31+
fs.statSync(apiPath)
32+
console.log('This repo is currently being used by another daemon')
33+
process.exit(1)
34+
} catch (err) {
35+
fs.writeFileSync(apiPath, 'api is on by js-ipfs', {flag: 'w+'})
4236
}
4337

44-
// TODO: set up cors correctly, following config
45-
var server = exports.server = new Hapi.Server({
46-
connections: {
47-
routes: {
48-
cors: true
49-
}
38+
this.ipfs.config.show((err, config) => {
39+
if (err) {
40+
return callback(err)
5041
}
51-
})
52-
const api = config.Addresses.API.split('/')
53-
const gateway = config.Addresses.Gateway.split('/')
5442

55-
// for the CLI to know the where abouts of the API
56-
fs.writeFileSync(apiPath, config.Addresses.API)
43+
// TODO: set up cors correctly, following config
44+
this.server = new Hapi.Server({
45+
connections: {
46+
routes: {
47+
cors: true
48+
}
49+
}
50+
})
51+
this.server.app.ipfs = this.ipfs
52+
const api = config.Addresses.API.split('/')
53+
const gateway = config.Addresses.Gateway.split('/')
5754

58-
// select which connection with server.select(<label>) to add routes
59-
server.connection({ host: api[2], port: api[4], labels: 'API' })
60-
server.connection({ host: gateway[2], port: gateway[4], labels: 'Gateway' })
55+
// for the CLI to know the where abouts of the API
56+
fs.writeFileSync(apiPath, config.Addresses.API)
6157

62-
// load routes
63-
require('./routes')(server)
58+
// select which connection with server.select(<label>) to add routes
59+
this.server.connection({
60+
host: api[2],
61+
port: api[4],
62+
labels: 'API'
63+
})
64+
this.server.connection({
65+
host: gateway[2],
66+
port: gateway[4],
67+
labels: 'Gateway'
68+
})
6469

65-
ipfs.libp2p.start(() => {
66-
server.start((err) => {
67-
if (err) {
68-
return callback(err)
69-
}
70-
const api = server.select('API')
71-
const gateway = server.select('Gateway')
72-
console.log('API is listening on: %s', api.info.uri)
73-
console.log('Gateway (readonly) is listening on: %s', gateway.info.uri)
74-
callback()
70+
// load routes
71+
require('./routes')(this.server)
72+
73+
this.ipfs.libp2p.start(() => {
74+
this.server.start((err) => {
75+
if (err) {
76+
return callback(err)
77+
}
78+
const api = this.server.select('API')
79+
const gateway = this.server.select('Gateway')
80+
console.log('API is listening on: %s', api.info.uri)
81+
console.log('Gateway (readonly) is listening on: %s', gateway.info.uri)
82+
callback()
83+
})
7584
})
7685
})
7786
})
78-
})
79-
}
80-
81-
exports.stop = (callback) => {
82-
const repoPath = exports.ipfs.repo.path()
87+
}
8388

84-
fs.unlinkSync(path.join(repoPath, 'api'))
89+
this.stop = (callback) => {
90+
const repoPath = this.ipfs.repo.path()
91+
console.log('-> stoping api at %s', repoPath)
92+
fs.unlinkSync(path.join(repoPath, 'api'))
8593

86-
console.log('->', 'going to stop libp2p')
94+
console.log('->', 'going to stop libp2p')
8795

88-
exports.ipfs.libp2p.stop(() => {
89-
console.log('->', 'going to stop api server')
90-
exports.server.stop(callback)
91-
})
96+
this.ipfs.libp2p.stop(() => {
97+
console.log('->', 'going to stop api server')
98+
this.server.stop(callback)
99+
})
100+
}
92101
}

src/http-api/resources/block.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const ipfs = require('./../index.js').ipfs
43
const bs58 = require('bs58')
54
const multipart = require('ipfs-multipart')
65
const Block = require('ipfs-blocks').Block
@@ -37,7 +36,7 @@ exports.get = {
3736
handler: (request, reply) => {
3837
const key = request.pre.args.key
3938

40-
ipfs.block.get(key, (err, block) => {
39+
request.server.app.ipfs.block.get(key, (err, block) => {
4140
if (err) {
4241
log.error(err)
4342
return reply({
@@ -84,7 +83,7 @@ exports.put = {
8483

8584
const block = new Block(data)
8685

87-
ipfs.block.put(block, (err) => {
86+
request.server.app.ipfs.block.put(block, (err) => {
8887
if (err) {
8988
log.error(err)
9089
return reply({
@@ -109,7 +108,7 @@ exports.del = {
109108
handler: (request, reply) => {
110109
const key = request.pre.args.key
111110

112-
ipfs.block.del(key, (err, block) => {
111+
request.server.app.ipfs.block.del(key, (err, block) => {
113112
if (err) {
114113
log.error(err)
115114
return reply({
@@ -131,7 +130,7 @@ exports.stat = {
131130
handler: (request, reply) => {
132131
const key = request.pre.args.key
133132

134-
ipfs.block.stat(key, (err, block) => {
133+
request.server.app.ipfs.block.stat(key, (err, block) => {
135134
if (err) {
136135
log.error(err)
137136
return reply({

src/http-api/resources/bootstrap.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict'
22

3-
const ipfs = require('./../index.js').ipfs
43
const boom = require('boom')
54

65
exports = module.exports
76

87
exports.list = (request, reply) => {
9-
ipfs.bootstrap.list((err, list) => {
8+
request.server.app.ipfs.bootstrap.list((err, list) => {
109
if (err) {
1110
return reply(boom.badRequest(err))
1211
}
@@ -15,14 +14,14 @@ exports.list = (request, reply) => {
1514
}
1615

1716
exports.add = (request, reply) => {
18-
// ipfs.id((err, id) => {
17+
// request.server.app.ipfs.id((err, id) => {
1918
// if (err) { return reply(boom.badRequest(err)) }
2019
// return reply(id)
2120
// })
2221
}
2322

2423
exports.rm = (request, reply) => {
25-
// ipfs.id((err, id) => {
24+
// request.server.app.ipfs.id((err, id) => {
2625
// if (err) { return reply(boom.badRequest(err)) }
2726
// return reply(id)
2827
// })

src/http-api/resources/config.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const ipfs = require('./../index.js').ipfs
43
const debug = require('debug')
54
const get = require('lodash.get')
65
const set = require('lodash.set')
@@ -61,7 +60,7 @@ exports.getOrSet = {
6160

6261
if (value === undefined) {
6362
// Get the value of a given key
64-
return ipfs.config.show((err, config) => {
63+
return request.server.app.ipfs.config.show((err, config) => {
6564
if (err) {
6665
log.error(err)
6766
return reply({
@@ -85,7 +84,7 @@ exports.getOrSet = {
8584
})
8685
} else {
8786
// Set the new value of a given key
88-
ipfs.config.show((err, originalConfig) => {
87+
request.server.app.ipfs.config.show((err, originalConfig) => {
8988
if (err) {
9089
log.error(err)
9190
return reply({
@@ -95,7 +94,7 @@ exports.getOrSet = {
9594
}
9695

9796
const updatedConfig = set(originalConfig, key, value)
98-
ipfs.config.replace(updatedConfig, (err) => {
97+
request.server.app.ipfs.config.replace(updatedConfig, (err) => {
9998
if (err) {
10099
log.error(err)
101100
return reply({
@@ -115,7 +114,7 @@ exports.getOrSet = {
115114
}
116115

117116
exports.show = (request, reply) => {
118-
return ipfs.config.show((err, config) => {
117+
return request.server.app.ipfs.config.show((err, config) => {
119118
if (err) {
120119
log.error(err)
121120
return reply({
@@ -172,7 +171,7 @@ exports.replace = {
172171

173172
// main route handler which is called after the above `parseArgs`, but only if the args were valid
174173
handler: (request, reply) => {
175-
return ipfs.config.replace(request.pre.args.config, (err) => {
174+
return request.server.app.ipfs.config.replace(request.pre.args.config, (err) => {
176175
if (err) {
177176
log.error(err)
178177
return reply({

src/http-api/resources/id.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict'
22

3-
const ipfs = require('./../index.js').ipfs
43
const boom = require('boom')
54

65
exports = module.exports
76

87
exports.get = (request, reply) => {
9-
ipfs.id((err, id) => {
8+
request.server.app.ipfs.id((err, id) => {
109
if (err) { return reply(boom.badRequest(err)) }
1110
return reply(id)
1211
})

0 commit comments

Comments
 (0)