Restructuring and refactoring

This commit is contained in:
Brad Metcalf 2015-04-03 00:03:35 -05:00
commit c7bbd9e001

View file

@ -1,15 +1,15 @@
var phpwebkit = {
runApp: function(){
var gui = require('nw.gui'); var gui = require('nw.gui');
var win = gui.Window.get(); var win = gui.Window.get();
var http = require('http');
var fs = require('fs');
var php = require('../lib/bridge');
var express = require('express');
var app = express();
var os = require('os'); var os = require('os');
var os = os.platform(); var os = os.platform();
var pw = this;
process.on('uncaughtException', function(err){ process.on('uncaughtException', function(err){
changeState('<strong>'+err+'</strong>', '#CD0000'); pw.changeState('<strong>'+err+'</strong>', '#CD0000');
}); });
win.title = gui.App.manifest.name; win.title = gui.App.manifest.name;
@ -27,8 +27,10 @@ if(bin === undefined || bin == "") {
} }
} }
binExists(bin, function(filename){ this.fileExists(bin, function(result){
bin = filename; if(result === false) {
return 'php-cgi';
}
}); });
var path = gui.App.manifest.phpwebkit.path; var path = gui.App.manifest.phpwebkit.path;
@ -46,38 +48,58 @@ if(port === undefined || port == "") {
port = 0; port = 0;
} }
var phpinfo = { var config = {
"path": path, "path": path,
"bin": bin, "bin": bin,
"host": host, "host": host,
"port": port, "port": port,
"arguments": gui.App.argv, "arguments": gui.App.argv,
"manifest": gui.App.manifest "manifest": gui.App.manifest
}; }
this.startServer(config);
},
startServer: function(config, callback){
var http = require('http');
var php = require('../lib/bridge');
var express = require('express');
var app = express();
var host = config.host;
var port = config.port;
var pw = this;
var server = http.createServer(app); var server = http.createServer(app);
app.use('/', php.cgi(phpinfo)); app.use('/', php.cgi(config));
server.listen(port, host, function(){ server.listen(port, host, function(){
var url = 'http://'+host+':'+server.address().port+'/'; pw.changeState('Starting application...', '#00CD00');
changeState('Starting application...', '#00CD00'); window.location = 'http://'+host+':'+server.address().port +'/';
window.location = url; }).on('error', function(err) {
}).on('error', function(e) { pw.changeState('<strong>Error: '+server.err+'</strong>', '#CD0000');
changeState('<strong>Error: '+e.message+'</strong>', '#CD0000');
}); });
},
changeState: function(msg, color){
function changeState(msg, color){
$('body').css('background-color', color); $('body').css('background-color', color);
$('#loading p').html(msg); $('#loading p').html(msg);
} },
function binExists(file, callback){ fileExists: function(file, callback){
fs.stat(bin, function(err, stats) {
var fs = require('fs');
fs.stat(file, function(err, stats) {
if(!err && stats.isFile()) { if(!err && stats.isFile()) {
return stats.filename; return true;
} else { } else {
return 'php-cgi'; return false;
} }
}); });
} }
};
phpwebkit.runApp();