Verified Commit ffb86872 authored by Aral Balkan's avatar Aral Balkan
Browse files

Add --live option for launching the server as a daemon using pm2

parent 7105b1fe
......@@ -8,12 +8,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### To-do
- Integrate pm2 for production use.
- Create native binary releases.
### Done
## [6.5.0] - 2019-03-31
## Added
- --live flag to run as daemon and handle crashes/restarts via integrated pm2 process manager.
## [6.4.0] - 2019-03-31
......
//
// To be run as a daemon by the pm2 process manager when
// web-server is called with the --live option.
//
// Expects the path to serve as the only command-line argument.
//
const args = process.argv.slice(2)
if (args.length !== 1) {
console.log('\n 💕😈💕 Indie Web Server Daemon\n\n Please use via web-server --live instead.\n')
process.exit(1)
}
const pathToServe = args[0]
// Run the web server.
const webServer = require('../index.js')
webServer.serve({
pathToServe,
global: true
})
#!/usr/bin/env node
const os = require('os')
const fs = require('fs')
const path = require('path')
var ansi = require('ansi-escape-sequences')
const ansi = require('ansi-escape-sequences')
const webServer = require('../index.js')
const pm2 = require('pm2')
const arguments = require('minimist')(process.argv.slice(2))
if (arguments._.length > 2 || arguments.help === true) {
......@@ -57,12 +60,45 @@ if (!fs.existsSync(pathToServe)) {
process.exit(1)
}
// Start the server.
webServer.serve({
// If live mode is specified, run as a daemon using pm2,
// otherwise, start it as a regular process.
if (arguments.live !== undefined) {
//
// Start a pm2 daemon.
//
pm2.connect((error) => {
if (error) {
console.log(error)
process.exit(1)
}
pm2.start({
script: path.join(__dirname, 'daemon.js'),
args: pathToServe,
name: 'indie-web-server',
output: '~/.web-server/logs/output.log',
error: '~/.web-server/logs/error.log',
pid: '~/.web-server/pids/server.pid',
}, (error, processObj) => {
pm2.disconnect()
if (error) {
throw error
}
console.log(`${webServer.version()}\n 😈 Launched as a daemon on https://${os.hostname()}\n`)
})
})
} else {
//
// Start a regular server process.
//
webServer.serve({
path: pathToServe,
port,
global
})
})
}
// Helpers.
......
{
"name": "@ind.ie/web-server",
"version": "6.4.0",
"version": "6.5.0",
"description": "A secure and seamless Small Tech personal web server.",
"main": "index.js",
"bin": "bin/web-server.js",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment