You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

331 lines
9.4 KiB

//main proc for EasyDEX GUI
//this app spawns iguana in background in nontech-mode
const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
var express = require('express')
var bodyParser = require('body-parser')
const path = require('path')
const url = require('url')
const os = require('os')
var fs = require('fs');
var fs = require('fs-extra')
var mkdirp = require('mkdirp');
Promise = require('bluebird');
app.setName('Iguana');
if (os.platform() === 'linux') {
process.env.ELECTRON_RUN_AS_NODE = true
console.log(process.env);
}
// preload.js
const _setImmediate = setImmediate
const _clearImmediate = clearImmediate
process.once('loaded', () => {
global.setImmediate = _setImmediate
global.clearImmediate = _clearImmediate
if (os.platform() === 'darwin') { process.setFdLimit(90000); }
if (os.platform() === 'linux') { process.setFdLimit(1000000); }
})
// GUI APP settings and starting gui on address http://120.0.0.1:17777
var shepherd = require('./routes/shepherd');
var guiapp = express()
guiapp.use(bodyParser.json()); // support json encoded bodies
guiapp.use(bodyParser.urlencoded({ extended: false })); // support encoded bodies
guiapp.get('/', function (req, res) {
res.send('Hello World!')
})
var guipath = path.join(__dirname, '/gui')
guiapp.use('/gui', express.static(guipath))
guiapp.use('/shepherd', shepherd);
var rungui = guiapp.listen(17777, function () {
console.log('guiapp listening on port 17777!')
})
module.exports = guiapp;
// END GUI App Settings
//require('./assets/js/iguana.js'); //below code shall be separated into asset js for public version
// SELECTING IGUANA BUILD TO RUN AS PER OS DETECTED BY DESKTOP APP
var iguanaOSX = path.join(__dirname, '/assets/bin/osx/iguana');
var iguanaLinux = path.join(__dirname, '/assets/bin/linux64/iguana');
var iguanaWin = path.join(__dirname, '/assets/bin/win64/iguana.exe'); iguanaWin = path.normalize(iguanaWin);
var iguanaConfsDirSrc = path.join(__dirname, '/assets/deps/confs');
// SETTING OS DIR TO RUN IGUANA FROM
// SETTING APP ICON FOR LINUX AND WINDOWS
if (os.platform() === 'darwin') {
var iguanaPath = process.env.HOME + '/Library/Application Support/iguana';
var komodoBinPath = path.join(__dirname, '/assets/bin/osx/komodo');
var iguanaBinPath = iguanaOSX;
var iguanaConfsDir = iguanaPath + '/confs';
}
if (os.platform() === 'linux') {
var iguanaPath = process.env.HOME + '/.iguana'
var komodoBinPath = path.join(__dirname, '/assets/bin/linux/komodo');
var iguanaBinPath = iguanaLinux;
var iguanaConfsDir = iguanaPath + '/confs';
var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon_png/128x128.png')
}
if (os.platform() === 'win32') {
var iguanaPath = process.env.APPDATA + '/iguana'; iguanaPath = path.normalize(iguanaPath)
var komodoBinPath = path.join(__dirname, '/assets/bin/win32/komodo');
var iguanaBinPath = iguanaWin;
var iguanaConfsDir = process.env.APPDATA + '/iguana/confs'; iguanaConfsDir = path.normalize(iguanaConfsDir)
var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon.ico')
iguanaConfsDirSrc = path.normalize(iguanaConfsDirSrc);
}
if (os.platform() === 'linux') { var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon_png/128x128.png') }
if (os.platform() === 'win32') { var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon.ico') }
//console.log(iguanaPath);
/*
// MAKE SURE IGUANA DIR IS THERE FOR USER
mkdirp(iguanaPath, function (err) {
if (err)
console.error(err)
else
fs.readdir(iguanaPath, (err, files) => {
files.forEach(file => {
//console.log(file);
});
})
});
// COPY CONFS DIR WITH PEERS FILE TO IGUANA DIR, AND KEEP IT IN SYNC
fs.copy(iguanaConfsDirSrc, iguanaConfsDir, function (err) {
if (err) return console.error(err)
console.log('confs files copied successfully at: '+ iguanaConfsDir )
})
*/
let mainWindow
let loadingWindow
function createLoadingWindow() {
mainWindow = null;
// initialise window
loadingWindow = new BrowserWindow({width: 500, height: 300, frame: false, icon: iguanaIcon})
// load our index.html (i.e. easyDEX GUI)
loadingWindow.loadURL('http://127.0.0.1:17777/gui/');
// DEVTOOLS - only for dev purposes - ca333
//loadingWindow.webContents.openDevTools()
//launchPM2();
// if window closed we kill iguana proc
loadingWindow.on('closed', function () {
// our app does not have multiwindow - so we dereference the window object instead of
// putting them into an window_arr
loadingWindow = null
createWindow('open')
})
//ca333 todo - add os detector to use correct binary - so we can use the same bundle on ALL OS platforms
/*if (os.platform() === 'win32') {
process.chdir(iguanaPath);
//exec(iguanaWin, {cwd: iguanaPath}); //specify binary in startup
ig = spawn(iguanaWin);
}
if (os.platform() === 'linux') {
process.chdir(iguanaPath);
ig = spawn(iguanaLinux);
//corsproxy_process = spawn('corsproxy');
}
if (os.platform() === 'darwin') {
//process.chdir(iguanaPath);
//ig = spawn(iguanaOSX);
//corsproxy_process = spawn('corsproxy');
}*/
//if (os.platform() !== 'win32') { ig.stderr.on( 'error: ', data => { console.log( `stderr: ${data}` ); }); }
}
app.on('ready', createLoadingWindow)
function createWindow (status) {
if ( status === 'open') {
require(path.join(__dirname, 'private/mainmenu'));
// initialise window
mainWindow = new BrowserWindow({width: 1280, height: 800, icon: iguanaIcon})
// load our index.html (i.e. easyDEX GUI)
mainWindow.loadURL('http://127.0.0.1:17777/gui/main.html');
// DEVTOOLS - only for dev purposes - ca333
//mainWindow.webContents.openDevTools()
// if window closed we kill iguana proc
mainWindow.on('closed', function () {
/*
var ConnectToPm2 = function() {
return new Promise(function(resolve, reject) {
console.log('Closing Main Window...');
pm2.connect(function(err) {
console.log('connecting to pm2...');
if (err) {
console.log(err);
}
});
var result = 'Connecting To Pm2: done'
console.log(result)
resolve(result);
})
}
*/
/* //we launch in nodaemonmode - pm2 exits with process
var KillPm2 = function() {
return new Promise(function(resolve, reject) {
console.log('killing to pm2...');
pm2.killDaemon(function(err) {
pm2.disconnect();
console.log('killed to pm2...');
if (err) throw err
});
var result = 'Killing Pm2: done'
setTimeout(function() {
console.log(result)
resolve(result);
}, 2000)
})
}
*/
var HideMainWindow = function() {
return new Promise(function(resolve, reject) {
console.log('Exiting App...');
mainWindow = null
var result = 'Hiding Main Window: done'
console.log(result)
resolve(result);
})
}
var QuitApp = function() {
return new Promise(function(resolve, reject) {
app.quit();
var result = 'Quiting App: done'
console.log(result)
resolve(result);
})
}
/* //
ConnectToPm2()
.then(function(result) {
//return KillPm2();
})
.then(HideMainWindow)
.then(QuitApp)
*/
});
}
}
//app.on('ready', function() {
//createLoadingWindow
//})
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
if (mainWindow === null) {
//createWindow('open');
}
})
function launchPM2() {
/*
if (os.platform() === 'darwin') {
fixPath();
}
pm2.connect(true, function(err) { //start up pm2 god
if (err) {
console.log(err);
process.exit(2);
}
pm2.start({
script : iguanaBinPath, // path to binary
exec_mode : 'fork',
cwd: iguanaPath,
//exec_interpreter = none /Users/crypto/Desktop/dev_projects/pm2_iguana_respawn
//exec_mode : cluster --> for server production enviroment... if highload...
//instances : 4,
//max_memory_restart : '100M' // mem limit for restart
}, function(err, apps) {
pm2.disconnect(); // Disconnect from PM2
if (err) throw err
});
pm2.start({
script : komodoBinPath, // path to binary
exec_mode : 'fork'
// cwd: './iguana_dir/',
//exec_interpreter = none /Users/crypto/Desktop/dev_projects/pm2_iguana_respawn
//exec_mode : cluster --> for server production enviroment... if highload...
//instances : 4,
//max_memory_restart : '100M' // mem limit for restart
}, function(err, apps) {
pm2.disconnect(); // Disconnect from PM2
if (err) throw err
});
});
//pm2 startup workaround
/* var fpath = path.join(__dirname, '/node_modules/pm2/bin/pm2 start iguana2.json');
exec(fpath, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
console.log("pm2 launched...");
//process.exit(2);
*/
}