Skip to content

Commit

Permalink
Added start scripts and updater + bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
explodingcamera committed May 31, 2016
1 parent 2ebceaf commit 1934533
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 29 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ node_modules
socketserver/db
serverconfig.js
webconfig.js
webserver/public/lib/js/webconfig.js
webserver/public/lib/js/webconfig.js
pidfile
log.txt
65 changes: 37 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,70 @@
# mqp-server [![Version npm](https://img.shields.io/npm/v/mqp-server.svg?style=flat-square)](https://www.npmjs.com/package/mqp-server) [![npm Downloads](https://img.shields.io/npm/dm/mqp-server.svg?style=flat-square)](https://www.npmjs.com/package/mqp-server) [![Build Status](https://img.shields.io/travis/musiqpad/mqp-server/master.svg?style=flat-square)](https://travis-ci.org/musiqpad/mqp-server)

[![NPM](https://nodei.co/npm/mqp-server.png)](https://npmjs.org/package/mqp-server)

## About

The base for creating a self-hosted pad.

* [musiqpad.com](https://musiqpad.com)
* [Latest Release](https://github.com/musiqpad/mqp-server/releases/latest)
* [Discord](https://mqp.io/discord)
* [Feedback / Issues](https://mqp.io/feedback)
- [musiqpad.com](https://musiqpad.com)
- [Latest Release](https://github.com/musiqpad/mqp-server/releases/latest)
- [Discord](https://mqp.io/discord)
- [Feedback / Issues](https://mqp.io/feedback)

## Quick Start Install

1. Make sure you have installed [NodeJS](https://nodejs.org/en/download/) on the hosting computer with version 4.0.0 or later.
2. Download the [latest stable version](https://github.com/musiqpad/mqp-server/releases/latest)
3. Unzip it in the location you want to install
4. Open a terminal and `npm install` it
4. Copy the `serverconfig.example.js` to create the file `serverconfig.js`
5. Start the server by running `node start.js` or `npm start`
6. If everything went well, there should be no error messages.
5. Copy the `serverconfig.example.js` to create the file `serverconfig.js`
6. Start the server by running `npm start`
7. If everything went well, there should be no error messages.

You can also download the latest pre-release [here](https://github.com/musiqpad/mqp-server/releases) (rc = release candidate, exp = experimental)
If you want to start musiqpad using an application manager like forever, start the app.js file. To see server logs, run `npm run log` You can also download the latest pre-release [here](https://github.com/musiqpad/mqp-server/releases) (rc = release candidate, exp = experimental)

## Deploying musiqpad using NPM

1. Make sure you have installed NodeJS on the hosting computer with version 4.0.0 or later.
2. Run `npm install mqp-server` in your chosen directory.
3. Copy the `serverconfig.example.js` to create the file `serverconfig.js` and make sure this is located in the root musiqpad folder.
4. Create a javascript file in your directory called 'start.js' and inside the file put:
```javascript
var mqpServer = require('mqp-server');

var server = new mqpServer();

server.start();
```

```javascript
var mqpServer = require('mqp-server');

var server = new mqpServer();

server.start();
```

5. Start the server by running `node start.js`.

6. If everything went well, there should be no error messages.

#### mqpServer.start(params)

### mqpServer.start(params)

Params:

```javascript
{
forever: {
enabled: false,
options: {
root: './logs',
pidPath: './pids',
sockPath: './sock',
debug: false,
stream: false
}
}
forever: {
enabled: false,
options: {
root: './logs',
pidPath: './pids',
sockPath: './sock',
debug: false,
stream: false
}
}
}
```

## API

Please Refer to the [API Documentation](https://musiqpad.com/api/) for the Events, Actions and Data API's.

#### Support
### Support

Please email [support@musiqpad.com](mailto:support@musiqpad.com) if you have any questions or queries.
74 changes: 74 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var config = require('./serverconfig');
var fs = require('fs');
var SocketServer = require("./socketserver/socketserver");
var log = new(require('basic-logger'))({
showTimestamp: true,
prefix: "SocketServer"
});
var path = require('path');

if(!config.setup){
log.error("Please, setup your server by editing the 'serverconfig.js' file");
return;
}

var server = null;

var webConfig = '// THIS IS AN AUTOMATICALLY GENERATED FILE\n\nvar config=JSON.parse(\'' + JSON.stringify(
{
useSSL: config.useSSL,
serverPort: config.socketServer.port,
selfHosted: true,
serverHost: config.socketServer.host
}
) + '\')';

if (config.hostWebserver){
fs.writeFileSync(path.join(__dirname, '/webserver/public/lib/js', 'webconfig.js'), webConfig);
var webserver = require('./webserver/app');
server = (config.socketServer.port == config.webServer.port || config.socketServer.port == '') ? webserver.server : null;
}

if (config.apis.musiqpad.sendLobbyStats && (!config.apis.musiqpad.key || config.apis.musiqpad.key == '')) {
throw 'In order to send stats to the lobby you must generate an key here: https://musiqpad.com/lounge';
}

fs.writeFileSync(path.join(__dirname, '', 'webconfig.js'), webConfig);

var socketServer = new SocketServer(server);

process.on('uncaughtException', function(err) {
console.log(err);
console.log(err.stack);
socketServer.gracefulExit();
});

process.on('exit', socketServer.gracefulExit);

//catches ctrl+c event
process.on('SIGINT', socketServer.gracefulExit);

function fileExistsSync() {
var exists = false;
try {
exists = fs.statSync(path);
} catch(err) {
exists = false;
}

return !!exists;
}

if(process.argv[2] === "--daemon") {
if (fileExistsSync(__dirname + '/pidfile')) {
try {
var pid = fs.readFileSync(__dirname + '/pidfile', { encoding: 'utf-8' });
process.kill(pid, 0);
process.exit();
} catch (e) {
fs.unlinkSync(__dirname + '/pidfile');
}
}

fs.writeFile(__dirname + '/pidfile', process.pid);
}
109 changes: 109 additions & 0 deletions mqp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const chalk = require('chalk');
const cproc = require('child_process');
const fs = require('fs');
const daemon = require('daemon');
const path = require('path');
const update = require('./update');
const tail = require('file-tail');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');

const notifier = updateNotifier({
pkg,
updateCheckInterval: 0,
});
if (notifier.update) {
console.log('Update available ' + chalk.dim(notifier.update.current) + chalk.reset(' → ') + chalk.green(notifier.update.latest));
} else {
console.log(1);
}

function getRunningPid(callback) {
fs.readFile(__dirname + '/pidfile', {
encoding: 'utf-8',
}, function (err, pid) {
if (err) {
return callback(err);
}

try {
process.kill(parseInt(pid, 10), 0);
callback(null, parseInt(pid, 10));
} catch (e) {
callback(e);
}
});
}

switch (process.argv[2]) {
case 'start':
getRunningPid(function (err, pid) {
if (!err) {
console.log('Musiqpad is already running!');
} else {
console.log('\nStarting musiqpad');
console.log(' "' + chalk.yellow.bold('npm stop') + '" to stop the musiqpad server');
console.log(' "' + chalk.yellow.bold('npm run log') + '" to view server output');
console.log(' "' + chalk.yellow.bold('npm restart') + '" to restart musiqpad');

// Spawn a new musiqpad daemon process, might need some more settings but I'm waiting for the new config storage for that.
daemon.daemon(__dirname + '/app.js', '--daemon', {
stdout: fs.openSync(path.join(process.cwd(), 'log.txt'), 'a'),
});
}
});

break;
case 'stop':
getRunningPid(function (err, pid) {
if (!err) {
process.kill(pid, 'SIGTERM');
console.log('Stopping musiqpad!');
} else {
console.log('Musiqpad is already stopped.');
}
});

break;
case 'restart':
getRunningPid(function (err, pid) {
if (!err) {
process.kill(pid, 'SIGTERM');
console.log('\nRestarting musiqpad');
daemon.daemon(__dirname + '/app.js', '--daemon', {
stdout: fs.openSync(path.join(process.cwd(), 'log.txt'), 'a'),
});

} else {
console.log('musiqpad could not be restarted, as a running instance could not be found.');
}
});

break;
case 'log':
console.log('Type ' + 'Ctrl-C ' + 'to exit');

ft = tail.startTailing('./log.txt');
ft.on('line', function (line) {
console.log(line);
});

break;

case 'update':
getRunningPid(function (err, pid) {
if (!err) {
process.kill(pid, 'SIGTERM');
console.log('Stopping musiqpad!');
}

update();
});

break;
default:
console.log('Welcome to musiqpad!');
console.log('Usage: npm run {start|stop|restart|log|update}');
// TODO: Add infos for each cmd
break;
}
46 changes: 46 additions & 0 deletions update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const gitdl = require('download-git-repo');
const request = require('request');
const yesno = require('yesno');
const exec = require('child_process').exec;

function download(tag) {
gitdl('musiqpad/mqp-server#' + tag, process.cwd(), function (err) {
if (err) {
console.log(colors.red('Error: '.error) + err);
process.exit();
} else {
console.log('Download finished, now (re)installing dependencies');
console.log('This step might take some time ...');
exec('npm install', { cwd: (process.cwd()) }, function (error, stdout, stderr) {
if (error) {
console.error(`exec error: ${error}`);
return;
} else {
console.log('Succesfully installed all dependencies of musiqpad');
process.exit();
}
});
}
});
}

module.exports = function update() {
var options = {
url: 'https://api.github.com/repos/musiqpad/mqp-server/releases/latest',
headers: {
'User-Agent': 'request',
},
};
request(options, function (err, res, data) {
if (!err && res.statusCode == 200) {
yesno.ask('This will overide all existing code exept new files and modules. Do you want to continue? (y/N)', false, function (ok) {
if (ok) {
console.log('Now updating to latest version (' + JSON.parse(data).tag_name + ')!');
download(JSON.parse(data).tag_name);
} else
process.exit();
});
} else
console.log('Error getting the latest musiqpad version: ' + err);
});
};

0 comments on commit 1934533

Please sign in to comment.