ActivityPub now also usable standalone

This commit is contained in:
Armin 2019-02-27 15:17:12 +01:00
parent 25eef848f6
commit b76de03e28

View File

@ -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')
}