Skip to content

Instantly share code, notes, and snippets.

@azlkiniue
Created December 11, 2018 13:56
Show Gist options
  • Save azlkiniue/8bb724ca86ffca2317c00d2636dd7a88 to your computer and use it in GitHub Desktop.
Save azlkiniue/8bb724ca86ffca2317c00d2636dd7a88 to your computer and use it in GitHub Desktop.
let publicSocket = null;
const configure = function (io) {
if (!isConfigured())
publicSocket = io;
};
const isConfigured = function () {
return publicSocket != null;
};
const createRefreshResponse = function (refresh) {
return {
refresh: refresh
};
};
const emitEvent = function (eventName, body) {
publicSocket.emit(eventName, body);
};
const emitEventWithRefreshResponse = function (eventName, status) {
emitEvent(eventName, createRefreshResponse(status));
};
const notifyDistributionBandaranRtwDetail = function () {
emitEventWithRefreshResponse('/topic/distribution/bandaran/rtw/detail', true);
};
const notifyDistributionBandaranRtw = function () {
emitEventWithRefreshResponse('/topic/distribution/bandaran/rtw', true);
};
const notifyDistributionBandaranTa = function () {
emitEventWithRefreshResponse('/topic/distribution/bandaran/ta', true);
};
const notifyDistributionBenoaKade = function () {
emitEventWithRefreshResponse('/topic/distribution/benoa-kade', true);
};
const notifyDistributionPerakDetail = function () {
emitEventWithRefreshResponse('/topic/distribution/perak/detail', true);
};
const notifyDistributionPerak = function () {
emitEventWithRefreshResponse('/topic/distribution/perak', true);
};
const notifyDistributionTplJuandaRecord = function () {
emitEventWithRefreshResponse('/topic/distribution/tpl/juanda/record', true);
};
const notifyDistributionTplJuanda = function () {
emitEventWithRefreshResponse('/topic/distribution/tpl/juanda', true);
};
const notifyDistributionTplJuandaTank = function () {
emitEventWithRefreshResponse('/topic/distribution/tpl/juanda/tank', true);
};
const notifyReceivingHarbourPortProduct = function (harbourPortId) {
emitEventWithRefreshResponse('/topic/receiving/harbour/port/' + harbourPortId + '/product', true);
};
const notifyReceivingHarbourPort = function (harbourId) {
emitEventWithRefreshResponse('/topic/receiving/harbour/' + harbourId + '/port', true);
};
const notifyReceivingHarbourProduct = function (harbourId) {
emitEventWithRefreshResponse('/topic/receiving/harbour/' + harbourId + '/product', true);
};
const notifyReceivingHarbour = function () {
emitEventWithRefreshResponse('/topic/receiving/harbour', true);
};
const notifyReceivingHarbourRecord = function (harbourId) {
emitEventWithRefreshResponse('/topic/receiving/harbour/' + harbourId + '/record', true);
};
const notifyReceivingHarbourTank = function (harbourId) {
emitEventWithRefreshResponse('/topic/receiving/harbour/' + harbourId + '/tank', true);
};
const notifyLogbookApproval = function (logbookId) {
emitEventWithRefreshResponse('/topic/logbook/' + logbookId + '/approval', true);
};
const notifyLogbookDetail = function (logbookId) {
emitEventWithRefreshResponse('/topic/logbook/' + logbookId + '/detail', true);
};
const notifyLogbook = function () {
emitEventWithRefreshResponse('/topic/logbook', true);
};
const notifyUserLogbookApprovalCount = function (userId) {
emitEventWithRefreshResponse('/topic/logbook/approval/user/' + userId, true);
};
const notifyProduct = function () {
emitEventWithRefreshResponse('/topic/product', true);
};
const notifyRecordPumpHouse = function (location) {
emitEventWithRefreshResponse('/topic/record/pump-house/' + location, true);
};
const notifyRole = function () {
emitEventWithRefreshResponse('/topic/role', true);
};
const notifyStorage = function (location) {
emitEventWithRefreshResponse('/topic/storage/' + location, true);
};
const notifyReceivingTplTuban = function () {
emitEventWithRefreshResponse('/topic/receiving/tpl/tuban', true);
};
const notifyReceivingTplTubanTank = function () {
emitEventWithRefreshResponse('/topic/receiving/tpl/tuban/tank', true);
};
const notifyUser = function () {
emitEventWithRefreshResponse('/topic/user', true);
};
module.exports = {
configure,
notifyDistributionBandaranRtwDetail,
notifyDistributionBandaranRtw,
notifyDistributionBandaranTa,
notifyDistributionBenoaKade,
notifyDistributionPerakDetail,
notifyDistributionPerak,
notifyDistributionTplJuandaRecord,
notifyDistributionTplJuanda,
notifyDistributionTplJuandaTank,
notifyReceivingHarbourPortProduct,
notifyReceivingHarbourPort,
notifyReceivingHarbourProduct,
notifyReceivingHarbour,
notifyReceivingHarbourRecord,
notifyReceivingHarbourTank,
notifyLogbookApproval,
notifyLogbookDetail,
notifyLogbook,
notifyUserLogbookApprovalCount,
notifyProduct,
notifyRecordPumpHouse,
notifyRole,
notifyStorage,
notifyReceivingTplTuban,
notifyReceivingTplTubanTank,
notifyUser
};
#!/usr/bin/env node
/**
* Resource values
*/
const values = require('../resources/values');
/**
* Module dependencies.
*/
const app = require('../app');
const debug = require('debug')('pertamina-tbbm-dashboard:server');
const http = require('http');
const socketApp = require('../socket/socket-app');
const db = require('../db/mysql-db');
/**
* Get port from environment and store in Express.
*/
const port = normalizePort(process.env.PORT || values.port);
app.set('port', port);
/**
* Create HTTP server.
*/
const server = http.createServer(app);
const io = require('socket.io')(server);
/**
* Listen on provided port, on all network interfaces.
*/
db.connect(function (error) {
if (error)
onDbConnectError(error);
onDbConnectSuccess();
});
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
const port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* DB connect callback
*/
function onDbConnectError(error) {
console.error('Unable to connect to Db...');
if (error)
console.error(error);
process.exit(1);
}
function onDbConnectSuccess() {
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
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;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
const addr = server.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
socketApp.configure(io);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment