diff --git a/config/env/common.js b/config/env/common.js index c839c56..0daf6c6 100644 --- a/config/env/common.js +++ b/config/env/common.js @@ -78,5 +78,12 @@ module.exports = { // BCH Mnemonic for generating encryption keys and payment address mnemonic: process.env.MNEMONIC ? process.env.MNEMONIC : '', - debugLevel: process.env.DEBUG_LEVEL ? parseInt(process.env.DEBUG_LEVEL) : 2 + debugLevel: process.env.DEBUG_LEVEL ? parseInt(process.env.DEBUG_LEVEL) : 2, + + // Settings for production, using external go-ipfs node. + isProduction: process.env.SVC_ENV === 'production' ? true : false, + ipfsHost: process.env.IPFS_HOST ? process.env.IPFS_HOST : 'localhost', + ipfsApiPort: process.env.IPFS_API_PORT + ? parseInt(process.env.IPFS_API_PORT) + : 5001 } diff --git a/shell-scripts/local-external-ipfs-node.sh b/shell-scripts/local-external-ipfs-node.sh new file mode 100755 index 0000000..a81d112 --- /dev/null +++ b/shell-scripts/local-external-ipfs-node.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# This script is an example for running a production environment, which is +# defined by running an external go-ipfs node. + +# Ports +export PORT=5010 # REST API port + +# The human-readible name that is used when displaying data about this node. +export COORD_NAME=ipfs-service-provider-generic + +# This is used for end-to-end encryption (e2ee). +export MNEMONIC="churn aisle shield silver ladder swear hunt slim pen demand spoil veteran" + +# 0 = less verbose. 3 = most verbose +export DEBUG_LEVEL=1 + +# Production settings that use external IPFS node. +# https://github.com/christroutner/docker-ipfs +export SVC_ENV=production +export IPFS_HOST=localhost +export IPFS_API_PORT=5001 + +# MongoDB connection string. +export DBURL=mongodb://localhost:27017/ipfs-service-dev + +npm start diff --git a/src/adapters/ipfs/ipfs-coord.js b/src/adapters/ipfs/ipfs-coord.js index 142319b..ee53560 100644 --- a/src/adapters/ipfs/ipfs-coord.js +++ b/src/adapters/ipfs/ipfs-coord.js @@ -57,7 +57,7 @@ class IpfsCoordAdapter { } } - this.ipfsCoord = new this.IpfsCoord({ + const ipfsCoordOptions = { ipfs: this.ipfs, type: 'node.js', // type: 'browser', @@ -68,7 +68,14 @@ class IpfsCoordAdapter { apiInfo: this.config.apiInfo, announceJsonLd: this.config.announceJsonLd, debugLevel: this.config.debugLevel - }) + } + + // Production env uses external go-ipfs node. + if (this.config.isProduction) { + ipfsCoordOptions.nodeType = 'external' + } + + this.ipfsCoord = new this.IpfsCoord(ipfsCoordOptions) // Wait for the ipfs-coord library to signal that it is ready. await this.ipfsCoord.start() diff --git a/src/adapters/ipfs/ipfs.js b/src/adapters/ipfs/ipfs.js index 4e2ddba..b45b91f 100644 --- a/src/adapters/ipfs/ipfs.js +++ b/src/adapters/ipfs/ipfs.js @@ -11,7 +11,8 @@ // Global npm libraries // const IPFS = require('ipfs') // const IPFS = require('@chris.troutner/ipfs') -const IPFS = require('ipfs') +const IPFSembedded = require('ipfs') +const IPFSexternal = require('ipfs-http-client') const fs = require('fs') // Local libraries @@ -22,11 +23,17 @@ const IPFS_DIR = './.ipfsdata/ipfs' class IpfsAdapter { constructor (localConfig) { // Encapsulate dependencies - this.IPFS = IPFS + this.config = config + + // Choose the IPFS constructor based on the config settings. + this.IPFS = IPFSembedded // default + if (this.config.isProduction) { + this.IPFS = IPFSexternal + } // Properties of this class instance. this.isReady = false - this.config = config + this.fs = fs } @@ -34,7 +41,7 @@ class IpfsAdapter { async start () { try { // Ipfs Options - const ipfsOptions = { + const ipfsOptionsEmbedded = { repo: IPFS_DIR, start: true, config: { @@ -65,6 +72,16 @@ class IpfsAdapter { } } + const ipfsOptionsExternal = { + host: this.config.ipfsHost, + port: this.config.ipfsApiPort + } + + let ipfsOptions = ipfsOptionsEmbedded + if (this.config.isProduction) { + ipfsOptions = ipfsOptionsExternal + } + // Create a new IPFS node. this.ipfs = await this.IPFS.create(ipfsOptions)