Skip to content

Commit

Permalink
Pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
msimmer committed Dec 6, 2019
1 parent 0f69883 commit 0a210aa
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 204 deletions.
56 changes: 28 additions & 28 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,74 @@
* Module dependencies.
*/

const app = require('../app')
const debug = require('debug')('dashboard:server')
const http = require('http')
const app = require("../app");
const debug = require("debug")("dashboard:server");
const http = require("http");

/**
* Get port from environment and store in Express.
*/

const port = normalizePort(process.env.PORT || '3000')
app.set('port', port)
const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);

/**
* Create HTTP server.
*/

const server = http.createServer(app)
const server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port)
server.on('error', onError)
server.on('listening', onListening)
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
const port = parseInt(val, 10)
const port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val
return val;
}

if (port >= 0) {
// port number
return port
return port;
}

return false
return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error
if (error.syscall !== "listen") {
throw error;
}

const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`
const bind = typeof port === "string" ? `Pipe ${port}` : `Port ${port}`;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(`${bind} requires elevated privileges`)
process.exit(1)
break
case 'EADDRINUSE':
console.error(`${bind} is already in use`)
process.exit(1)
break
case "EACCES":
console.error(`${bind} requires elevated privileges`);
process.exit(1);
break;
case "EADDRINUSE":
console.error(`${bind} is already in use`);
process.exit(1);
break;
default:
throw error
throw error;
}
}

Expand All @@ -80,7 +80,7 @@ function onError(error) {
*/

function onListening() {
const addr = server.address()
const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`
debug(`Listening on ${bind}`)
const addr = server.address();
const bind = typeof addr === "string" ? `pipe ${addr}` : `port ${addr.port}`;
debug(`Listening on ${bind}`);
}
74 changes: 37 additions & 37 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
/* API for running shell scripts */

const { spawn } = require('child_process')
const { spawn } = require("child_process");

class API {
exec(script, args, callback) {
const proc = spawn(script, args)
const proc = spawn(script, args);

let stdout = ''
let stderr = ''
let stdout = "";
let stderr = "";

console.log(`Running %s with arguments %s`, script, args.join(' '))
console.log(`Running %s with arguments %s`, script, args.join(" "));

proc.stdout.on('data', data => (stdout += String(data)))
proc.stderr.on('data', data => (stderr += String(data)))
proc.stdout.on("data", data => (stdout += String(data)));
proc.stderr.on("data", data => (stderr += String(data)));

proc.on('close', code => {
if (code !== 0) return callback(stderr.trim())
return callback(null, stdout.trim())
})
proc.on("close", code => {
if (code !== 0) return callback(stderr.trim());
return callback(null, stdout.trim());
});
}

addClients(rpcPorts, transmissionPorts, callback) {
let args = []
args = rpcPorts.reduce((acc, port) => acc.concat(['-p', port]), args)
let args = [];
args = rpcPorts.reduce((acc, port) => acc.concat(["-p", port]), args);
args = transmissionPorts.reduce(
(acc, port) => acc.concat(['-P', port]),
(acc, port) => acc.concat(["-P", port]),
args
)
);

this.exec(process.env.SCRIPT_CREATE_CLIENTS, args, callback)
this.exec(process.env.SCRIPT_CREATE_CLIENTS, args, callback);
}

removeClients(rpcPorts, callback) {
const args = rpcPorts.reduce((acc, port) => acc.concat(['-p', port]), [])
this.exec(process.env.SCRIPT_REMOVE_CLIENTS, args, callback)
const args = rpcPorts.reduce((acc, port) => acc.concat(["-p", port]), []);
this.exec(process.env.SCRIPT_REMOVE_CLIENTS, args, callback);
}

startClients(rpcPorts, callback) {
const args = rpcPorts.reduce((acc, port) => acc.concat(['-p', port]), [])
this.exec(process.env.SCRIPT_START_CLIENTS, args, callback)
const args = rpcPorts.reduce((acc, port) => acc.concat(["-p", port]), []);
this.exec(process.env.SCRIPT_START_CLIENTS, args, callback);
}

stopClients(rpcPorts, callback) {
const args = rpcPorts.reduce((acc, port) => acc.concat(['-p', port]), [])
this.exec(process.env.SCRIPT_STOP_CLIENTS, args, callback)
const args = rpcPorts.reduce((acc, port) => acc.concat(["-p", port]), []);
this.exec(process.env.SCRIPT_STOP_CLIENTS, args, callback);
}

