This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Decouple HTTP Servers from cli/commands/daemon #1950
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a3e0bd4
refactor: decouple HttpApi from cli/commands/daemon
lidel 0b9076d
fix: print HTTP listeners only when run as daemon
lidel 7c32da8
test: use StandaloneDaemon in test/http-api,gateway
lidel 1e82102
refactor: rename StandaloneDaemon to Daemon
lidel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ docs | |
# Logs | ||
logs | ||
*.log | ||
# npm pack | ||
*.tgz | ||
|
||
coverage | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict' | ||
|
||
const debug = require('debug') | ||
|
||
const IPFS = require('../core') | ||
const HttpApi = require('../http') | ||
const WStar = require('libp2p-webrtc-star') | ||
const TCP = require('libp2p-tcp') | ||
const MulticastDNS = require('libp2p-mdns') | ||
const WS = require('libp2p-websockets') | ||
const Bootstrap = require('libp2p-bootstrap') | ||
const promisify = require('promisify-es6') | ||
|
||
class Daemon { | ||
constructor (options) { | ||
this._options = options || {} | ||
this._log = debug('ipfs:daemon') | ||
this._log.error = debug('ipfs:daemon:error') | ||
|
||
if (process.env.IPFS_MONITORING) { | ||
// Setup debug metrics collection | ||
const prometheusClient = require('prom-client') | ||
const prometheusGcStats = require('prometheus-gc-stats') | ||
const collectDefaultMetrics = prometheusClient.collectDefaultMetrics | ||
collectDefaultMetrics({ timeout: 5000 }) | ||
prometheusGcStats(prometheusClient.register)() | ||
} | ||
} | ||
|
||
async start () { | ||
this._log('starting') | ||
|
||
const libp2p = { modules: {} } | ||
|
||
// Attempt to use any of the WebRTC versions available globally | ||
let electronWebRTC | ||
let wrtc | ||
try { | ||
electronWebRTC = require('electron-webrtc')() | ||
} catch (err) { | ||
this._log('failed to load optional electron-webrtc dependency') | ||
} | ||
try { | ||
wrtc = require('wrtc') | ||
} catch (err) { | ||
this._log('failed to load optional webrtc dependency') | ||
} | ||
|
||
if (wrtc || electronWebRTC) { | ||
const using = wrtc ? 'wrtc' : 'electron-webrtc' | ||
this._log(`Using ${using} for webrtc support`) | ||
const wstar = new WStar({ wrtc: (wrtc || electronWebRTC) }) | ||
libp2p.modules.transport = [TCP, WS, wstar] | ||
libp2p.modules.peerDiscovery = [MulticastDNS, Bootstrap, wstar.discovery] | ||
} | ||
|
||
// start the daemon | ||
const ipfsOpts = Object.assign({ init: false }, this._options, { start: true, libp2p }) | ||
const ipfs = new IPFS(ipfsOpts) | ||
|
||
await new Promise((resolve, reject) => { | ||
ipfs.once('error', err => { | ||
this._log('error starting core', err) | ||
err.code = 'ENOENT' | ||
reject(err) | ||
}) | ||
ipfs.once('start', resolve) | ||
}) | ||
|
||
this._ipfs = ipfs | ||
|
||
// start HTTP servers (if API or Gateway is enabled in options) | ||
const httpApi = new HttpApi(ipfs, ipfsOpts) | ||
this._httpApi = await httpApi.start() | ||
|
||
// for the CLI to know the where abouts of the API | ||
if (this._httpApi._apiServers.length) { | ||
await promisify(ipfs._repo.apiAddr.set)(this._httpApi._apiServers[0].info.ma) | ||
} | ||
|
||
this._log('started') | ||
return this | ||
} | ||
|
||
async stop () { | ||
this._log('stopping') | ||
await Promise.all([ | ||
this._httpApi && this._httpApi.stop(), | ||
this._ipfs && this._ipfs.stop() | ||
]) | ||
this._log('stopped') | ||
return this | ||
} | ||
} | ||
|
||
module.exports = Daemon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