fix(ipfs): Got running with external IPFS node

This commit is contained in:
Chris Troutner
2021-12-29 17:28:35 -08:00
parent 37e98e4248
commit d6c3635c6a
4 changed files with 65 additions and 7 deletions
+8 -1
View File
@@ -78,5 +78,12 @@ module.exports = {
// BCH Mnemonic for generating encryption keys and payment address // BCH Mnemonic for generating encryption keys and payment address
mnemonic: process.env.MNEMONIC ? process.env.MNEMONIC : '', 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
} }
+27
View File
@@ -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
+9 -2
View File
@@ -57,7 +57,7 @@ class IpfsCoordAdapter {
} }
} }
this.ipfsCoord = new this.IpfsCoord({ const ipfsCoordOptions = {
ipfs: this.ipfs, ipfs: this.ipfs,
type: 'node.js', type: 'node.js',
// type: 'browser', // type: 'browser',
@@ -68,7 +68,14 @@ class IpfsCoordAdapter {
apiInfo: this.config.apiInfo, apiInfo: this.config.apiInfo,
announceJsonLd: this.config.announceJsonLd, announceJsonLd: this.config.announceJsonLd,
debugLevel: this.config.debugLevel 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. // Wait for the ipfs-coord library to signal that it is ready.
await this.ipfsCoord.start() await this.ipfsCoord.start()
+21 -4
View File
@@ -11,7 +11,8 @@
// Global npm libraries // Global npm libraries
// const IPFS = require('ipfs') // const IPFS = require('ipfs')
// const IPFS = require('@chris.troutner/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') const fs = require('fs')
// Local libraries // Local libraries
@@ -22,11 +23,17 @@ const IPFS_DIR = './.ipfsdata/ipfs'
class IpfsAdapter { class IpfsAdapter {
constructor (localConfig) { constructor (localConfig) {
// Encapsulate dependencies // 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. // Properties of this class instance.
this.isReady = false this.isReady = false
this.config = config
this.fs = fs this.fs = fs
} }
@@ -34,7 +41,7 @@ class IpfsAdapter {
async start () { async start () {
try { try {
// Ipfs Options // Ipfs Options
const ipfsOptions = { const ipfsOptionsEmbedded = {
repo: IPFS_DIR, repo: IPFS_DIR,
start: true, start: true,
config: { 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. // Create a new IPFS node.
this.ipfs = await this.IPFS.create(ipfsOptions) this.ipfs = await this.IPFS.create(ipfsOptions)