From b76de03e2804afcb0b4da16b7e6d923adc639a9e Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 27 Feb 2019 15:17:12 +0100 Subject: [PATCH] ActivityPub now also usable standalone --- src/activitypub/ActivityPub.js | 37 +++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/activitypub/ActivityPub.js b/src/activitypub/ActivityPub.js index c2ea1b2c1..4f5774d9d 100644 --- a/src/activitypub/ActivityPub.js +++ b/src/activitypub/ActivityPub.js @@ -8,13 +8,18 @@ import { sendAcceptActivity, sendRejectActivity } from './utils/activity' +import cluster from 'cluster' +import os from 'os' import request from 'request' import as from 'activitystrea.ms' import NitroDatasource from './NitroDatasource' import router from './routes' import dotenv from 'dotenv' +import express from 'express' +import http from 'http' import { resolve } from 'path' const debug = require('debug')('ea') +const numCPUs = os.cpus().length let activityPub = null @@ -29,11 +34,33 @@ export default class ActivityPub { static init (server) { if (!activityPub) { dotenv.config({ path: resolve('src', 'activitypub', '.env') }) - // const app = express() - activityPub = new ActivityPub(process.env.ACTIVITYPUB_DOMAIN || 'localhost', process.env.ACTIVITYPUB_PORT || 4100) - server.express.set('ap', activityPub) - server.express.use(router) - debug('ActivityPub middleware added to the express service') + const port = process.env.ACTIVITYPUB_PORT + activityPub = new ActivityPub(process.env.ACTIVITYPUB_DOMAIN || 'localhost', port || 4100) + + if (server) { + // integrated into "server" express framework + server.express.set('ap', activityPub) + server.express.use(router) + debug('ActivityPub middleware added to the express service') + } else { + if (cluster.isMaster) { + debug(`master with pid = ${process.pid} is running`) + for (let i = 0; i < numCPUs; i++) { + cluster.fork() + } + cluster.on('exit', (worker, code, signal) => { + debug(`worker ${worker.process.pid} died with code ${code} and signal ${signal}`) + }) + } else { + // Standalone + const app = express() + app.set('ap', activityPub) + app.use(router) + http.createServer(app).listen(port, () => { + debug(`ActivityPub express service listening on port ${port}`) + }) + } + } } else { debug('ActivityPub middleware already added to the express service') }