createTorrent(fileName, callback) {
this.exec(process.env.SCRIPT_CREATE_TORRENT, [fileName], callback)
this.exec(process.env.SCRIPT_CREATE_TORRENT, [fileName], callback);
}

addTorrents(rpcPorts, torrents, callback) {
let args = []
args = rpcPorts.reduce((acc, port) => acc.concat(['-p', port]), args)
args = torrents.reduce((acc, name) => acc.concat(['-t', name]), args)
let args = [];
args = rpcPorts.reduce((acc, port) => acc.concat(["-p", port]), args);
args = torrents.reduce((acc, name) => acc.concat(["-t", name]), args);

this.exec(process.env.SCRIPT_ADD_TORRENTS, args, callback)
this.exec(process.env.SCRIPT_ADD_TORRENTS, args, callback);
}

removeTorrents(rpcPorts, torrents, callback) {
let args = []
args = rpcPorts.reduce((acc, port) => acc.concat(['-p', port]), args)
args = torrents.reduce((acc, hash) => acc.concat(['-t', hash]), args)
let args = [];
args = rpcPorts.reduce((acc, port) => acc.concat(["-p", port]), args);
args = torrents.reduce((acc, hash) => acc.concat(["-t", hash]), args);

this.exec(process.env.SCRIPT_REMOVE_TORRENTS, args, callback)
this.exec(process.env.SCRIPT_REMOVE_TORRENTS, args, callback);
}

addWhitelistedHashes(hashes, callback) {
const args = hashes.reduce((acc, hash) => acc.concat(['-h', hash]), [])
this.exec(process.env.SCRIPT_TRACKER_ADD_TORRENTS, args, callback)
const args = hashes.reduce((acc, hash) => acc.concat(["-h", hash]), []);
this.exec(process.env.SCRIPT_TRACKER_ADD_TORRENTS, args, callback);
}

removeWhitelistedHashes(hashes, callback) {
const args = hashes.reduce((acc, hash) => acc.concat(['-h', hash]), [])
this.exec(process.env.SCRIPT_TRACKER_REMOVE_TORRENTS, args, callback)
const args = hashes.reduce((acc, hash) => acc.concat(["-h", hash]), []);
this.exec(process.env.SCRIPT_TRACKER_REMOVE_TORRENTS, args, callback);
}

restartTracker(callback) {
this.exec(process.env.SCRIPT_TRACKER_RESTART, [], callback)
this.exec(process.env.SCRIPT_TRACKER_RESTART, [], callback);
}
}

Expand All @@ -95,4 +95,4 @@ class API {
// api.removeTorrents([9091, 9092], ['torrent-1', 'torrent-2'], callback)
// api.createTorrent('foo', callback)

module.exports = new API()
module.exports = new API();
4 changes: 2 additions & 2 deletions lib/create-table-client-torrent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports =
'DROP TABLE IF EXISTS `client_torrents`; \
"DROP TABLE IF EXISTS `client_torrents`; \
CREATE TABLE `client_torrents` ( \
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, \
`client_id` bigint(20) unsigned NOT NULL, \
Expand All @@ -10,4 +10,4 @@ module.exports =
UNIQUE KEY (`client_id`, `torrent_id`), \
CONSTRAINT `client_torrents_ibfk_1` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE CASCADE, \
CONSTRAINT `client_torrents_ibfk_2` FOREIGN KEY (`torrent_id`) REFERENCES `torrents` (`id`) ON DELETE CASCADE \
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;";
2 changes: 1 addition & 1 deletion lib/create-table-clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ module.exports =
PRIMARY KEY (`id`), \
UNIQUE KEY `rcp_port` (`rpc_port`), \
UNIQUE KEY `transmission_port` (`transmission_port`) \
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;"
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;";
2 changes: 1 addition & 1 deletion lib/create-table-ports.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ module.exports =
PRIMARY KEY (`id`), \
UNIQUE KEY `rcp_port` (`rpc_port`), \
UNIQUE KEY `transmission_port` (`transmission_port`) \
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;"
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;";
4 changes: 2 additions & 2 deletions lib/create-table-torrents.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports =
'DROP TABLE IF EXISTS `torrents`; \
"DROP TABLE IF EXISTS `torrents`; \
CREATE TABLE `torrents` ( \
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, \
`name` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL, \
`hash` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL, \
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, \
PRIMARY KEY (`id`) \
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;";
Loading

0 comments on commit 0a210aa

Please sign in to comment.